I have a party system script that teleports players in a table to a reserved server.
I also want this script to show a GUI and play a sound for those players.
The teleporting only works for the players in the table, but the GUI and sound show and play for every player on the server.
Here is the part of the script that has this problem:
local playersToTP = {}
for i, playerInParty in pairs(game.ReplicatedStorage.Parties[value1].Players:GetChildren()) do
table.insert(playersToTP, game.Players[playerInParty.Value])
end
for _, d in pairs(game.Players:GetPlayers(playersToTP)) do
d.Character.Humanoid.WalkSpeed = 0
d.Character.Humanoid.JumpPower = 0
--Fires for everyone
game.ReplicatedStorage.LoadingSound:FireClient(d)
game.ReplicatedStorage.TpEvent:FireClient(d)
-- ^
end
local tpOptions = Instance.new("TeleportOptions")
tpOptions.ShouldReserveServer = true
tps:TeleportAsync(placeId, playersToTP, tpOptions)
The reason everyone is hearing the sound is because you are looping through a table which was created via :GetPlayers() which returns all the players currently in the game
To stop that instead loop through the playersToTP table so you would change this
for _, d in pairs(game.Players:GetPlayers(playersToTP)) do
d.Character.Humanoid.WalkSpeed = 0
d.Character.Humanoid.JumpPower = 0
--Fires for everyone
game.ReplicatedStorage.LoadingSound:FireClient(d)
game.ReplicatedStorage.TpEvent:FireClient(d)
-- ^
end
You would do
for _, d in pairs(playersToTp) do
d.Character.Humanoid.WalkSpeed = 0
d.Character.Humanoid.JumpPower = 0
--Fires for everyone
game.ReplicatedStorage.LoadingSound:FireClient(d)
game.ReplicatedStorage.TpEvent:FireClient(d)
-- ^
end