Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I import sound to specific players by using RemoteEvents?

Asked by 4 years ago
Edited 4 years ago

I am trying to give a specific player a sound to play with RemoteEvents, but I don't really understand how to import the sound from the server script to the local script. This is what I have so far:

--LocalScript
local RepStorage = game.ReplicatedStorage:WaitForChild("RemoteEvent")

RepStorage.OnClientEvent:Connect(function(args)
    game.Workspace.ChaseTheme:Play()
end)


--ServerScript
local ReplicatedStorage = game.ReplicatedStorage:WaitForChild("RemoteEvent")

ReplicatedStorage:FireClient()


--Loop Players (LocalScript)
for _, r in pairs(game.Teams.Pearl:GetPlayers()) do

local RepStorage = game.ReplicatedStorage:WaitForChild("RemoteEvent")
    RepStorage.OnClientEvent:Connect(function()
        game.Workspace.ChaseTheme:Play()
    end)
end
0
What do you mean loop of players programmerHere 371 — 4y
0
I edited my script. You can take a look there. User#27966 0 — 4y
0
Then you would do FireClient(r). But, if you're doing it for every player, just use FireAllClients(). programmerHere 371 — 4y
0
Ok. Thank you! User#27966 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

First let's fix the misleading identifiers. Your variables are pointing to a remote event not the replicated storage.

Second you do not need the args parameter if you won't use it.

What the issue is, you need a player to fire to, or else how will roblox know who to work with? In this example I will get the player through PlayerAdded.

--LocalScript
local remote_event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

remote_event.OnClientEvent:Connect(function()
    game.Workspace.ChaseTheme:Play()
end)




--ServerScript
local remote_event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game:GetService("Players").PlayerAdded:Connect(function(client)
    remote_event:FireClient(client) -- pass a player reference
end)
0
What if it's in a loop of players? How would I approach to that? User#27966 0 — 4y
Ad

Answer this question