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

How can I make players spawn to different spawnlocations?

How can I make this so all players will be splited and teleported to different spawnpoints?

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
                if plr.Character then
                    plr.Character.HumanoidRootPart.CFrame = spawnpoint.CFrame or spawnpoint2.CFrame or spawnpoint3.CFrame
                    print("All players have been teleported to: "..spawnpoint.Name)

1 comment

0

Use math.random to randomly select a spawnpoint.

2 answers

Explanation

You seemed to have misunderstood how or works; I'll explain it for you real quick.

In your situation or means:

if spawnpoint1 doesn't exist then choose spawnpoint 2. If spawnpoint2 doesn't exist then choose spawnpoint3.

But I'll assume these 3 spawnpoints will always exist in your game throughout the whole game. Thus using or will not work here.

Fix

Math.random fixes this problem and randomly selects a spawnpoint, and then teleports the player to it.

Implementation

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
	if player.Character then
		local character = player.Character

		local index = math.random(1, #workspace.spawnpoints:GetChildren())
		local spawnPoint = workspace.spawnpoints:GetChildren()[index]

		character.HumanoidRootPart.CFrame = spawnPoint.CFrame
		print("All players have been teleported to: "..spawnpoint.Name)
	end
end

1 comment

0

Hey, I have send a question but I didn't reply to this because I can't type lua code in here. Hope you get a notification now :)

Alright, Thank you for you're help! I suppose that the "spawnpoints" is a folder wich the GetChildren all the things inside gets? However, I get a Infinite yield possible on 'Workspace.Obby:WaitForChild("Spawn")'.

I have changed the location a little bit to where the spawns are located, But it does not work?

The spawns are located at: game.Workspace.Obby.Spawns

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
	    if player.Character then
	        local character = player.Character
	
	        local index = math.random(1, #workspace.Obby.Spawns:GetChildren())
	        local spawnPoint = workspace.Obby.Spawns:GetChildren()[index]
	
	        character.HumanoidRootPart.CFrame = spawnPoint.CFrame
	        print("All players have been teleported to: "..spawnPoint.Name)
	    end
	end

Can you help me again with this?

Thanks in advice.

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