How would i add a datastore to roblox' checkpoint system

Hey there!

I am currently making a game that requires checkpoints and it would be nice to have it save so that when the player returns to the game, they’re at the spawn they were at last.

I tried thinking of ways to create this but couldnt since roblox names the value as the player ID.

Here is the script:

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

local checkpoint = script.Parent

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData ="Folder")
			checkpointData.Name = "CheckpointData"
			checkpointData.Parent = ServerStorage
		local userIdString = tostring(player.UserId)
		local checkpointValue = checkpointData:FindFirstChild(userIdString)
		if not checkpointValue then
			checkpointValue ="ObjectValue")
			checkpointValue.Name = userIdString
			checkpointValue.Parent = checkpointData
				local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
				character:MoveTo(storedCheckpoint.Position +, 4), 4, math.random(-4, 4)))
		checkpointValue.Value = checkpoint


Thanks for the help! :smile:

You can get the username from user id:
local name = game.Players:GetNameFromUserIdAsync(userId)
And it’ll return the username of given id

1 Like

Thank you, but i need to get the name of an ObjectValue which is the players UserID, not their username.

If you are trying to get the players userId from their username there is another function for it, I recommend you take a look at this.

1 Like