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

How do you make a slow kill script where it stops once they are off the brick?

Asked by 5 years ago

I'm trying to make a slow kill script to slowly kill a person when they are on the brick, but it stops damaging the person when they step off of it. Here's the code I have, it works but it doesn't stop when they step off the brick:

function onTouched(hit)
local h = hit.Parent:findFirstChild("Humanoid")
    repeat
     h.Health = h.Health - 0.2
     wait(2)
    until h.Health == 0
end

script.Parent.Touched:connect(onTouched)

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

BasePart:GetTouchingParts() seems promising.

local Players = game:GetService("Players")
local Debounce = .3
local Damage = 5
while wait(Debounce) do
    local Connection = script.Parent.Touched:Connect(function() end)
    for _,Object in pairs(script.Parent:GetTouchingParts()) do
        Connection:Disconnect()
        local Rig = Object:FindFirstAncestorWhichIsA("Model")
        if Rig ~= nil and Players:GetPlayerFromCharacter(Rig) ~= nil then
            local Humanoid = Rig:FindFirstChildOfClass("Humanoid")
            if Humanoid ~= nil then
                Humanoid:TakeDamage(Damage)
            end
        end
    end
end
0
do not use while wait() do DeceptiveCaster 3761 — 5y
0
@MCAndRobloxUnited I don't think you understand how this works. They would have the same functionality, and I can gaurentee you are incorrect. CorruptScript 86 — 5y
0
Thanks xItzDogey 4 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You can simply do this:

script.Parent.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        wait(2)
        hum:TakeDamage(0.2) -- Gradual damage
    end
end)

The reason why your script keeps going is because you're telling it to repeat the action even when the Humanoid stops making contact with the Part. (You should never do this.)

0
and no, this doesn't spam the game with damage actions but if it does then add a debounce DeceptiveCaster 3761 — 5y

Answer this question