Hello, I am experiencing issues with this script. I need this to take damage whenever a player steps on it, I tried a local and a script, non worked! I feel that local script should be used. script.Parent.Touched:Connect(function() local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") if Humanoid then Humanoid:TakeDamage(100)
Don’t use a LocalScript to deal damage, as it will only display damage on the person that’s touched the part, damage won’t replicate, I suggest you use a Script and use this code instead:
local Part = script.Parent
Part.Touched:Connect(function(Hit) -- parameter provided by the touched function
local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid then
Humanoid:TakeDamage(100)
end
end)
To furthermore explain, the Touched function provides a parameter (Hit), which is the Part that collides with the Part connected to the Touched Event.
Then, we can get the parent of the part and check it’s children to find a Humanoid, if it finds it then, the code runs and the part deals damage to whoever it touched, if it doesn’t it won’t run, but it also won’t break like say finding a Humanoid with :WaitForChild(), since doing that would make the thread yield forever.
For one your ends are missing. You should also use a server script for this. Hit provides not just a player but ANYTHING including a part touching it so you should check if it has a humanoid.
script.Parent.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChildOfClass("Humanoid")
if h then -- does the humanoid exist?
h:TakeDamage(100) -- takes damage
end -- end from the if statement
end) -- end from the function
just put that script in ur object you want to set
local Howmuchdamage = 10
script.Parent.Touched:Connect(function(Object)
local touchpart = Object.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health ~= 0 then
hit.Parent.Humanoid:TakeDamage(Howmuchdamage)
end
end)
end)