In the future, please do include as much information as possible in your post, and be sure to atleast format your LUA code.
I do not know your script's context, but from what I see, you're using LocalPlayer in a script, which cannot be done.
Now, lets start from the beginning:
local Spawns = game.Workspace.Spawns:GetChildren()
local RandomSpawn = Spawns[math.random(1, #Spawns)]
Those two lines are perfect, good job!
Now, from the title of your thread, you've led me to believe that you want all players to be teleported to a random spawn. We can accomplish this by creating a *Players *table. After that, we just need to iterate through the our "Players" table, and teleport them.
local Spawns = game.Workspace.Spawns:GetChildren()
local players = game:GetService("Players"):GetPlayers()
for index, player in pairs(players) do
local RandomSpawn = Spawns[math.random(1, #Spawns)]
local isPlaying = player:FindFirstChild("Playing")
if isPlaying and isPlaying.Value == true then
if(player.Character and player.Character.Humanoid and player.Character.Humanoid.Health >0 ) then
player.Character:MoveTo(RandomSpawn.Position)
end
end
end
FilteringEnabled on? Problem solver: Remote events/Functions
- Insert a RemoteEvent in ReplicatedStorage, or anywheres which can be accessed both by the Client and the Server.
- We'll name this RemoteEvent teleportation event
Now, we need to make this event fire each time we want to teleport a player to a certain location!
Since we'll be using this in a remoteEvent, we need to simply use the :FireClient() method, and not the :FireAllClients() method. (Although we could do it both ways if we did this differently...! )
This will be the ServerSideScript
local Spawns = game.Workspace.Spawns:GetChildren()
local players = game:GetService("Players"):GetPlayers()
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild ("RemoteEvent")
for index, player in pairs(players) do
local RandomSpawn = Spawns[math.random(1, #Spawns)]
remoteEvent:FireClient(player, RandomSpawn)
end
This will be a localscript, located in the StarterPack
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild ("RemoteEvent")
function functionName(...)
local Tuple = {...}
local player = Tuple[1]
local randomspawn = Tuple[2]
local isPlaying = player:FindFirstChild("Playing")
if isPlaying and isPlaying.Value == true then
if(player.Character and player.Character.Humanoid and
player.Character.Humanoid.Health >0 ) then
player.Character:MoveTo(randomspawn.Position)
end
end
end
remoteEvent.OnClientEvent:connect(functionName) -This will fire when we fired it from the server-side script. We use OnClientEvent, since we pushed a client method.
If you encounter any errors with the Script, be sure to let me know so I can fix them. I know I didn't really explain the RemoteEvent things very good, but I'm short on time, so...
3 comments
Sorry if it is hard to read
What do the errors say
Can you include a lot more information like is this a local script what the error is and what are the children of spawns?