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.
1 | wait( 1 ) |
2 | function onTouched(part) |
3 | local h = part.Parent:findFirstChild( "Humanoid" ) |
4 | if h~ = nil then |
5 | h.Health = h.Health - 5 |
6 | end |
7 | end |
8 |
9 | script.Parent.Touched:connect(onTouched) |
You need to add a debounce if you want the part to damage the player every 1 second.
01 | local Disabled = false --debounce |
02 |
03 | function 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 |
17 | end |
18 |
19 | script.Parent.Touched:connect(onTouched) |
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 |
3 | while 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 |
7 | end |
That will only work if you are using terrain water as you say you are using water.