I’m trying to play a sound whenever x happens from server-side, however to my tiny knowledge I had no idea that playing sounds from a server will literally play it everywhere, regardless whether the sound is parented to X part or not.
I tried going around this via :FireAllClients(), but I don’t think that did the trick, or maybe i just did it incorrectly.
That’s not how remote events work. A remote event is a way to send information between the client and the server. It is not a way to directly pass a function that will execute. If that’s the only thing you want, you need to add a listener for that event.
I don’t understand what “only from server side” means, so I will give you an example where it picks a random player in the server and plays the sound for him. You can read more about remote events the client-server communication on the engine documentation.
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:FindFirstChild("RemoteEvent")
task.wait(5)
print("Getting players in server.")
local players = Players:GetPlayers()
if #players > 0 then
local randomPlayer = players[math.random(1, #players)]
local audioId = "rbxassetid://1844271848" -- Replace with your actual audio ID
remoteEvent:FireClient(randomPlayer, audioId, game.Workspace)
end
Client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:FindFirstChild("RemoteEvent")
remoteEvent.OnClientEvent:Connect(function(audioId, parent)
local sound = Instance.new("Sound")
sound.SoundId = audioId
sound.Parent = parent
sound:Play()
sound.Ended:Connect(function()
sound:Destroy()
end)
end)
The following is only an example. Feel free to pass anything else you might need such as volume, equalizer effects, etc.
Also like @anon81993163 said, you are not very clear with your question. Playing sounds via game:GetService("SoundService"):PlayLocalSound() will make them play globally for the client that called the function. Parenting a sound to workspace has similar behaviour, it will be heard anywhere. If you need the sound to play from a certain position or point, you need to parent it to a part or attachment. More on that here.
Here is a video of how that code I showed you works, you can hear the sound on the client, but not on the server (or other clients, but watch off for respect filtering enabled.)
I’m aware of remote events, I just thought :FireAllClients() wouldn’t require a clientside communication.
I’m dumb, not stupid.
Even if, where would I put the client-side code? → (besides a local script) ← by that I mean where do i put the localscript, not what script I put the client code in
Just use RemoteEvent:FireClient() per player that is within range, this should sync the hearing between all players.
Or you want to give it to the client which makes it not in sync, but you will check the distance and use the said method above, passing the sound as a parameter.
there is sound propertied on the sound object: RolloffmaxDistance, which is the maximum distance any player can hear the sound, simply set that property to ur liking and play the sound from server