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