How to make checkpoints save with DataStore?

Here is a script for checkpoints:

local spawn = script.Parent
spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = game.ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Model", game.ServerStorage)
			checkpointData.Name = "CheckpointData"
		end
		
		local checkpoint = checkpointData:FindFirstChild(tostring(player.userId))
		if not checkpoint then
			checkpoint = Instance.new("ObjectValue", checkpointData)
			checkpoint.Name = tostring(player.userId)
			
			player.CharacterAdded:connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = game.ServerStorage.CheckpointData[tostring(player.userId)].Value.CFrame + Vector3.new(0, 4, 0)
			end)
		end
		
		checkpoint.Value = spawn
	end
end)

How would i make the checkpoints save with DataStore?

Hi, datastores can be a bit complex and difficult to get working efficiently and properly, there are also problems with it such as data loss. Luckily there exist people who have made systems that is friendly and easy for people who just wants to store player data super easily. I personally use profileservice, which is a really good way of storing player data. I am aware your specifically asking for how to do this with “DataStore” but if you find DataStore to be difficult, then I would advise using profileservice and learning that.

If you want to do this with a datastore, you can add a value of how many checkpoints someone has reached, and with a script you can do this :


local DS = game:GetService("DataStoreService"):GetDataStore("Checkpoints")

function SaveAttempt(user,value)
	wait(0.01)
	local id = tostring(user.UserId)
	local success, errormsg = pcall(function()
		DS:SetAsync(id,value)
	end)
	if not success then
		print("AN ERROR OCCURED. RETRYING...")
		SaveAttempt()
	else
		print("SUCCESSFULLY SAVED DATA")
	end
end

function LoadAttempt(user)
	wait(0.01)
	local id = tostring(user.UserId)
	local value = nil
	local success, errormsg = pcall(function()
		value = DS:SetAsync(id,value)
	end)
	if not success then
		print("AN ERROR OCCURED. RETRYING...")
		LoadAttempt()
	else
		print("SUCCESSFULLY LOADED DATA")
		return value
	end
end

game.Players.PlayerRemoved:Connect(function(user)
	local value = YOUR_VALUE -- Replace this with the value of the checkpoints the player has.
	SaveAttempt(user,value)
end)

game.Players.PlayerAdded:Connect(function(user)
	local SAVED_VALUE = LoadAttempt(user) -- This will be the loaded value.
end)

Make sure that the script is not local. Also, replace the “value” variable with the amount of checkpoints the player has reached.

If this doesn’t work or if you have any questions, please inform me and I’ll reply when I’m able to.

2 Likes

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