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)
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
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.)