So I am making an obby and I want to ask a question. I have a checkpoint system in my obby. I have the script inside the serverscriptservice. I have 2 values on the leader board: Coins & Checkpoints. I also have the data store which saves the values on the leader board. But when I join the game, it does not spawn me on the first checkpoint which it used to do before I added the data store inside the same script.
This is the script inside the serverscriptservice:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Checkpoint = Instance.new("IntValue", leaderstats)
Checkpoint.Name = "Checkpoint"
Checkpoint.Value = 1
local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
coins.Value = 0
--Checkpoint Section
player.CharacterAdded:Connect(function(character)
repeat wait() until player.character ~= nil
local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value)
print(checkpoint)
character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 2, 0))
end)
--Checkpoint Section End's here
--Data Saving
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
coins.Value = data.Coins
Checkpoint.Value = data.Checkpoint
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local data = {
Coins = player.leaderstats.Coins.Value;
Checkpoint = player.leaderstats.Checkpoint.Value
}
myDataStore:SetAsync(player.UserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error saving the data.")
warn(errormessage)
end
end)
So inside the checkpoint section, if I don't make the data store below, the checkpoint will spawn me on the first checkpoint. But if I add the data store which is below the checkpoint section, the checkpoint section script does not spawn me to the first checkpoint. However, the data store is working perfectly fine and it is saving the checkpoint's and the coins in the leader board.
Please do help.
Thanks

2 comments
your script is only loading you to the checkpoint, anything after that you will respawn on level 1
But I don't spawn on the first checkpoint.