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

how do I make a brick that damages the player when he is on it??

Asked by
nachsor 36
6 years ago

Im having a problem. I wanted to make a brick which while the player is on the brick, it damages him.

My script is:

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

end)

to me, the while do loop would only work if the player is still on the brick. But when I run it, its a soon as he steps on the brick, it continues to do the loop until he dies. I also tried this:

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

end)

and this:

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

end)

But none of the above work. I really need help. Thank you Nachsor

2 answers

Log in to vote
0
Answered by 6 years ago

:connect() is deprecated, switch to :Connect

If you want it to damage WHILE it’s standing on the brick, and stop when it stops touching, do this, it will teach you a thing or two.

local debounce = false
local touching = false

script.Parent.Touched:Connect(function(part) -- connect is deprecated use Connect
    local hum = part.Parent:FindFirstChild"Humanoid"

    if hum then
        if not debounce then
            debounce = true
            touching = true 

            while touching do
                hum:TakeDamage(5)
                wait(.5)
            end

        wait(.5)
        debounce = false
        end
    end
end)

script.Parent.TouchEnded:Connect(function(part)
    touching = false
end)
Ad
Log in to vote
0
Answered by
nachsor 36
6 years ago

Hello incapaz,

Thank you, that does work but only if the player is moving. This is still really helpful and is better than the situation was stuck in before.

Thank you very much for your help, it is very appreciated.

Thank you Nachsor

Answer this question