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.

1wait(1)
2function onTouched(part)
3    local h = part.Parent:findFirstChild("Humanoid")
4    if h~=nil then
5        h.Health = h.Health -5
6    end
7end
8 
9script.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.

01local Disabled = false --debounce
02 
03function onTouched(part)
04    if not Disabled then -- If it's not disabled then
05        Disabled = true -- disable :connect
06 
07        local h = part.Parent:findFirstChild("Humanoid")
08 
09        if h~=nil then
10            h.Health = h.Health -5
11        end
12 
13        wait(1)
14 
15        Disabled = false -- enable :connect
16    end
17end
18 
19script.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:

1-- Put this in startercharacterscripts under starterplayer.
2 
3while wait(1) do -- Run every second.
4    if script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Swimming then -- Check if they are in terrain water.
5        script.Parent.Humanoid:TakeDamage(1) -- Take damage.
6    end
7end

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