so i have this slow killing script, I want it to stop damaging the player when the player steps out of the "gas" area can anyone help?
function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if h~=nil then h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -1 wait (1.5) h.Health = h.Health -100 wait (1.5) end end script.Parent.Touched:connect(onTouched)
for
loops, these are your friends in code shortening.local touching = false -- checks if touching local function onTouch(part) local hum = part.Parent:FindFirstChild("Humanoid") -- uppercase F, lowercase f is deprecated if hum then -- no need for ~= nil touching = true -- it's a humanoid, has to be a player for i = hum.Health, 0, -1 do -- decrease health by 1 each increment if touching then -- make sure they're touching the gas!! hum.Health = i wait(1.5) else -- player isn't touching the gas... break -- ... we're going to end the loop since they didn't touch! end end end end script.Parent.Touched:Connect(onTouch) -- Use Connect instead of connect, the latter is deprecated and should not be used in new work script.Parent.TouchEnded:Connect(function(part) if part.Parent:FindFirstChild("Humanoid") then touching = false -- set to false on touch end end end)
Instead of only damaging the player while they are inside the area, you are checking to see if they step into the area then damaging them until they die. You should reconsider how you are going about this, and think about using a while
loop. The while loop will check for a condition and see if it is true before looping again.
Here is an idea of what you should do:
local playerIsInGas = false gas.Touched:Connect(function() playerIsInGas = true while playerIsInGas then playerHumanoid:TakeDamage(1.5) wait(1.5) end end) gas.TouchEnded:Connect(function() playerIsInGas = false end)
Also, please use code blocks (the blue Lua button) to format your code.
Hope this helps! :)