Spawned to disabled spawn location in roblox studio

I want to spawn players to a specific location when they die. I have a game that contains a few levels and I want the users to be teleported to the start of the level. I have a spawn location that is on each level, but for some reason when I die on the second level, I sometimes get spawned to a disabled spawn pad on the level I have not yet completed.

There are two approaches that I took to solve the issue, one was by disabling all spawn locations except for the level the player is on, the other was creating custom spawn code that sends the user to the last level that the user has already been to.

I did try to create custom spawning code to detect the character being added and spawn them to a variable location but this does not work every time or if at all. If anybody has an idea about best practices for this case I would love to hear it.

The game is: (1) Julianopolous - Roblox

Spawn locations have given me a hard time in the past. On a recent project, I created a module that works like spawn points by having a table that contains the spawn position as a vector3 for the player, and when they “respawn” I teleport them to that position.

Example

  1. Player touched new spawn point (if it’s an obby)
  2. I send the position of the spawn point to the server, and index it for the player in the spawn position table.
  3. When they reset, it teleports them to that new position

Which event exactly are you using to detect the respawn?

local function onCharacterAdded(character)
	local player = Players:GetPlayerFromCharacter(character)
	-- Check if we saved a respawn location for this player
	if inventoryModule.ObbyStage[player] then
		-- Teleport the player there when their HumanoidRootPart is available
		local hrp = character:WaitForChild("HumanoidRootPart")
		-- Wait a brief moment before teleporting, as Roblox will teleport the
		-- player to their designated SpawnLocation (which we will override)
		RunService.Stepped:wait()
		hrp.CFrame = CFrame.new(inventoryModule.ObbyStage[player][3] + Vector3.new(0, 3.5, 0))
	end
end

local function onCharacterRemoving(character)
	-- Get the player and their HumanoidRootPart and save their death location
	local player = Players:GetPlayerFromCharacter(character)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp and inventoryModule.ObbyStage[player] then
		inventoryModule.ObbyStage[player][3] = inventoryModule.ObbyStage[player][3] + Vector3.new(0, 3.5, 0)
	end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterRemoving:Connect(onCharacterRemoving)
end)

Can I assume this is a server script?

Yes

Out of curiosity did you also disable the default auto spawn as is present here: Players.CharacterAutoLoads (roblox.com)

No, I’m letting the player spawn in but at the same time I’m changing their position

You could just store an ObjectValue instance inside of the player instance which tracks the last spawn the player’s character touched. When the player’s character comes into contact with any spawn the value of their ObjectValue instance is set to the spawn itself, then whenever the player’s character respawns they are teleported to the last spawn point they touched by checking this ObjectValue instance which belongs to them.

local Players = game:GetService("Players")
local Spawns = workspace.Spawns --change to name of folder/model which contains spawns

Players.PlayerAdded:Connect(function(Player)
	local _Spawn = Instance.new("ObjectValue")
	_Spawn.Name = "Spawn"
	_Spawn.Parent = Player
	Player.CharacterAdded:Connect(function(Character)
		task.wait()
		if _Spawn.Value then
			Character:PivotTo(_Spawn.Value.CFrame)
		end
	end)
end)

for _, _Spawn in ipairs(Spawns:GetChildren()) do
	if _Spawn:IsA("SpawnLocation") then
		_Spawn.Touched:Connect(function(Hit)
			local Player = Players:GetPlayerFromCharacter(Hit.Parent)
			if Player then
				Player._Spawn.Value = _Spawn
			end
		end)
	end
end

Unfortunately something else is happening with the autospawn feature, I am still getting spawned to the wrong spot, so I believe that I am going to turn off auto spawn and attempt to simplify my spawning code to one location.

This is ultimately what I will do anyways when a user enters the game so that I can put a sort of welcome to the game interface possibly multi-language at some point.

In addition, I believe I am going to put a polling check of every so often checking my variable of the users level/last checkpoint against their actual position to make sure they are where they belong. I have not determined exactly the constraints outside of these minimal thoughts, but this is my current direction.

Thank you for your comments