Randomly Spawning Players

Hello.

  1. What do you want to achieve? Create a system that spawns everyone in a map in the same place at the start. However, after dying in the map, you spawn in a completely random place on the map.

  2. What is the issue? I don’t really know how to get it to script it, though I have some ideas on how to do it, and I’m genuinely quite lost on where to start. Another problem is that if the player dies, they get sent back to the spawn at the lobby.

  3. What solutions have you tried so far? I think I know how to get it to work, I just need help putting it into practice. For communicaton purposes, assume that the part that all players teleport to at the start is called “Telepart”. The idea is:

  • While a round is in progress (I have a boolean value in Replicated Storage that lets us know if it is in progress called “InRound”), first keep the telepart in a fixed position at the start for 2 seconds, and then move it to random positions at the same Y position, but with random X and Z coordinates.
  • Check if player has died and is on the “Fighting” Team (= on the map)
  • If the above condition is met, teleport the player (I assume this is using the CFrame) to the telepart at that moment and provide a forcefield for 2 seconds.

I’m quite new to game development on Roblox Studio specifically, and to the Developer Forum, so please let me know if I have done something really wrong. Here are 2 scripts in Server Script Service that relate to this:

-- Round Handler, decides if it is a round or not and shows intermission / round gui
local roundLength = 30
local intermissionLength = 5
local InRound = game.ReplicatedStorage:WaitForChild("InRound") -- bool value for if a round is in progress.
local Status = game.ReplicatedStorage:WaitForChild("Status") -- string value that holds what the GUI shows .

local LobbySpawn = game.Workspace.Lobby.LobbySpawn
local GameAreaSpawn = game.Workspace.Map.GameAreaSpawn

InRound.Changed:Connect(function()
	wait(1)
	if InRound.Value == true then
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
		end
	else
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
		end
	end
end)
local function roundTimer()
	while wait() do
		for i = intermissionLength, 1, -1 do
			InRound.Value = false
			wait(1)
			Status.Value = "Intermission: ".. i .." seconds left!"
		end
		for i = roundLength, 1, -1 do
			InRound.Value = true
			wait(1)
			Status.Value = "Game: ".. i .." seconds left!"
		end
	end
end
spawn(roundTimer)
lua
-- Team Handler, puts people on a team.
local gamespawn = game.Workspace.Map.Floor
local lobbyspawn = game.Workspace.Lobby.Floor
local fighting = game.Teams.Fighting
local lobby = game.Teams.Lobby

gamespawn.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		plr.Team = fighting
	end
end)

lobbyspawn.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		plr.Team = lobby
	end
end)

Help would be greatly appreciated. Thank you so much!
P.S. I assume that you would need to know the minimum/maximum X and Z and the fixed Y position - you may refer to these as MaxX, FixedY for simplicity’s sake, or otherwise if you want.

Is there a SpawnLocation for the team? Is there more than one of them?

Sort of. I have a part that teleports players from the lobby to the map, but not a proper SpawnLocation. I can go ahead and put that in as a replacement.

That would work but here’s something else:

Anyways, I cannot exactly understand your code, but you can connect something to Player.CharacterAdded and you can set the character’s position from there.

What does “CharacterAdded” really do? Would I need to change the CFrame of the HumanoidRootPart to the SpawnLocation? Also, how would I know if the player had died?

You can use CharacterAdded to wait for the new character the player spawned in, and then move the character to your desired location.

Just have a spawn location set for like the red team, then team everyone who joins to the red team, then when they die team them to the green team, and have a bunch of green team spawn locations

Thanks, I seem to have figured something out:
I’m not sure if this will work, but I hope that it will move the character’s HumanoidRootPart to the SpawnPoint

local point = game.Workspace.Map.GameAreaSpawn
local fighting = game.Teams.Fighting
game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			if player.Team == fighting then
				player.Character:WaitForChild("HumanoidRootPart").CFrame = point.CFrame
			end
		end)
	end)
end)

Not a bad idea, though I’m so far into this I might as well continue. I will use your idea if what I’m doing currently backfires. Thanks!

1 Like

Ok, so I made it respawn me correctly, but the spawnpoint moving doesn’t work at all. The Output bar shows nothing in red text either.

local point = game.Workspace.Map.GameAreaSpawn
local InRound = game.ReplicatedStorage.InRound
MinX = 735
MinZ = -194
FixY = 50.5
MaxX = 866
MaxZ = -87
point.CFrame = CFrame.new(800, 50.5, -139)
if InRound == true then
	point.CFrame = CFrame.new(800, 50.5, -139)
	wait(2)
	while InRound == true do
		CurX = math.random(MinX, MaxX)
		CurZ = math.random(MinZ, MaxZ)
		point.CFrame = CFrame.new(CurX, FixY, CurZ)
	end
end

I want it to stay in a fixed position at the start of a round, but then move to constantly random positions after 2 seconds. @SubtotalAnt8185

Ok, fixed it. If any future viewers are interested, here is the code:

local point = game.Workspace.Map.GameAreaSpawn
local InRound = game.ReplicatedStorage.InRound
MinX = 735
MinZ = -194
FixY = 50.5
MaxX = 866
MaxZ = -87
point.CFrame = CFrame.new(800, 50.5, -139)
math.randomseed(tick())
while true do
	wait(0.01)
	local x = math.random(MinX, MaxX)
	local z = math.random(MinZ, MaxZ)
	point.CFrame = CFrame.new(x, FixY, z)
end

Obviously, change the Min/Max values as you need. You can get these values by placing 2 parts at diagonally opposite corners and reading off the position property.

You need to add an if statement in that loop, and when it is not InRound then place it into the original position you save into a variable.

1 Like

I dont know if this is correct or the answer your looking for but I think you could move a bunch of spawns from server storage when the round starts.