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:

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)

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