Detecting When a Player Walks Through Water

How would I go about detecting when a player walks through terrain water? My game has shallow pools of water. If a player walks through water, I want it to play a sound for walking through water. I have a toolbox footstep script so I was thinking that instead of making a separate sound script, I could make it so that when the player enters water, the footstep sound ID changes to a water sound ID.

Only problem is: How would I detect when a player enters the water?

Should I make a part that when the player enters it, it changes the sound? Or is there another more efficient way?

Side Note: I also need a good “walking through water” sound if you have one :smile:

1 Like

Use Raycasting and fire a ray straight down. It will identify what the ray hits.
Also, don’t hide things like that in your post, there’s really no need and it’s pretty annoying.

local Players = game:GetServices("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid", 3)
if not Humanoid then script:Destroy() end

local function OnFloorMaterialChanged()
	if Humanoid.FloorMaterial == Enum.Material.Water then
		print("Hello world!")
	end
end

Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(OnFloorMaterialChanged)
local Players = game:GetServices("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid", 3)
if not Humanoid then script:Destroy() end

local function OnHumanoidSwimming(Speed)
	print(Speed)
end

Humanoid.Swimming:Connect(OnHumanoidSwimming)
3 Likes

Thanks for the suggestion, and may I ask, why do you think it’s annoying? It’s a side note that’s not important to the topic.

Could you give some explanation about what this does? I like to learn about these things for future projects.

The first example uses the Humanoid instance property FloorMaterial (Enum). This detects on what ground the humanoid is stepping on. The script detects when the Floor Material changes and checks if the material is water (usually its when the player is inside water)

The second example uses the humanoid event Swimming (RobloxEvent). This event fires when the player is swimming and also provides the speed of the humanoid (how fast he is swimming)

Will the first one work if only the legs are in the terrain water?

Actually i tested it and it does not detect water, You should use the second example.

Well, the water in my game is not always deep enough to swim in. Does it require the humanoid to be swimming.

If the water is shallow then probably forget about the examples. if you want it to work with non humanoids you can probably just raycast

They are humanoids, but most water pools won’t go above the legs. I still want the different sound for walking through the water though.

Sorry, it’s just that you put “Side Note:” and then your note with no explanation of what was being hidden. I’m guessing most people would click it to find out anyway.

I’ve just been seeing people using blurred text more and more in the forums and most times it doesn’t need blurring. Maybe it’s just my curious nature…

And raycasting from the players rootpart would allow them to walk up to their chest and still detect the water first, not the material underneath the water.
You can put a blacklist for the ray to not read the player.

I use this in a riding lawnmower model (no humanoid of course) to detect what you are driving over and change a ParticleEmitter to ‘blow’ dust, grass, snow, sparks, or spray for water. It works well for shallow water.

Since I have no idea what raycasting is, do you think this could also be done with a detection box? Like, if any part of the player is within a part, it changes the noise.

Intro to Raycasting showed me how to figure out my first raycasting experiment with the lawnmower. It took some trial and error, as well as some searches here to get it finalized, but it works perfectly for detecting whatever is below the ray.
As always, check the developer.roblox.com site, or the newer create.roblox.com site for information about how to create things in Studio.
Here’s where you can see the lawnmower in action: Tracked vehicle, suspension and racing tests. - Roblox

Any answer? Still stuck in it. Have the OP found any method?

My posts have a valid answer that uses raycasting.
OP may not have marked it as the solution though.

If what you’re trying to do is solely for paths and not really for when players are fully submerged in water, I suggest the easiest option: dedicating a non-water material to act as a water material.

Like you said here:

Just set the material of the part to something like Enum.Material.Rubber (or some other material you’re not going to use later) and set it to whatever water sound you’d like inside of your toolbox footstep script that you already have.

And make sure the parts are transparent :wink:

Please read all the posts in the thread rather than just the first post.
It’s @playg4meyt who’s asking now, not OP.

Raycasting is easier to use than Touched or GetPartsInPart which you’d need to do for your system.
I’d never used it before and the links I put in previous posts are all I needed to successfully figure it out.

I use a BuoyancySensor to check if they’re touching water. It has a FullySubmerged and a TouchingSurface properties that tell if the basepart it’s under is touching water, or if it’s completely underwater. Works pretty well

documentation: BuoyancySensor | Documentation - Roblox Creator Hub

1 Like

I’d never seen that before. Pretty cool!

In my case I wanted to change whether a lawn tractor was over certain materials to change the ParticleEmitters used to emit for dirt/water/rocks etc.

1 Like