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

How do I make an effect stop when the object is not being touched?

I'm trying to make a "hitbox" that stops taking health when you step out of it, though when you step out of it, it just keeps taking health, any solutions? here is my script:

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		while hit.Parent.Humanoid do
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 1
			wait()
		end
	end
end)

1 answer

local dealdamage = false

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and not dealdamage then
		dealdamage = true
        while hit.Parent.Humanoid do
		if dealdamage then
           	 hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 1
		else
			break;
		end
            wait()
        end
    end
end)

script.Parent.TouchEnded:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		dealdamage = false;
	end
end)

Basically this script will deal damage when the player touches the part and then stop dealing damage once the player steps off the part. Also, you might want to think about lowering the damage you're dealing to the player. 1 might kill the player a little fast.

1 comment

0

thankie

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