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.
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?
What solutions have you tried so far? Have been looking through some posts, none of them made it clear enough for me.
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)
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)