My data is not saving

My deaths value and seconds value do not save but my stages value does.

local died = false
local secondsHandler = false
local checkpoints = workspace.Checkpoints
local dss = game:GetService("DataStoreService")
local Datastore = dss:GetDataStore("ObbyData")
local Players = game:GetService("Players")
local DataLoaded = {}
game:BindToClose(function()
	for _,Player in pairs(Players:GetPlayers())do
		Player:Kick("This server is shutting down")
	end
	wait(3)
end)
print("Checkpoints leaderstats working")
print("Deaths leaderstats working")
print("Time leaderstats working")
game.Players.PlayerAdded:Connect(function(plr)
	local obbyData = Datastore:GetAsync(plr.UserId .. "-obbyStageProgress")
	local secondData = Datastore:GetAsync(plr.UserId .. "-SecondsData")
	local wipeoutsData = Datastore:GetAsync(plr.UserId .. "-DeathsData")
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local stage = Instance.new("StringValue")
	stage.Name = "Stage"
	stage.Value = (obbyData and obbyData[1]) or 1
	stage.Parent = stats
	
	local wipeouts = Instance.new("IntValue")
	
	wipeouts.Name = "Deaths"
	wipeouts.Value = (wipeoutsData and wipeoutsData[2])
	wipeouts.Parent = stats
	
	local second = Instance.new("IntValue")

	second.Name = "Seconds"
	second.Value = (secondData and secondData[3])
	second.Parent = stats

        DataLoaded[plr.Name] = true	

	local char = plr.Character or plr.CharacterAdded:Wait()
	char:WaitForChild("HumanoidRootPart").CFrame = checkpoints[stage.Value].CFrame
	
	char.Humanoid.Touched:Connect(function(touch)
		if touch.Parent == checkpoints then
			if (tonumber(touch.Name)and tonumber(touch.Name)>tonumber(stage.Value))or touch.Name == "End"then
				stage.Value = touch.Name
				
			end
		end
		
		plr.CharacterAdded:Connect(function(char)
			local hrp = char:WaitForChild("HumanoidRootPart")
			local humanoid = char:WaitForChild("Humanoid")
			hrp:GetPropertyChangedSignal("CFrame"):Wait()
					hrp.CFrame = checkpoints[stage.Value].CFrame
			humanoid.Died:Connect(function()
			if not died then
				died = true
				wipeouts.Value = wipeouts.Value + 1
                wait(5)
				died = false
			end
end)	
				
			
				
				local humanoid = char:WaitForChild("Humanoid")	
			humanoid.Touched:Connect(function(touch)
				if touch.Parent == checkpoints then
					if (tonumber(touch.Name)and tonumber(touch.Name)>tonumber(stage.Value))or touch.Name == "End" then
						stage.Value = touch.Name
						
					end
					
				end
			end)
			
								
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local DatastoreTable = {
	player.leaderstats.Stage.Value,
	player.leaderstats.Deaths.Value,
	player.leaderstats.Seconds.Value
	}
	local success, fail = pcall(function()
		Datastore:SetAsync(player.UserId .. "-obbyStageProgress", DatastoreTable)
	end)
	if success then print(success) else print(fail) end
end)

while true do
	wait()
    for _,player in pairs(Players:GetPlayers()) do
     if DataLoaded[player.Name] then
         player.leaderstats.Seconds.Value = player.leaderstats.Seconds.Value + 1
     end
        wait(1)
    end
end

9 posts were merged into an existing topic: Data not saving

Do not create duplicate topics.