Custom synced footsteps cause's network lag after a while

I made a synced footsteps thing for my game and I am trying to optimize it but its causing players to lag on the server side and I am trying to fix it

Code for server side:

PlayFootStepSound.OnServerEvent:Connect(function(Player,Sound1: Sound,Sound2: Sound,Value: string,Value2: string)
	
	if not Player:FindFirstChild("IsRunning") then
		if Value2 == "1" then
			Sound1.Volume = 0.5
			if Sound1.IsPlaying == true then
				Sound1:Stop()
			end
			Sound1:Play()
		elseif Value2 == "2" then
			Sound2.Volume = 0.5
			if Sound2.IsPlaying == true then
				Sound2:Stop()
			end
			Sound2:Play()
		end
	elseif Player:FindFirstChild("IsRunning") then
		if Value2 == "1" then
			Sound1.Volume = 3
			if Sound1.IsPlaying == true then
				Sound1:Stop()
			end
			Sound1:Play()
		elseif Value2 == "2" then
			Sound2.Volume = 3
			if Sound2.IsPlaying == true then
				Sound2:Stop()
			end
			Sound2:Play()
		end
	end
	
end)

Code for Client-Side:

function module.HandleFootStepSoundClient(Player: Player)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid: Humanoid = Char:WaitForChild("Humanoid")
	local Animator: Animator = Humanoid:WaitForChild("Animator")
	local FootStepSound1: Sound = Char["Left Leg"]:WaitForChild("FootStep1")
	local FootStepSound2: Sound = Char["Right Leg"]:WaitForChild("FootStep2")
	local Cooldown = false
	local SoundIsPlaying = false
	local PlayFootStepSound = game.ReplicatedStorage.GameEvents.PlayerEvents.FootStepSoundEvents:WaitForChild("PlayFootStepSound")

	if Char:FindFirstChild("HumanoidRootPart"):FindFirstChild("Running") then
		local Sound: Sound = Char.HumanoidRootPart:FindFirstChild("Running")
		if Sound then
			Sound.Volume = 0
		end
	end
	
	Humanoid.Running:Connect(function(speed)	
		if speed > 0.1 then
			if Humanoid:GetState() == Enum.HumanoidStateType.Running then
				for i, v: AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
					local function PlaySound(Value: string,Value2: string)
						if Value == "Walking" then
							if not Player:FindFirstChild("IsRunning") then
								PlayFootStepSound:FireServer(FootStepSound1,FootStepSound2,Value,Value2)
							end
						end	
						if Value == "Running" then
							if Player:FindFirstChild("IsRunning") then
								PlayFootStepSound:FireServer(FootStepSound1,FootStepSound2,Value,Value2)
							end
						end
					end
					if v.Name == "WalkAnim" then
						v:GetMarkerReachedSignal("Walk 1"):Connect(function()
							PlaySound("Walking","1")
						end)
						v:GetMarkerReachedSignal("Walk 2"):Connect(function()
							PlaySound("Walking","2")
						end)
					end
					if v.Name == "RunAnimation" then
						v:GetMarkerReachedSignal("Run 1"):Connect(function()
							PlaySound("Running","1")
						end)
						v:GetMarkerReachedSignal("Run 2"):Connect(function()
							PlaySound("Running","2")
						end)
					end
				end
			end
		else
			-- Idle
		end
	end)
end

The root of your issue is most likely in this function

Humanoid.Running:Connect(function(speed)	
		if speed > 0.1 then
			if Humanoid:GetState() == Enum.HumanoidStateType.Running then
				for i, v: AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
					local function PlaySound(Value: string,Value2: string)
						if Value == "Walking" then
							if not Player:FindFirstChild("IsRunning") then
								PlayFootStepSound:FireServer(FootStepSound1,FootStepSound2,Value,Value2)
							end
						end	
						if Value == "Running" then
							if Player:FindFirstChild("IsRunning") then
								PlayFootStepSound:FireServer(FootStepSound1,FootStepSound2,Value,Value2)
							end
						end
					end
					if v.Name == "WalkAnim" then
						v:GetMarkerReachedSignal("Walk 1"):Connect(function()
							PlaySound("Walking","1")
						end)
						v:GetMarkerReachedSignal("Walk 2"):Connect(function()
							PlaySound("Walking","2")
						end)
					end
					if v.Name == "RunAnimation" then
						v:GetMarkerReachedSignal("Run 1"):Connect(function()
							PlaySound("Running","1")
						end)
						v:GetMarkerReachedSignal("Run 2"):Connect(function()
							PlaySound("Running","2")
						end)
					end
				end
			end
		else
			-- Idle
		end

I see that you are connecting events to the PlaySound function, however you do so every single time the Humanoid.Running event fires, without ever disconnecting the previous ones. So, as a fix, either keep track of and disconnect all the :GetMarkerReachedSignal connections upon a new Humanoid.Running event, or you can go for something more dynamic that’d check if the connection you wanna make is already in place, in which case you don’t re-connect.

Also, depending on how exactly you plan on this working, you could also just call the :GetMarkerReachedSignal once for every event in every walking/running animation when loading them in instead of doing so in the Humanoid.Running event.

1 Like

thank you for the help! I added some disconnections like you suggested and it worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.