Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago
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.

0
thankie winner208 74 — 6y
Ad

Answer this question