My footstep sound script effects every player if one person walks

Okay so here is my footstep sound code (obviously another script adds the sounds to the head when the player loads in)

local plrs = game:GetService("Players")

plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")

		local sounds = char:WaitForChild("Head")

		local humrundb = false
		local walkspeed = 0
		
		local cancel = false
		
		humanoid.Died:Connect(function()
			cancel = true
		end)

		local currentmaterial = nil

		humanoid.Running:Connect(function(speed)
			walkspeed = speed
		end)

		while task.wait() do
			if cancel == true then
				break
			end
			
			if not plr:IsDescendantOf(plrs) then
				break
			end
			
			pcall(function()
				if walkspeed > 7 and plr:WaitForChild("disablesteps").Value <= 0 then
					print(plr.Name.." speed: "..tostring(walkspeed))
					if humanoid.FloorMaterial ~= Enum.Material.Air then
						if currentmaterial ~= nil then
							if currentmaterial ~= humanoid.FloorMaterial then
								
								task.wait(.1)

								currentmaterial = humanoid.FloorMaterial

								for i,v in pairs(sounds:GetChildren()) do
									if v:IsA("Sound") then
										if v.Name ~= tostring(currentmaterial):split(".")[3] then
											v:Stop()
										else
											v.PlaybackSpeed = walkspeed/20
											v:Play()
										end
									end
								end

							elseif currentmaterial == humanoid.FloorMaterial then

								for i,v in pairs(sounds:GetChildren()) do
									if v:IsA("Sound") then
										if v.Name == tostring(currentmaterial):split(".")[3] then
											v.PlaybackSpeed = walkspeed/20
										end
									end
								end

							end

						elseif currentmaterial == nil then

							currentmaterial = humanoid.FloorMaterial

							for i,v in pairs(sounds:GetChildren()) do
								if v:IsA("Sound") then
									if v.Name == tostring(currentmaterial):split(".")[3] then
										v.PlaybackSpeed = walkspeed/20
										v:Play()
									end
								end
							end

						end

					elseif humanoid.FloorMaterial == Enum.Material.Air then

						for i,v in pairs(sounds:GetChildren()) do
							if v:IsA("Sound") then
								currentmaterial = nil
								v:Stop()
							end
						end

					end
				elseif currentmaterial ~= nil then

					for i,v in pairs(sounds:GetChildren()) do
						if v:IsA("Sound") then
							currentmaterial = nil
							v:Stop()
						end
					end

				end
			end)
		end
	end)
end)

Now my problem is when I run a play test.
If player1 starts walking it functions
if player2 starts walking it functions

however when player1 (who loaded before player2) walks the footstep sounds play normally HOWEVER the sounds also play for player2 but it considers his walkspeed very fast even if hes standing still while player1 is moving around. Why is this and how can I fix it its very frustrating and I’ve been struggling with this for a while. I just cant seem to tell whats wrong and whats causing this error.

Ok so I tried doing this print in my running event

humanoid.Running:Connect(function(speed)
			print("event for "..char.Name)
			walkspeed = speed
		end)

And it seems that when Player1 runs it fires player2s run event but this still doesnt explain why this is happening. I just know now its related to the run event.

Can you show the other script?

1 Like

The other script just adds the sounds into the head of the player from serverstorage.

for i,v in pairs(folder.sound:GetChildren()) do
				local clone = v:Clone()
				clone.Parent = char:WaitForChild("Head")
			end

The first script is the full script?

1 Like

Yes thats the whole script for footstep sounds

Try doing a local script in StarterCharacterScripts, and using RemoteEvents.

1 Like

I just did a bit more research on the running event and apparently its not intended to work on the server. I wonder if there are any alternatives to .running I could use to get the players movement speed? I could use remote events and such but what about NPCs in my game that run on the server? I would like for them to make footstep sounds too but that’d be impossible since they are on the server.

1 Like

I could use humanoidrootpart assemblylinearvelocity as an alternative but I am not sure how I would apply that since it works with multiple values instead of just 1.

I would use a local script, because the walkspeed is set in the client and it replicates in the server.

1 Like

But I would still need an alternative regardless for NPCs. Therefore I should probably figure out a way to make it work with assemblylinearvelocity