Datastore is saving isn't saving players data!

I made this leaderboard where it saves the players time spent on the server? Any reason why it won’t save?

Photos:

Code:


--v Settings
local rank = "Global"
local updateTime = 20


--v Datastore
local datastore = game:GetService("DataStoreService")
local timeSpentDatastore = datastore:GetDataStore("timeSpentDatastore")
local serverStorage = game:GetService("ServerStorage")
local timeSpentSaved = {
	spent = 0;
}

game.Players.PlayerAdded:Connect(function(player)
	wait(10)
	game.Workspace:WaitForChild(player.Name)
	local timeSpent = timeSpentDatastore:GetAsync(player.UserId)
	local timeSpentFolder = Instance.new("Folder")
	local timeSpentValue = Instance.new("NumberValue")
	timeSpentFolder.Name = player.Name
	timeSpentFolder.Parent = serverStorage:WaitForChild("timeData")
	timeSpentValue.Name = "timeSpent"
	timeSpentValue.Value = 0
	timeSpentValue.Parent = serverStorage.timeData:WaitForChild(player.Name)
	wait(1)
	if timeSpent then
		serverStorage.timeData:WaitForChild(player.Name):WaitForChild("timeSpent").Value = timeSpent.spent
		print("Loaded "..player.Name.." data successfully".. ": "..timeSpent.spent)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local newTimeSpentSaved = {
		spent = serverStorage.timeData:WaitForChild(player.Name).timeSpent.Value;
	}
	
	timeSpentDatastore:SetAsync(player.UserId, newTimeSpentSaved)
	print("Saved "..player.Name.." data successfully".. ": "..newTimeSpentSaved.spent)
end)


--v Add time
game.Players.PlayerAdded:Connect(function(player)
	wait(12)
	while wait(1) do
		if serverStorage.timeData:FindFirstChild(player.Name) ~= nil then
			serverStorage.timeData:FindFirstChild(player.Name).timeSpent.Value = serverStorage.timeData:FindFirstChild(player.Name).timeSpent.Value +1
		end
	end
end)

function convertToHM(seconds)
	local Minutes = (seconds -seconds%60)/60
	wait()
	local Hours = (Minutes - Minutes%60)/60
	wait()
	local final = Hours.."hrs, "..Minutes.."mins"
	return final
end

--v Update leaderboard
local leaderboard = script.Parent
local scrollingFrame = leaderboard.SurfaceGui.ScrollingFrame
local last = 0
local lastLayout = 0

while wait(updateTime) do
	local timeSpentData = serverStorage.timeData:GetChildren()
	local previousSlots = scrollingFrame:GetChildren()
	for i = 1, #previousSlots do
		local previousSlot = previousSlots[i]
		if previousSlot:IsA("Frame") then
			previousSlot:Destroy()
		end
	end
	wait()
	for i = 1, #timeSpentData do
		local spentData = timeSpentData[i]
		local slot = leaderboard.Slot:Clone()
		slot.Name = spentData.Name
		slot.profile.Image = game.Players:GetUserThumbnailAsync(game.Players[spentData.Name].UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		slot.name.Text = spentData.Name
		slot.rank.Text = game.Players[spentData.Name]:GetRoleInGroup(4311276)
		slot.time.Text = convertToHM(spentData.timeSpent.Value)
		slot.Parent = scrollingFrame
		
		if spentData.timeSpent.Value > last then
			slot.LayoutOrder = lastLayout +1
		else
			slot.LayoutOrder = 0
		end
		local lastLayout = slot.LayoutOrder
		local last = spentData.timeSpent.Value
		wait(1)
	end
end

First off, you really should use pcalls. Secondly, try giving servers time to shut down.

game:BindToClose(function() 
for i,plr in pairs(game.Players:GetPlayers()) do
   plr:Kick('Shutdown') --Just in case player still exists, fires the player removed event
end
wait(30) --Gives server time
end)
2 Likes