Problems with trying to stop sound when player stops moving

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)
1 Like

use a loop and just don’t use :Play(), just use .Playing = true

1 Like

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)
1 Like

you could also change the walk sound from the character

1 Like

I can? I never knew that! How would I do that?

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

The problem with that is, about different FloorMaterials. I would check this out, and change the sounds out with the sounds of the characters head.

1 Like

Do you know if they replicate to the server? I’m changing them on the client. Should I change them on the server?

1 Like

yes, else other players will hear the same footstep sound

1 Like

Should I connect it with a PlayerAdded event or what should I do?

1 Like

add a remote event

make a new scripts in serverstorage:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, sound)
script.Parent.[The FootStepSoundNameHere].SoundId = sound
end)

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Wait()
local char = plr.Character
local script = game.ServerStorage.Script:Clone()
script.Parent = char.PrimaryPart
end)

fire the event from your client script with this arguments: ([Material SoundId Here As A String])

1 Like

The sounds don’t replicate on the server in HumanoidRootPart.

1 Like

wdym? extra words sddfsgdfhdfh

Looks like I don’t need to replicate them, Sound script changes - #55 by colbert2677

I have a system that only changes sounds locally and I’m the only one who can hear them

1 Like

They deleted the server “Sounds” folder, so I guess people can’t hear them.

“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.”

1 Like