Why does my auto respawn system not choose a random spawn point?

Hello!

So I have a ChooseSpawnOnDeath system kind of like Jailbreak’s. Whenever you die, a GUI appears and asks you to choose a spawn point. That works. If you do not choose a spawn point, the game will choose a random spawn point for you. That does not work. The countdown label reaches 5 and then respawns the character at the main spawn point, and not the points in the Locations folder in Workspace.

Hierarchy:
image

Server:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:Connect(function(Player)
	local SpawnChosen = Instance.new("NumberValue")
	SpawnChosen.Name = "SpawnChosen"
	SpawnChosen.Value = 0
	SpawnChosen.Parent = Player
	
	Player.CharacterAppearanceLoaded:Connect(function(Character)
		if SpawnChosen.Value ~= 0 then
			repeat task.wait() until Character:FindFirstChild("HumanoidRootPart")
			if Character:FindFirstChild("HumanoidRootPart") then
				Character.HumanoidRootPart.CFrame = workspace.Locations[tostring(SpawnChosen.Value)].CFrame
			end
			SpawnChosen.Value = 0
		end
		
		Character.Humanoid.Died:Connect(function()
			ReplicatedStorage.ChooseSpawnOnDeathEvent:FireClient(Player, "Died")
			
		end)
	end)
	Player:LoadCharacter()
end)

ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
	if not Player then return end
	if not Argument then return end
	if Player and Argument == "Respawn" then
		Player:LoadCharacter()
	elseif Player and Argument == "Chosen" then
		Player.SpawnChosen.Value = tonumber(Number)
		Player:LoadCharacter()
	end
end)

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Choose = script.Parent.Choose
local Start = script.Parent.Choose.Start
local FirstCountdown = script.Parent.FirstCountdown

local Frame = script.Parent.Frame
local SecondCountdown = Frame.SecondCountdown

local CountNumber
local Continue = false
local Success = false

ReplicatedStorage.ChooseSpawnOnDeathEvent.OnClientEvent:Connect(function(Argument)
	if not Argument then return end
	if Argument == "Died" then
		Choose.Visible = true
		FirstCountdown.Visible = true
		
		CountNumber = 5
		
		repeat
			CountNumber -= 1
			FirstCountdown.Text = CountNumber
			task.wait(1)
		until CountNumber <= 0 or Continue == true
		
		if CountNumber <= 0 then
			ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Respawn")
		end
	end
end)

Start.MouseButton1Click:Connect(function()
	Continue = true
	Choose.Visible = false
	FirstCountdown.Visible = false
	Frame.Visible = true
	
	CountNumber = 10
	
	repeat
		CountNumber -= 1
		SecondCountdown.Text = "AUTO RESPAWN IN "..CountNumber.."..."
		task.wait(1)
	until CountNumber <= 0 or Success == true
	
	if CountNumber <= 0 then
		ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Respawn")
	end
end)

for i, v in ipairs(Frame:GetChildren()) do
	if v.Name == "TextButton" then
		v.MouseButton1Click:Connect(function()
			Frame.Visible = false
			if v:FindFirstChildOfClass("NumberValue") then
				ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Choose", v:FindFirstChildOfClass("NumberValue").Value)
			else
				ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Respawn")
			end
		end)
	end
end

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

6 Likes

ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
	if not Player then return end
	if not Argument then return end
	if Player and Argument == "Respawn" then
		Player:LoadCharacter()
	elseif Player and Argument == "Chosen" then
		Player.SpawnChosen.Value = tonumber(Number)
		Player:LoadCharacter()
	end
end)

For this block it seems “Chosen” sets SpawnChosen.Value, but “Respawn” does not so it will stay the same. Try setting SpawnChosen.Value to a random value before loading the character. Maybe this will work for you?

local maximum_spawn_locations = #workspace.Locations:GetChildren() -- may be incorrect; place your actual number here if different
Player.SpawnChosen.Value = math.floor(math.random(0, maximum_spawn_locations))
Player:LoadCharacter()
3 Likes

