How would I randomly select a part from the workspace?

Hey, I’m trying to set the players spawn location when they first join, but I have 8 spawns I want them to spawn on one of them randomly, but I don’t know how to do this.
I have wrote a script to set their spawn but it only works with 1 spawn location, I cant make is so that there is a random chance of them spawning at any one of the eight spawn locations.
Here is my script:

game.Players.PlayerAdded:Connect(function(player)
	player.RespawnLocation = game.Workspace.SpawnLocation1
	print("Set respawn location")
end)
2 Likes

maybe try using math.random and that should work

1 Like

Put all the spawn locations into a folder then put this in a server script

local SpawnLocs = game.Workspace.SpawnLocs:GetChildren()
game.Players.PlayerAdded:Connect(function(v1)
 v1.RespawnLocation = SpawnLocs[math.random(1, #SpawnLocs)]
end)
1 Like

Hi thanks for the response, I just tested it and its just spawning me on the same one over and over… any ideas how I can fix this?

1 Like

I think that if you have more then 1 spawn location in your game the player will already spawn on one at random.

4 Likes

then try this

local SpawnLocs = game.Workspace.SpawnLocs:GetChildren()
local function f1(v1)
 v1.RespawnLocation = SpawnLocs[math.random(1, #SpawnLocs)]
end
game.Players.PlayerAdded:Connect(function(v1)
 f1(v1)
 v1.Character.Humanoid.Died:Connect(function()
  f1(v1)
 end)
end)
2 Likes

Yes but I have other spawn locations that I dont want the players to spawn on from just joining the game.

1 Like

Still no luck unfortunately, I keep spawning on the same one

1 Like

Did you check if the spawns are being deleted?

1 Like

How do I do this? And why would they be getting deleted?

1 Like

Workspace

1 Like

Do you mean I run the game and check if they are being deleted from workspace?

2 Likes

Try, Store an object spawn in a table by assigning the keyvalue pair the key with a number, and using math.random to randomly number the key, you’ll get a random spawn object.
like

table[1] = spawn1
table[2] = spawn2

table[8] = spawn8

num = math.random(1,8)
SpawnOfPlayer = table[num]

1 Like

Do you get any errors ?
And instead of that you can try this.
Like remove all the respawn points and put a part in place of them, then put this script in the server script

local SpawnLocs = game.Workspace.SpawnLocs:GetChildren()
local function f1(v1)
 v1.Character.HumanoidRootPart.CFrame = SpawnLocs[math.random(1, #SpawnLocs)].CFrame
end
game.Players.PlayerAdded:Connect(function(v1)
 f1(v1)
 v1.Character.Humanoid.Died:Connect(function()
  f1(v1)
 end)
end)
2 Likes

Maybe try using team spawns?

Char limit :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.