why is my sound delayed despite using a remote event to run the sound on the client side? when i used this method for running particles on the client there were no problems whatsoever.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local RunService = game:GetService("RunService")
local clientSfx = ReplicatedStorage.RemoteEvents.Server:WaitForChild("ClientSfx")
clientSfx.OnClientEvent:Connect(function(location, SoundName, volume)
local tagged = game:GetService("CollectionService"):GetTagged("Sound")
for i,v in pairs(tagged) do
if v.Name == SoundName then
local clone = v:Clone()
local soundpart = Instance.new("Part")
soundpart.Anchored = true
soundpart.CanCollide = false
soundpart.Transparency = 1
clone.Parent = soundpart
soundpart.Position = location
soundpart.Parent = workspace
clone:Play()
clone.Ended:Connect(function()
soundpart:Destroy()
end)
end
end
end)
There will also be some delay simply due to the fact that information has to travel from the client to the server and vice versa. You cannot remove this delay entirely, although you may see a shorter delay with your approach of using remotes, due to some differences in how Roblox replicates audio and handles remotes.
Another source of delay you may be experiencing is loadtimes. Consider preloading your sound ids when the player joins.
An easy fix for this is to split the sound effects to client and server. Client is supposed to be heard by the client and can be heard instantly, since you play them in a local script, server are supposed to be heard by other players, when they replicate to them. Since it takes time for other players to see the hit itself, the sound should be synced.
Basically the client that hits another player doesn’t need to wait for the hit to be validated in order to hear the corresponding sound, since it’s just a sound effect. If you have designed your game logic in such a way so your client code also has some checks in place, the only situation where a client will hear a sound without a hit will be if they lag a lot or exploit.