Roblox: Make an NPC immovable? - lua

I'm currently working on a game on ROBLOX that contains a TON of NPC's. I need a way to make the player not be able to move them around in any way. I've tried anchoring the HumanoidRootPart, and that worked, but it made the NPC unable to move.
Can anyone help?

You could weld the NPC to the ground, if possible.
This could work:
local weld = Instance.new("WeldConstraint")
weld.Part0 = part
weld.Part1 = otherPart
weld.Parent = part
There is more info here on welding.
If you don't need players to clip through said NPCs, you could "unclip" the NPC, allowing players to move through it but not move it.
local function setDescendantCollisionGroup(Descendant)
if Descendant:IsA("BasePart") and Descendant.CanCollide then
-- set collision group
end
end
model.DescendantAdded:Connect(setDescendantCollisionGroup)
for _, descendant in pairs(model:GetDescendants()) do
setDescendantCollisionGroup(descendant)
end

This should be able to be done using a property . Create a startercharacter so players wear this character,and then modify it's customphysicalproperties "friction" , "density" to a very low number.
You should also be able to put such in a script such as when a player join,the children with class "Part" have their density and friction low.

Try something like this:
local NPC = workspace.NPC --The NPC variable
NPC.HumanoidRootPart.Mass = 69420
It should make the NPC alot heavier!
And when you want it to move:
local NPC = workspace.NPC --The NPC variable
NPC.HumanoidRootPart.Mass = 10
That will make the NPC lighter!
So this is the final script:
local NPC = workspace.NPC --The NPC variable
local humanoid = NPC:WaitForChild('Humanoid') --NPC's humanoid
local hrp = NPC.HumanoidRootPart --NPC's HumanoidRootPart
local mass1 = 69420
local mass2 = 10
humanoid.Running:Connect(function(speed)
if speed > 0.001 then
hrp.Mass = mass2
else
hrp.Mass = mass1
end
end)
Make sure to write that code in a ServerScript!
And put the Script inside the NPC if you want.

Related

attempt to index nil with leaderstats

