Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I teleport players to random spawn on a map?

I tried :

Spawns = game.Workspace.Spawns:GetChildren()
	 RandomSpawn = Spawns[math.random(1, #Spawns)]
	 if game.Players.LocalPlayer.Playing.Value == true then
		 plrs.Character:MoveTo(RandomSpawn.Position)
	 end

But in game line 18 is an error then in roblox studio line 19 is an error

What am I doing wrong? How can I fix this?

3 comments

0

Sorry if it is hard to read

0

What do the errors say

0

Can you include a lot more information like is this a local script what the error is and what are the children of spawns?

3 answers

✓ Accepted answer

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() --GetPlayers returns a table with a list of all the players.
for index, player in pairs(players) do
	--We use forloops to iterate through tables.
	--You can read more about them here: http://wiki.roblox.com/index.php?title=Loops#For

	local RandomSpawn = Spawns[math.random(1, #Spawns)]  --Create a new randomspawn area for each player. (The loop will run for #players)
	local isPlaying = player:FindFirstChild("Playing") --Variable for your value in the player
	if isPlaying and isPlaying.Value == true then  -- Check if its in the player and that the value is true
		if(player.Character and player.Character.Humanoid and player.Character.Humanoid.Health >0 ) then --Check if the player is not dead
		player.Character:MoveTo(RandomSpawn.Position) 
		end
	end
end

FilteringEnabled on? Problem solver: Remote events/Functions

  1. Insert a RemoteEvent in ReplicatedStorage, or anywheres which can be accessed both by the Client and the Server.
  2. 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 = {...} --The arguments here are in form of a tuple(This is just a table with all your paramaters)
	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 --Check if the player is not dead
		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...

7 comments

0

Thanks but there is one problem... When I try your solution in Roblox Studio, it works. But It does not work in the game itself when I push "play". I appreciate your answer. What does not work in your solution when I push the "Play" button is it is not teleporting me to the Spawns. My game is Filtering Enabled and I would appreciate it if you could help me TP the players, In the Console No errors

0

@lm345 That issue can be resolved by using remote events. I can definitely do this for you, later when I will have my computer

0

Thanks @Thetacah, I do not know how to do remote events yet... Thats my next goal to learn...

0

when you said remoteEvent:OnClientEvent:connect(functionName) do you mean remoteEvent.OnClientEvent:connect(functionName) ?

0

I ask cause I get an error on the OnClientEvent:connect

0

Ah, yes!

0

ok, thanks

step 1: group all your spawns together and name it "Spawns" (make sure "Spawns" is in the workspace)

step 2: put this into your script:

local plrs = {}
	
for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player)
	end
end
		
local Spawns = workspace.Spawns
local AvailableSpawnPoints = Spawns:GetChildren()
	
for i, player in pairs(plrs) do
	if player then
		character = player.Character
			
		if character then
				
			character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
			table.remove(AvailableSpawnPoints,1)
				
			local GameTag = Instance.new("BoolValue")
			GameTag.Name = "GameTag"
			GameTag.Parent = player.Character
		else
				
			if not character then
				table.remove(plrs,i)
			end
		end
	end
end

this should work since I have tested it.

Just put a giant neutral spawn, covering the map. It should work.

1 comment

0

Oh wait, one thing! Turn off CanCollide and make the Transparency "1", this is optional and is only for looks.

This discussion is preserved as it was. New answers, comments, and votes are closed.