Leaderstat save not working

I am trying to make a checkpoint save when the player leaves and so when they join it teleports them back to the CFrame of the checkpoint, however I am having trouble identifying which checkpoint the player was last at.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

--Don't worry about the rest of the code, except for line 25.
game.Players.PlayerAdded:connect(function(player)
	local Stage = player.leaderstats.Stage
	Stage.Value = ds:GetAsync(player.UserId)
	ds:SetAsync(player.UserId, Stage.Value)
	Stage.Changed:connect(function()
		ds:SetAsync(player.UserId, Stage.Value)
	end)
	game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.CFrame = game.Workspace.Checkpoint(Stage.Value)
end)

game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.Stage.Value) --Change "Points" to the name of your leaderstat.
end)

How would I change game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.CFrame = game.Workspace.Checkpoint(Stage.Value) to work? There are parts in the Workspace called Checkpoint1, Checkpoint2, Checkpoint3 etc…

First, there is no guarantee of the character even existing here in the first place when you do your teleport. This can be fixed by:

local character = if player.Character then player.Character else player.CharacterAdded:Wait()

Then, to teleport the character to the stage value, you can PivotTo() the character (you can set the HumanoidRootPart CFrame too but you would need to also wait for it). The CFrame to PivotTo would be equal to the CFrame of the part with the name "Checkpoint"..tostring(Stage.Value). So you can look for this part in workspace.

local checkpointName = "Checkpoint"..tostring(Stage.Value)
character:PivotTo(workspace:FindFirstChild(checkpointName).CFrame)
1 Like

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