Sounds on server or FireClient()

  1. What do you want to achieve? Basically I have a sword. Each time you click the mouse button a swing sound plays. everybody nearby hears the sound.

  2. What is the issue? So i want to make the game as much optimised as possible. My question is, Does playing sounds from FireClient() instead of playing them in server reduce lag?

  3. What solutions have you tried so far? Have been looking through some posts, none of them made it clear enough for me.

Here is my code currently

Sword local script

replicatedstorage.SoundEvent:FireServer(handle.Swing)

SoundEvent script (Without FireClient)

replicatedstorage.SoundEvent.OnServerEvent:Connect(function(plr,sound)
	local random = math.random(8,12)
	sound.PlaybackSpeed = random/10
	sound:Play()
end)

And soundevent WITH fireclient would probably look like this:

replicatedstorage.SoundEvent.OnServerEvent:Connect(function(plr,sound)
	local char = workspace:WaitForChild(plr.Name)
	
	for i,player in pairs(players:GetChildren()) do
		if workspace:FindFirstChild(player.Name) then
			if (workspace[player.Name].HumanoidRootPart.Position - char.HumanoidRootPart.Position).Magnitude < 50 then
				replicatedstorage.SoundEvent:FireClient(player,sound)
			end
		end
	end
end)

Now which version is less laggy?

First of all, don’t pass the sound from client to server, it will let exploiters to play any sound on your server.

-- Not secure
replicatedstorage.SoundEvent.OnServerEvent:Connect(function(plr,sound)
	local char = workspace:WaitForChild(plr.Name)
	
	for i,player in pairs(players:GetChildren()) do
		if workspace:FindFirstChild(player.Name) then
			if (workspace[player.Name].HumanoidRootPart.Position - char.HumanoidRootPart.Position).Magnitude < 50 then
				replicatedstorage.SoundEvent:FireClient(player,sound)
			end
		end
	end
end)

--------------

-- Not secure
replicatedstorage.SoundEvent.OnServerEvent:Connect(function(plr,sound)
	local random = math.random(8,12)
	sound.PlaybackSpeed = random/10
	sound:Play()
end)

In your fire client example, you can skip the magnitude check, and just signal all clients, and do the check on their client already.

So like

-- Server
replicatedstorage.SoundEvent.OnServerEvent:Connect(function(plr)
	if (plr.Character.PrimaryPart and plr.Character:FindFirstChildOfClass("Humanoid")) and plr.Humanoid.Health > 0 then
		replicatedstorage.SoundEvent:FireAllClients("somesoundarg", plr.Character.PrimaryPart.Position)
	end
end)
-- Client
replicatedstorage.SoundEvent.OnClientEvent:Connect(function(sound, soundCallerPos)
	if (game.Players.LocalPlayer.Character.PrimaryPart.Position-soundCallerPos).Magnitude < 50 then
		cache[sound]:Play() -- sound calling stuff, idk how your things work so just an example
	end
end)

Hope this helps.

  • ElectrizedAdam
1 Like

Is using FireAllClients better for performance tho? Or should i just play the sounds on server?

It’s always better to give server less work, and leave it’s resources for calculations.
So yeah, pretty much it is.

1 Like

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