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
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)
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)
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
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.
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