It didn’t work. Did I do it right?

ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
	if not Player then return end
	if not Argument then return end
	if Player and Argument == "Respawn" then
		Player:LoadCharacter()
	elseif Player and Argument == "Chosen" then
		local MaxSpawnLocations = #workspace:WaitForChild("Locations"):GetChildren()
		Player.SpawnChosen.Value = math.floor(math.random(0, MaxSpawnLocations))
		Player:LoadCharacter()
	end
end)
1 Like

No, place the random addition to the “Respawn” section. “Chosen” is for when they choose a place to respawn, not have it assigned randomly

1 Like
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
	if not Player then return end
	if not Argument then return end
	if Player and Argument == "Respawn" then
		local MaxSpawnLocations = #workspace:WaitForChild("Locations"):GetChildren()
		Player.SpawnChosen.Value = math.floor(math.random(0, MaxSpawnLocations))
		Player:LoadCharacter()
	elseif Player and Argument == "Chosen" then
		Player.SpawnChosen.Value = tonumber(Number)
		Player:LoadCharacter()
	end
end)
3 Likes

first of all, instead of the repeat wait : findfirstchild. its better to use waitforchild, this yields on itself.

instead of appearance loaded, you could use CharacterAdded.

instead of setting the players humanoidrootpart position it would be better to use the character:PivotTo(cframe)

and since it always chooses the main spawnpoint, are you useing the spawnpoints that come with roblox? it might be that roblox’s spawn script is overtaking your script. add a task.wait() before you set the players character position.

2 Likes

No, I am using my own spawn points.

1 Like

This is the script with the changes now. Did I do everything right?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:Connect(function(Player)
	local SpawnChosen = Instance.new("NumberValue")
	SpawnChosen.Name = "SpawnChosen"
	SpawnChosen.Value = 0
	SpawnChosen.Parent = Player
	
	Player.CharacterAdded:Connect(function(Character)
		if SpawnChosen.Value ~= 0 then
			repeat task.wait() until Character:WaitForChild("HumanoidRootPart")
			if Character:WaitForChild("HumanoidRootPart") then
				task.wait(1)
				Character.HumanoidRootPart:PivotTo(workspace.Locations[tostring(SpawnChosen.Value)].CFrame)
			end
			SpawnChosen.Value = 0
		end
		
		Character.Humanoid.Died:Connect(function()
			ReplicatedStorage.ChooseSpawnOnDeathEvent:FireClient(Player, "Died")
			
		end)
	end)
	Player:LoadCharacter()
end)

ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
	if not Player then return end
	if not Argument then return end
	if Player and Argument == "Respawn" then
		local MaxSpawnLocations = #workspace:WaitForChild("Locations"):GetChildren()
		Player.SpawnChosen.Value = math.floor(math.random(0, MaxSpawnLocations))
		Player:LoadCharacter()
	elseif Player and Argument == "Chosen" then
		Player.SpawnChosen.Value = tonumber(Number)
		Player:LoadCharacter()
	end
end)
1 Like

repeat waitforchild is not needed and when you use pivotto you do not need the humanoid rootpart

pivot to works on models so the character itself

1 Like

Like this?

repeat task.wait() until Character:FindFirstChild("HumanoidRootPart")
			if Character:FindFirstChild("HumanoidRootPart") then
				task.wait(1)
				Character:PivotTo(workspace.Locations[tostring(SpawnChosen.Value)].CFrame)
1 Like

Try just using :WaitForChild

-----
-- repeat task.wait() until Character:FindFirstChild("HumanoidRootPart")
-- replaced by:
-----
local rootpart = Character:WaitForChild("HumanoidRootPart")
Character:PivotTo(workspace.Locations[tostring(SpawnChosen.Value)].CFrame)
1 Like

That didn’t work. There are a lot of things that are messed up now, and I’ll list everything in the morning. I got this off a tutorial:
How to make a SPAWN CHOICE ON DEATH [Like Jailbreak!] in ROBLOX - YouTube
If you want, you can review this and see if you can find what’s wrong.

1 Like

Nvm I fixed it! I disabled CharacterAutoLoads in the Players service and set the RespawnTime to 0. I also misnamed a parameter passed through and that was part of the problem!

Thank you guys for your help. Have a great day! :slight_smile:

1 Like

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