I'm making a game where you ram cars into houses and it unanchored the parts that are touched by a part in the front of the car. I want to have it so that whenever you unanchor a part, you get a coin
local HitPart = script.Parent
local function onTouch(otherPart)
local player = otherPart.Parent
if otherPart then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if otherPart then
game.Players.LocalPlayer.leaderstats.coins.Value = game.Players.LocalPlayer.leaderstats.coins.Value + 1
end
end
end
HitPart.Touched:Connect(onTouch)
HitPart is the part that is touching the other parts. However, I keep getting "attempt to index nil with 'leaderstats'" error. does anyone know whats wrong?
Have you created the leaderstats inside the player? Here's an in-depth article about In-game leaderboards by Roblox
The error is telling you that you are trying to access the leaderstats of a player that doesn't exist.
You have a few issues.
Since this is (probably) a Script, game.Players.LocalPlayer doesn't exist. LocalPlayer only exists in LocalScripts.
Since the Touched signal will fire for any object that touches it, you need to add safety checks that the player object actually exists
To make finding a player easier, I would recommend the Players:GetPlayerFromCharacter function. You pass it a Model from the Workspace, and it tells you if you've got a Player.
local HitPart = script.Parent
local function onTouch(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:FindPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
HitPart.Touched:Connect(onTouch)

Roblox Lua Give LocalPlayer weapon doesn't work,

My code: https://justpaste.it/87evk
local original = game:GetService("ReplicatedStorage"):FindFirstChild("CloneSmoke")
local tool = script.Parent
local copy = original:Clone()
tool.Activated:Connect(function()
print("Running...")
local sound = script.Parent.Sound
copy.Parent = script.Parent.Handle
sound:Play()
wait(3)
tool:Destroy()
local plr = game.Players.LocalPlayer
local weapon = game.ReplicatedStorage.Jutsus.Momoshiki["Momoshikis Sword"]
local w2 = weapon:Clone()
w2.Parent = plr.Backpack
end)
Idk what i have to do here to get the Player and give him a Weapon.I tried much but i dont get the right Soloution.
It were nice wenn you help me
First off, make sure this is in a LocalScript
Also change local original = game:GetService("ReplicatedStorage"):FindFirstChild("CloneSmoke") to local original = game:GetService("ReplicatedStorage"):WaitForChild("CloneSmoke") This is because the script loads faster than items in the game, so chances are, the sword hasn't loaded into the game by the time the script runs; the script won't work if it doesn't find the object you are trying to reference.

Why doesn't this change gui code work for Roblox Studio?

I am trying to create a TextLabel that changes every 5 seconds. I have this code, but it doesn't work.
local player = game.Players:GetPlayerFromCharacter(part.Parent) —this is our gateway to getting the PlayerGui object.
local PlayerUI = player:WaitForChild(“PlayerGui”)
local txtLabel = PlayerUI[“Welcome_Text”].TextLabel
while x < 1 do
wait(5)
txtLabel.Text = “Welcome to The Shadow Realm!”
wait(5)
txtLabel.Text = “Warning: This game contains scenes that may be too scary for some roblox players”
end
I am getting an error message that says.
ServerScriptService.Script:2: attempt to index global 'part' (a nil value)
I don't know where to put my gui.
If I am correctly understanding what you are trying to do, you should be able to create a ScreenGui and place it in StartGui so that every player will have it copied to his PlayerGui when he joins the game. You could place a LocalScript inside of that GUI that would control the text on screen.
I see your LocalScript looking something like this:
-- Customize names on your own; these are just generic placeholders.
-- The structure of the GUI would look like this:
--
-- ScreenGui
-- LocalScript
-- Frame
-- TextLabel
local WELCOME = "Welcome to the Shadow Realm!"
local WARNING = "Warning! This game contains scenes that may be too scary for some players"
local runService = game:GetService("RunService")
local gui = script.Parent
local frame = gui.Frame
local label = frame.TextLabel
local function update_text()
label.Text = WELCOME
wait(5)
label.Text = WARNING
return
end
runService.RenderStepped:Connect(update_text)
Making this a LocalScript within a client-side GUI shifts the overhead to the client and eliminates the need to use the method Players::GetPlayerFromCharacter.

I am trying to change the Mesh texture of a tool in a players backpack

I made a part and added a script with a function that detects if the part was touched. If a player touches this part I need to access that players Backpack and a find a tool. In this case, it is a Pistol. I then try to change the TextureId of the Pistols mesh, which is inside of the Handle part within the Pistol
I have tried accessing the players backpack by using hit.Parent.Backpack,
but when I touch the part I get an error in the console saying
Backpack is not a valid member of Accessory
Here is the entire script...
function onTouched(hit)
local player = hit.Parent
local p = player.Backpack:FindFirstChild("Pistol")
local h = p:FindFirstChild("Handle")
local m = h:FindFirstChild("Mesh")
local id = m.TextureId
id = "rbxassetid://3707943717"
end
script.Parent.Touched:Connect(onTouched)
The expected result should be:
When a player touches this part it should look for a "Pistol" in the players Backpack and then look for the pistols "Mesh" and change the meshes "TextureId" to whatever texture I have set it to in the script.
I was able to fix this after some more research. My first mistake is that hit.Parent seems to only be the model. So i used that to find the "Humanoid" then from there got the name of the player so that i can find the player in the player list and access the backpack to change the pistols "Mesh.TextureId"
Here is the new script...
function onTouched(m)
local p = m.Parent:FindFirstChild("Humanoid")
if p ~= nil then
local n = p.Parent.Name
local player = game.Players:FindFirstChild(n, false) -- find the player that hit the Part.
local gun = player.Backpack:FindFirstChild("Pistol")
if player == nil then return end -- escape if something goes wrong.
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh
mesh.TextureId = "rbxassetid://3707943717" -- change the gun texture
end
end
script.Parent.Touched:Connect(onTouched)

Removing a player's leaderstats if they click a gui on roblox

Some simple code
script.Parent.MouseButton1Up:connect(function()
????.leaderstats.lvl.Value = 0
????.leaderstats.xp.Value = 0
????.leaderstats.gold.Value = 0
It's not even working. So the player clicks the gui, but how can it reset the players leaderstats, specifically lvl, xp, and gold, I run a fairly popular roblox rpg game with about 400 people right now and this would be an immense help.
You could put the following code in a LocalScript inside your button.
Player = game.Players.LocalPlayer --Only works inside a localscript, replace with the line below it if you need it as a Script.
-- Player=script.Parent while (not Player:IsA("Player")) do Player = Player.Parent end
script.Parent.MouseButton1Up:connect(function()
local index, stat
for index, stat in pairs(Player.leaderstats:GetChildren()) do
stat.Value = 0
end
end)
It should be in a LocalScript, so you can just use the LocalPlayer variable to get the leaderstats and set them.

Resources