Random spawns not being random?

function teleportPlayer(player)
	if player then
		local character = player.Character
		if character then
			local selectedSpawn = getRandomSpawn(player.Team)
        end
    end
end

function getRandomSpawn(team)
	if gameMap then
		local spawns = {}
		if team == teams.Red then
			spawns = gameMap.Spawns.Taggers:GetChildren()
		else
			spawns = gameMap.Spawns.Runners:GetChildren()
		end
		local spawn = spawns[math.random(1, #spawns)]
		return spawn
	end
	return nil
end

function organizeTeams()
	    for _, player in pairs(game.Players:GetPlayers()) do
		    if player then
			    teleportPlayer(player)
        end
		end
	end

The problem im having is it is sending every player on red team to one spawn, and every player on the other team to one spawn. This means there’s round 12 players spawning at the same spot, causing players to going flying around, etc. I have 12 spawns in the ‘Runners’ spawns, but it only picks 1 out of those 12 and gives it to all the players as their spawn point

1 Like

Do these two things:

  1. Add print("There are this many spawns to choose from "..#spawns) just before local spawn = spawns[math.random(1, #spawns)]

  2. At the top of the script put “math.randomseed(os.time())

Once you’ve done these, reply with what the first step prints. Your spawning may be fixed by the second step.

I prefer the more complicated randomseed method of math.randomseed((tick()/time())*math.random())
but your method works fine if the respawn events don’t occur to closely to each other.

I do that too but thought people might think I’m weird if I put that here.

Nope, weirdness dosen’t exists when sharing scripts. Saying how ugly code looks or how inefficient it is exists, but weirdness dosen’t because there are usually like 30 ways to do the same thing.

[There are this many spawns to choose from 12]
thats what printed

also just a nitpick you dont need
local spawns = {}
you can just use
local spawns
bc ur gonna overwrite it anyways

Also have you tried making sure the spawns arent in the same place?

They are all spread amongst the map

function getRandomSpawn(team)
	if gameMap then
		local spawns
		spawns = gameMap.Spawns:GetChildren()
		local spawn = spawns[math.random(1, #spawns)]
		return spawn
	end
	return nil
end

I just ungrouped all the spawns, so theres 16 spawn points now, but now all 16 players get tped to the same one spawn.

What is the team argument being sent to that function?

I’m going to assume you added the math.randomseed().

Do these:

  1. Please put “print(spawn.Position)” underneath local spawn = spawns[math.random(1, #spawns)] and tell me what is printed.

  2. Show the function where you actually teleport the player to the selected spawn.

Oh I can probably get rid of it now that im not checking for teams

How are you using what that function returns after it returns it?

local selectedSpawn = getRandomSpawn(player.Team)
		local humanoidRootPart = character:FindFirstChild('HumanoidRootPart')
		if humanoidRootPart then
			humanoidRootPart.CFrame = selectedSpawn.CFrame + Vector3.new(0, 2, 0)

end

It prints the same coordinates with a x2 (just testing in studio with 2 players so yea)

But each round it’s a different cframe, just the same for both players

You could try removing that spawn from the list of possible spawns after it’s used.

Hi, please do this:

function teleportPlayer(player)
	if player then
		local character = player.Character
		if character then
			local selectedSpawn = getRandomSpawn(player.Team)
			local humanoidRootPart = character:FindFirstChild('HumanoidRootPart')
			if humanoidRootPart then
				humanoidRootPart.CFrame = selectedSpawn.CFrame + Vector3.new(0, 2, 0)
				characterSetup(player)
				local teamGui = teamGui:Clone()
				teamGui.Parent = player.Character.Head
				teamGui.Frame.BackgroundColor3 = player.TeamColor.Color
			end
		else
			player:LoadCharacter()
		end
	end
end

In this case you might want to consider using the new random number generation API, it should provide more random, random numbers.

Otherwise, provided there are more spawns than players spawning on them, iterate through the players, and spawn each one on the corresponding spawn number.

1 Like