Datastore script not working

Hey Everyone,
I’m making a datastore script, but it’s not working. It does not save the data.
Here is my script:

local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')

local WinsDataStore = DataStoreService:GetDataStore('Wins')


Players.PlayerAdded:Connect(function(Player)
	
	local Stats = Instance.new('Folder')
	Stats.Name = 'leaderstats'
	Stats.Parent = Player
	
	local Wins = Instance.new('IntValue')
	Wins.Name = 'Wins'
	Wins.Parent = Stats
	
	
	local Data = WinsDataStore:GetAsync(Player.UserId)
	
	
	if Data then
		for name, value in pairs(Data.Stats) do
			Stats[name].Value = value
		end
		
	end
	
		
		
end)

Players.PlayerRemoving:Connect(function(Player)
	for _, Player in pairs(game.Players:GetPlayers()) do
		local SaveData = {Stats = {}}
		

		
		for _,stat in pairs(Player.leaderstats:GetChildren()) do
			SaveData[stat.Name] = stat.Value
		end
		

		
		WinsDataStore:SetAsync(Player.UserId,SaveData)
		
	end
	
	wait(2)
end)
	
	

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local SaveData = {Stats = {}}
		

		
		for _,stat in pairs(Player.leaderstats:GetChildren()) do
			SaveData[stat.Name] = stat.Value
		end
		

		
		WinsDataStore:SetAsync(Player.UserId,SaveData)
		
	end
	
	wait(2)
end)

I have no errors. thanks for any help!

2 Likes

Can you try adding some prints so we can know whats the problem here?

2 Likes

Could It be my leaderboard script?

local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')

local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobalNew')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard

local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()

local function update()
	
	for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
		if child:IsA('Frame') then
			child:Destroy()
		end
	end
	
	local success,err = pcall(function()
		local data = GlobalDataStore:GetSortedAsync(false,50)
		local page = data:GetCurrentPage()
		
		for rank,plrData in ipairs(page) do
			local userid = plrData.key
			local wins = plrData.value
			
			if rank <= 3 then
				local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
				
				if npc then
					npc.UserId.Value = userid

				end
			end 
			
			local new = winsTemplate:Clone()
			new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
			new.PlrAmount.Text = wins
			new.LayoutOrder = rank
			
			new.Parent = winsBoard.SurfaceGui.Leaderboard
		end
	end)
	
	
end

while true do
	
	update()
	
	wait(math.random(120,180))
	
	spawn(function()
		for _, Player in pairs(game.Players:GetPlayers()) do
			
			GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
			wait(math.random(2,4))
		end
	end)
end
1 Like

Please change it to local SaveData = {}.

2 Likes

are you trying to save the wins or the leaderboard?

1 Like

Both…

2 Likes

witch one is not working? (30 characters)

1 Like

The datastore one…

1 Like

Wrap both of these in a pcall function. This way if the script errors you can get a reason of the problem, and then figure it out from there.

 local Succses, ErrorMsg = pcall(function()
 WinsDataStore:SetAsync(Player.UserId,SaveData)
 end)

If Succses then print(Succses) else print(ErrorMsg) end

The error message should give you a code and you can find out what it means by searching it up.

1 Like

I changed it to that and it worked, thank you.

2 Likes

Do you know why my leaderboard is empty if I have 3 wins?

local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')

local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobalNew')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard

local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()

local function update()
	
	for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
		if child:IsA('Frame') then
			child:Destroy()
		end
	end
	
	local success,err = pcall(function()
		local data = GlobalDataStore:GetSortedAsync(false,50)
		local page = data:GetCurrentPage()
		
		for rank,plrData in ipairs(page) do
			local userid = plrData.key
			local wins = plrData.value
			
			if rank <= 3 then
				local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
				
				if npc then
					npc.UserId.Value = userid

				end
			end 
			
			local new = winsTemplate:Clone()
			new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
			new.PlrAmount.Text = wins
			new.LayoutOrder = rank
			
			new.Parent = winsBoard.SurfaceGui.Leaderboard
		end
	end)
	
	
end

while true do
	
	update()
	
	wait(math.random(120,180))
	
	spawn(function()
		for _, Player in pairs(game.Players:GetPlayers()) do
			
			GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
			wait(math.random(2,4))
		end
	end)
end
1 Like

It’s probably failing to get the data in datastore.

1 Like

Do you know how I can fix it?..

1 Like

You need to wrap it in a pcall to find out if then request was successful or not. Proceed to @ScriptedBinary’s post.

1 Like

I added this:

local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')

local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobalNew')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard

local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()

local function update()
	
	for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
		if child:IsA('Frame') then
			child:Destroy()
		end
	end
	
	local success,err = pcall(function()
		local data = GlobalDataStore:GetSortedAsync(false,50)
		local page = data:GetCurrentPage()
		
		for rank,plrData in ipairs(page) do
			local userid = plrData.key
			local wins = plrData.value
			
			if rank <= 3 then
				local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
				
				if npc then
					npc.UserId.Value = userid

				end
			end 
			
			local new = winsTemplate:Clone()
			new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
			new.PlrAmount.Text = wins
			new.LayoutOrder = rank
			
			new.Parent = winsBoard.SurfaceGui.Leaderboard
		end
	end)
	
	if not success then
		warn('An error occured, '..err)
	else
		print('Success')
	end
	
	
end

while true do
	
	update()
	
	wait(math.random(120,180))
	
	spawn(function()
		for _, Player in pairs(game.Players:GetPlayers()) do
			
			GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
			wait(math.random(2,4))
		end
	end)
end

But it’s printing Success