so i have a local script that play a gun fire sound when the player shoots a gun, but how do i play it to other clients as well? the only thing that comes to my mind is to fire a remote that fires another remote that plays the gun fire for other clients, but i feel like thats not the most optimal method
1 Like
playing the gunfire on the server is a better idea.
there would be a delay between the sound and the animation tho
It likely would not. Basically, you would put a script in ServerScriptService and in there, you can add a sound instance, parent it to the character torso, then play the sound when the gun fires.
Does the gun first fire the effects locally (Light, particles, etc.) and then sends an event to the server, which sends it to all players?
If so, whenever the player fires locally, play the sound locally.
When the player gets an event sent from the server, play the sound but parented to the player that fired.
Exmaple:
-- Client
GunFire.Something:Connect(function()
GunshotSound:Play()
ShootEvent:FireServer()
end)
ShootEvent.OnClientEvent:Connect(function(OtherPlayer)
local Character = OtherPlayer.Character -- Check if player has character
if Character then
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local RootPart = Character:FindFirstChild("HumanoidRootPart")
if Humanoid and Humanoid.Health > 0 and RootPart then -- Check if player is alive and has a root part
local Sound = GunshotSound:Clone() -- Clone the sound to the player's root part and play it from there
Sound.Parent = RootPart
Sound:Play()
Sound.Ended:Wait()
Sound:Destroy()
end
end
end)
-- Server
ShootEvent.OnServerEvent:Connect(function(Player)
for i,OtherPlayer in pairs(game:GetService("Players"):GetPlayers()) do
if OtherPlayer ~= Player then
ShootEvent:FireClient(OtherPlayer)
end
end
end)