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

How do I make terrain water detection?

local touchingWater = false
local otherPart
game.Workspace.TerrainTouched:connect(function(oP)
	otherPart = oP
	if oP.Parent == Plr.Character then
		touchingWater = true
	end
end)
game.Workspace.Terrain.TouchEnded:connect(function(otherPart)
		touchingWater = false
end)

This applies for all terrain, but how do I specify only water?

4 comments

0

Is TerrainTouched even an event of Workspace? Never seen it in the API.

0

oh sorry it's suppose to be Terrain.Touched. I was trying stuff like Terrain.Water.Touched just for the heck of it. When I deleted Water, I deleted the "." too.

0

There is no specific event for touching water. As far as I know, Terrain Touched and TouchEnded don't do anything.

0

There is no but you can put a invisible part without collision on the water so maybe would work with OnTouched

1 answer

✓ Accepted answer

Typed all this on mobile so sorry if anything is weird/inaccurate.

If you want to detect when a player is in water or not, you can use the :GetState() function and the HumanoidStateType enum.

Here is a rough example :

local humanoid = somewhereidk

if humanoid:GetState() == Enum.HumanoidStateType.Swimming then
    print('Swimming')
end

In this script, the script checks the state of the humanoid. If it’s swimming, it prints “Swimming”, otherwise it does nothing.

Igoralexeymarengobr’s comment can also work too, but you’ll have to put the parts in every single area filled with water. I would prefer my way if you’re planning on only characters to be detected in water. If you want separate parts to be detected though, you’ll probably have to make the invisible parts, or do something else but I don’t know because I can’t test anything at the moment.

Oh, and if you’re trying to check if any player touches the water, you can make a script like my example and put it in StarterCharacterScripts, or get all the players at once and check if they’re in water or not.

Hope this helped!

1 comment

0

thank you!

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