Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i stop this script from breaking when it touches a object without a humanoid?

i have this script it damages the humanoid upon being touched but it breaks after touching something without a humanoid in it. here is the script

local PartHit = false

script.Parent.Touched:Connect(function(hit)
	
	if not PartHit then
		
		PartHit = true
		
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
		
		wait(0.5)
		
		PartHit = false
		
	end
end)

thanks in advance

1 answer

✓ Accepted answer

You need to check if there is a humanoid, so it might look something like this

local PartHit = false

script.Parent.Touched:Connect(function(hit)

	local hum = hit.Parent:FindFirstChild("Humanoid")

    if not PartHit and hum then

        PartHit = true

        hum.Health = hum.Health - 10

        wait(0.5)

        PartHit = false

    end

end)

This discussion is preserved as it was. New answers, comments, and votes are closed.