I’m trying to create footsteps in Roblox, but I’m having a problem with trying to stop it if the player is not moving and start it whenever they are moving and over again, etc. I don’t want to loop it, bc it will mess up the sound. Could somebody help me? I don’t mind if you rewrite this code, or change it a bit.
local FootStep = require(game.ReplicatedStorage.FootstepModule)
local LocalPlayer = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")
local character = script.Parent
local humanoid = character.Humanoid
local sound = nil
local function changeSongId(soundId)
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Parent = script
sound = sound
end
local MaterialTbl = FootStep:GetTableFromMaterial(humanoid.FloorMaterial)
if MaterialTbl then
local RandomSound = FootStep:GetRandomSound(MaterialTbl)
changeSongId(RandomSound)
end
humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
local LastFloorMaterial = script:WaitForChild("LastMaterial").Value
local MaterialTbl = FootStep:GetTableFromMaterial(humanoid.FloorMaterial)
if MaterialTbl then
local RandomSound = FootStep:GetRandomSound(MaterialTbl)
changeSongId(RandomSound)
end
script:WaitForChild("LastMaterial").Value = tostring(humanoid.FloorMaterial)
end)
local function playSound(sound)
sound:Play()
sound.Ended:Wait()
playSound(sound)
end
script:WaitForChild("Walking").Changed:Connect(function(newValue)
print(newValue)
if newValue == false then
sound:Stop()
elseif newValue == true then
sound:Play()
sound.Ended:Wait()
playSound(sound)
end
end)
task.spawn(function()
while task.wait() do
if humanoid.MoveDirection.Magnitude > 0 then
if sound then
playSound(sound)
script:WaitForChild("Walking").Value = true
end
else
script:WaitForChild("Walking").Value = false
end
end
end)
That works, but how would I do it so the sound matches with the footsteps of the player?
local FootStep = require(game.ReplicatedStorage.FootstepModule)
local LocalPlayer = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")
local character = script.Parent
local humanoid = character.Humanoid
local sound = script:WaitForChild("Sound")
local function changeSongId(soundId)
local sound = script:WaitForChild("Sound")
sound.SoundId = soundId
print("something")
end
local MaterialTbl = FootStep:GetTableFromMaterial(humanoid.FloorMaterial)
if MaterialTbl then
local RandomSound = FootStep:GetRandomSound(MaterialTbl)
changeSongId(RandomSound)
end
humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
local LastFloorMaterial = script:WaitForChild("LastMaterial").Value
local MaterialTbl = FootStep:GetTableFromMaterial(humanoid.FloorMaterial)
if MaterialTbl then
local RandomSound = FootStep:GetRandomSound(MaterialTbl)
changeSongId(RandomSound)
end
script:WaitForChild("LastMaterial").Value = tostring(humanoid.FloorMaterial)
end)
local function playSound(sound)
task.spawn(function()
sound.Playing = true
sound.Ended:Wait()
playSound(sound)
end)
end
script:WaitForChild("Walking").Changed:Connect(function(newValue)
print(newValue)
if newValue == false then
sound.Playing = false
elseif newValue == true then
sound.Playing = true
sound.Ended:Wait()
playSound(sound)
end
end)
task.spawn(function()
while task.wait() do
if humanoid.MoveDirection.Magnitude > 0 then
if sound then
playSound(sound)
script:WaitForChild("Walking").Value = true
end
else
script:WaitForChild("Walking").Value = false
end
end
end)
Your character has all of it’s sounds stored inside the HumanoidRootPart and Head, I don’t remember exactly the sound name but you could check it by playing inside studio and checking inside your HumanoidRootPart
“Your concern has already been addressed in the OP. The only change is that the client is responsible for sound creation and playback for other characters on their end based on Humanoid states.”