RespawnLocation problem

I’m creating a spawn location system based on the player’s current leaderstats value. However, when I try configuring the RespawnLocation, I get an error that the property is asking for an object (“Unable to assign property RespawnLocation. Object expected, got CFrame”). I’ve realized by now that it also doesn’t accept CFrames, but how can I give RespawnLocation the player’s current checkpoint given this code?

	
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local CurrentCheckpoint = Instance.new("IntValue")
	CurrentCheckpoint.Parent = leaderstats
	CurrentCheckpoint.Value = 0
	CurrentCheckpoint.Name = "Checkpoint"
	
	local obby = workspace.Obby
	local checkpoints = obby.Checkpoints
	
	for checkpoint = 1, #checkpoints:GetChildren() do
		checkpoints[checkpoint].Touched:Connect(function(touch)
			CurrentCheckpoint.Value = checkpoint
			
			local player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent)
			player.RespawnLocation =   checkpoints[checkpoint].CFrame -- The line in question
		end) 
	end
end)```

player.RespawnLocation takes an object not a CFrame

player.RespawnLocation = checkpoints[checkpoint] (Remove the .CFrame from this line)

1 Like

Script throws this error when I step on the second checkpoint.

Expected SpawnLocation got Part for Player:RespawnLocation.

You are trying to assign a normal part as a RespawnLocation
A SpawnLocation is different from a normal part

Instead, teleport the player to the checkpoint when they spawn
(Put this in StarterPlayer > StarterCharacterScripts as a script)

local Players = game:GetService("Players")

local Character = script.Parent
local Player = Players:GetPlayerFromCharacter(Character)

local Checkpoint = workspace.Obby.Checkpoints:FindFirstChild(Player.leaderstats.Checkpoint.Value)

if Checkpoint then
	Character:PivotTo(Checkpoint.CFrame)
end

The script above gets their player object from their character
We then get the checkpoint from their leaderstats
If this checkpoint exists, we move the character to that checkpoint

Make sure you remove the player.RespawnLocation assignment line when you put this script in or it will continue to error

1 Like

Hello!
The issue is that you’re trying to set a CFrame to an IntValue.

Change this:

To this:

local CurrentCheckpoint = Instance.new("CFrameValue")

CFrameValues contains CFrames. IntValue contains whole numbers.

awesome, this works! Sorry if this is an obvious question but would this still work if I add future checkpoints?

1 Like

Yes it will continue to work if you add more checkpoints

2 Likes

Of course! Just put all of your checkpoints inside of the same container (In your case, obby.Checkpoints) and there will be no problems :smile:

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