I have a datastore that struggles to save, but I was told that there was an issue with BindToClose not being in the script. However, how would I do that as I want it to save consistently with no problems.
local STAT_NAME = "Stage"
local PREVENT_SKIPPING = true
local dataStore = game:GetService('DataStoreService'):GetDataStore('PlaceHolederName')
local checkpoints = {}
local i = 1
while true do
local checkpoint = game.Workspace:FindFirstChild("Checkpoint " .. i, true)
if not checkpoint then print("Last Checkpoint : " .. i-1) break end
table.insert(checkpoints, checkpoint)
i = i + 1
end
game.Players.PlayerAdded:connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats") or Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = STAT_NAME
checkpointStat.Value = 1
local pointsKey = "player"..player.UserId
local savedStuff = dataStore:GetAsync(pointsKey)
if savedStuff then
checkpointStat.Value = savedStuff
else
wait(5)
if savedStuff then
checkpointStat.Value = savedStuff
end
end
player.CharacterAdded:connect(function(character)
local goto = checkpoints[leaderstats.Stage.Value]
if goto then
repeat wait() until character.Parent
character:MoveTo(goto.Position)
else
warn("Checkpoint " .. checkpointStat.Value .. " not found")
end
end)
while true do
wait(30)
dataStore:SetAsync(pointsKey, leaderstats.Stage.Value)
end
end)
for index, checkpoint in ipairs(checkpoints) do
checkpoint.Touched:connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid or humanoid.Health <= 0 then return end
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
if (PREVENT_SKIPPING and leaderstats.Stage.Value + 1 == index) or (not PREVENT_SKIPPING and leaderstats.Stage.Value < index) then
leaderstats.Stage.Value = index
end
end)
end
game.Players.PlayerRemoving:Connect(function(player)
local pointsKey = "player"..player.UserId
local valuesToSave = player.leaderstats.Stage.Value
wait(5)
if not valuesToSave then return end
dataStore:SetAsync(pointsKey, valuesToSave)
end)