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

How could I make a loop that checks .FloorMaterial and changes the footstep sounds?

Hey guys, I'm pretty new to Lua and I'm having some difficulty figuring out how I'm supposed to do this. As the title states, I'm trying to do some kind of loop that checks what the player is standing on and then changes the footstep sounds.

This is my initial code, which is not really working as intended. Once I step on the snow the sound doesn't loop and plays a million times at the same time.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local isWalking = false


player.Character.Sound:Destroy()
for i,object in pairs(player.Character.Head:getChildren()) do
	if object:IsA("Sound") then
		object:Destroy()
	end
end


local footsteps = Instance.new("Sound", character.Head)
footsteps.SoundId = "http://www.roblox.com/asset/?id=619083295"
footsteps.Volume = 1
footsteps.Pitch = 1
footsteps.Looped = true


player.Character.Humanoid.Running:connect(function(speed)
	if speed > 7 then
		isWalking = true
	else
		isWalking = false
	end
end)


while true do
	wait()
	if character.Humanoid.FloorMaterial == Enum.Material.Snow and isWalking then
		footsteps:Play()
	else
		footsteps:Stop()
	end
end

I tried messing around with it and I replaced the last part with this:

while true do
	wait()
	if character.Humanoid.FloorMaterial ~= nil then
		print(character.Humanoid.FloorMaterial.Name)
	end
end

Which does read what I'm standing on but I'm not sure how to apply it to what I'm trying to do.

I also tried this but it's not working either:

while true do
	wait()
	if character.Humanoid.FloorMaterial ~= nil then
		if character.Humanoid.FloorMaterial.Name == "Snow" and isWalking then
			footsteps:Play()
		else
			footsteps:Stop()
		end
	end
end

I'd appreciate any tips you guys could give me. Thank you

Edit: This is what I'm trying to achieve: https://www.youtube.com/watch?v=pbiGbEfiIcQ

1 answer

Try this, hope it helps! And if it does be sure to accept the answer so others with a similar problem will know it's resolved. I left a lot of comments so you will be able to learn, and not just be spoon-fed.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local head = character:WaitForChild('Head') -- incase the player resets while running/to stop an error
local humanoid = character.Humanoid
local isRunning = false

while true do -- do this so each time a player dies and character respawns it will get rid of the sound
	wait(0.01)
	local sound = head:GetChildren()
	for _,v in pairs(sound) do
		if v:IsA("Sound") and v.Name == 'Running' then -- it's unnecessary to destroy all other game sounds, only the stepping one
			v.Looped = false
			v:Stop()
			v:Destroy()
		else 
			return
		end
	end
end

local footsteps = Instance.new("Sound") -- not declaring the parent before the properties is more efficient
footsteps.Name = 'footsteps'
footsteps.SoundId = "rbxassetid://619083295" -- much shorter
footsteps.Volume = 1
footsteps.Pitch = 1
footsteps.Looped = true
footsteps.Parent = head

humanoid.Runnning:Connect(function(WalkSpeed)
	if WalkSpeed  > 7 then
		isRunning = true
	else
		isRunning = false -- make another variable and name it isWalking and do isWalking = true after this line, follow it with another if statement like: if WalkSpeed >0 and WalkSpeed <7 then isWalking = true
	end
end)

while true do
	wait(0.01) -- quicker than the default wait()
	if humanoid.FloorMaterial == "Snow" and isRunning == true and footsteps.IsPlaying == false then -- to stop spamming
		footsteps:Play()
	else
		footsteps:Stop()
	end
end

2 comments

0

Doesn't work, but I'll take your stuff and try to mess around with it until it works. One thing I'd mention is on line 39, I've read that Enum.Material.Snow would work better

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