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

How do I make this water script slowly kill you?

Asked by 6 years ago

I'm making this script where when I stand in this water it makes the player lose 5 health every 1 second they stand in the water.

wait(1)
function onTouched(part) 
    local h = part.Parent:findFirstChild("Humanoid") 
    if h~=nil then 
        h.Health = h.Health -5
    end 
end 

script.Parent.Touched:connect(onTouched)

2 answers

Log in to vote
1
Answered by 6 years ago

You need to add a debounce if you want the part to damage the player every 1 second.

local Disabled = false --debounce

function onTouched(part)
    if not Disabled then -- If it's not disabled then
        Disabled = true -- disable :connect

        local h = part.Parent:findFirstChild("Humanoid") 

        if h~=nil then 
            h.Health = h.Health -5
        end

        wait(1)

        Disabled = false -- enable :connect
    end
end 

script.Parent.Touched:connect(onTouched)
0
Accept incapaz's answer, it's more advanced GatitosMansion 187 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

There is actually a function for this!!

THIS WILL ONLY WORK IF YOU ARE USING TERRAIN WATER.

Here's basic code:

-- Put this in startercharacterscripts under starterplayer.

while wait(1) do -- Run every second.
    if script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Swimming then -- Check if they are in terrain water.
        script.Parent.Humanoid:TakeDamage(1) -- Take damage.
    end
end

That will only work if you are using terrain water as you say you are using water.

0
Ew don't use wait as your condition. User#19524 175 — 6y
0
no, just using a brick iRoklas 1 — 6y
0
ah.. alright. outlook1234567890 115 — 6y

Answer this question