Datastore not working but worked in another game?

Alright so in my game theres a leaderstats called minutes played, but whenever i try to datastore it it wont work ive used datastores before in other games but this one doesnt seem to work, any help?

-- TrainedDoorman

-- Put this in ServerScriptService
-- Watch the kawaii


-- Saving and Loading settings --
local AUTO_SAVE = false			-- Make true to enable auto saving
local TIME_BETWEEN_SAVES = 60	-- In seconds (WARNING): Do not put this lower than 60 seconds
local PRINT_OUTPUT = false		-- Will print saves and loads in the output
local SAFE_SAVE = false			-- Upon server shutdown, holds server open to save all data
---------------------------------

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local leaderboardData = dataStoreService:GetDataStore("LeaderStats")

local function Print(message)
	if PRINT_OUTPUT then print(message) end
end

local function SaveData(player)
	if player.userId < 0 then return end
	player:WaitForChild("leaderstats")
	wait()
	local leaderstats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		table.insert(leaderstats, {stat.Name, stat.Value})
	end
	leaderboardData:SetAsync(player.userId, leaderstats)
	Print("Saved "..player.Name.."'s data")
end

local function LoadData(player)
	if player.userId < 0 then return end
	player:WaitForChild("leaderstats")
	wait()
	local leaderboardStats = leaderboardData:GetAsync(player.userId)
	for i, stat in pairs(leaderboardStats) do
		local currentStat = player.leaderstats:FindFirstChild(stat[1])
		if not currentStat then return end
		currentStat.Value = stat[2]
	end
	Print("Loaded "..player.Name.."'s data")
end

players.PlayerAdded:connect(LoadData)
players.PlayerRemoving:connect(SaveData)

if SAFE_SAVE then
	game.OnClose = function()
		for i, player in pairs(players:GetChildren()) do
			SaveData(player)
		end
		wait(1)
	end
end

while AUTO_SAVE do
	wait(TIME_BETWEEN_SAVES)
	for i, player in pairs(players:GetChildren()) do
		SaveData(player)
	end
end```
1 Like

Are there any errors? If so, could you provide them and mention at which line it fails?

A method i use to find mistakes in my code when no errors occur:
Type print() in certain spots where you think the code will fail and if the print() doesn’t get fired at the location, you know the code has issues there.

code has no issues just wont save

1 Like

I have been working on some code. It may not be perfect but it worked for me.

-- Services
Players = game:GetService("Players")
DataStoreService = game:GetService("DataStoreService")

-- References
leaderstats = script:FindFirstChild("leaderstats")
DataStore = DataStoreService:GetDataStore("leaderstatsData")

-- Configuration
Autosave = true
AutosaveDelay = 60
ShutdownSave = true
MaxAccessAttempts = 3
SaveAttemptDelay = 1

-- Script
if not leaderstats then
	leaderstats = Instance.new("IntValue") do
		leaderstats.Name = "leaderstats"
		local MinutesPlayed = Instance.new("NumberValue") do
			MinutesPlayed.Name = "Minutes played"
			MinutesPlayed.Parent = leaderstats
		end
	end
end

function LoadDefault(Player)
	local NewStats = leaderstats:Clone() do
		NewStats.Parent = Player
	end
end

function SaveData(Player)
	local leaderstats = Player:FindFirstChild("leaderstats")
	
	assert(leaderstats, "leaderstats doesn't exist!")
	
	local CurrentAccessAttempt = 0
	
	local Data = {}
	local Success, Error
	
	for i, v in ipairs(leaderstats:GetChildren()) do
		table.insert(Data, {Name = v.Name, Value = v.Value})
	end
	
	repeat
		CurrentAccessAttempt = CurrentAccessAttempt + 1
		
		Success, Error = pcall(function()
			DataStore:SetAsync(Player.UserId, Data)
		end)
		
		if Success then
			break
		end
	until CurrentAccessAttempt == MaxAccessAttempts
	
	if Success then
		print("[SaveData] Successfully saved data of player "..Player.Name)
	else
		print("[SaveData] Unsucessfully saved data of player "..Player.Name..". Error: "..Error)
	end
end

function LoadData(Player)
	local leaderstats = leaderstats:Clone()
	
	local CurrentAccessAttempt = 0
	
	local Data
	local Success, Error
	
	repeat
		CurrentAccessAttempt = CurrentAccessAttempt + 1
		
		Success, Error = pcall(function()
			Data = DataStore:GetAsync(Player.UserId)
		end)
		
		if Success then
			break
		end
	until CurrentAccessAttempt == MaxAccessAttempts or Success
	
	if Success then
		if Data then
			print("[LoadData] Successfully retrieved data of player "..Player.Name)
			for i, v in ipairs(Data) do
				local Stat = leaderstats:FindFirstChild(v.Name)
				if Stat then
					Stat.Value = v.Value
				end
			end
			leaderstats.Parent = Player
		else
			print("[LoadData] Player "..Player.Name.."'s data doesn't exist yet, saving default stats")
			LoadDefault(Player)
		end
	else
		print("[LoadData] Unsucessfully retrieved data of player "..Player.Name..", loading default stats. Error: "..Error)
		LoadDefault(Player)
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Player:WaitForChild("leaderstats")
	local Minutesplayed = leaderstats:WaitForChild("Minutes played")
	
	while wait(60) do
		Minutesplayed.Value = Minutesplayed.Value + 1
	end
end)

game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)

if Autosave then
	spawn(function()
		while wait(60) do
			for i, v in ipairs(Players:GetPlayers()) do
				SaveData(v)
			end
		end
	end)
end

if ShutdownSave then
	game.Close:Connect(function()
		while wait(60) do
			for i, v in ipairs(Players:GetPlayers()) do
				SaveData(v)
			end
		end
	end)
end