Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this water script slowly kill you?

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

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)

1 comment

0

Accept incapaz's answer, it's more advanced

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.

3 comments

0

Ew don't use wait as your condition.

0

no, just using a brick

0

ah.. alright.

This discussion is preserved as it was. New answers, comments, and votes are closed.