I am trying to make a program where the player’s checkpoints are saved. When the player rejoins, they get teleported back to the last stage they were on.
I have no idea why this is happening and I need help
Scripts for saves:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("StagesForPlayer")
game.Players.PlayerAdded:Connect(function(player)
local data
local success, errorMessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
player.leaderstats.Stage.Value = data
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data
local success, errorMessage = pcall(function()
data = myDataStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
end)
if success then
print("Data Saved")
else
warn(errorMessage)
end
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
v:Kick("Server Closed")
end
wait(2)
end)
Scripts for checkpoints:
local checkpoints = workspace:WaitForChild("Checkpoints")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = 0
stage.Parent = leaderstats
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
wait()
char:MoveTo(checkpoints[stage.Value].Position)
hum.Touched:Connect(function(hit)
if hit.Parent == checkpoints then
if tonumber(hit.Name) == stage.Value + 1 then
stage.Value = stage.Value + 1
end
end
end)
end)
end)
Combine both your scripts together so that its synced.
Try this: (delete both scripts and make a new one)
--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
--//Variables
local myDataStore = DataStoreService:GetDataStore("StagesForPlayer")
local checkpoints = workspace:WaitForChild("Checkpoints")
--//Functions
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = 0
stage.Parent = leaderstats
local data = nil
local success, errorMessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
stage.Value = data or 0
else
warn(errorMessage)
end
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
task.wait()
char:MoveTo(checkpoints[stage.Value].Position)
hum.Touched:Connect(function(hit)
if hit.Parent == checkpoints and tonumber(hit.Name) == stage.Value + 1 then
stage.Value += 1
end
end)
end)
end)
Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
end)
if success then
print("Data Saved")
else
warn(errorMessage)
end
end)
game:BindToClose(function()
for i, player in ipairs(Players:GetPlayers()) do
player:Kick("Server Closed")
end
end)