DataStore not working

So i have created this DataStore script but it doesnt save any data but i dont get any errors

API services are enabled so thats not it. So maybe somebody can find a error in my code?

local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Coins
	local save2 = plr.leaderstats.Gems
	local save3 = plr.leaderstats.Wins
	local save4 = plr.leaderstats.XP
	
	local GetSaved = dataStore:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
		save4.Value = GetSaved[4]
	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
		dataStore:GetAsync(plrid,NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
end)

Try doing SetAsync using game:BindToClose and try again.

So replace
dataStore:SetAsync with game:BindToClose?

No, create another connection using BindToClose and then do SetAsync there.

When you use :BindToClose you want to get all the players in the game and save their stats by just doing setASync() and adding a random wait() to yield the server from stopping so it has time to store all the data.

Forgot to add that, thanks! Usually a task.wait(3) should do it.

1 Like
game.Players.PlayerRemoving:Connect(function(plr)
	task.wait(3)
	dataStore:GetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})

so it should look like this?

char limit

Edit: i tried this but still no data gets saved

In the code you send you call :GetAsync() instead of :SetAsync()

game:BindToClose(function()
    task.wait(3)
    for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
       dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
    end
end)

It somehow still doesnt save any data

Can you try printing before the data is saved and after?

local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Coins
	local save2 = plr.leaderstats.Gems
	local save3 = plr.leaderstats.Wins
	local save4 = plr.leaderstats.XP
	

	local GetSaved = dataStore:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
		save4.Value = GetSaved[4]
	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
		dataStore:GetAsync(plrid,NumberForSaving)
		
		print("Yes")
	end
end)

game:BindToClose(function()
	print("no")
	task.wait(3)
	for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
		dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
	end
end)

It prints the β€˜β€˜Yes’’, but not the β€˜β€˜No’’ so the problem is in the last section of code

Ok then. BindToClose is a security measure to update the data when the game shutdowns, but I meant to print the values you are storing.

local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Coins
	local save2 = plr.leaderstats.Gems
	local save3 = plr.leaderstats.Wins
	local save4 = plr.leaderstats.XP
	

	local GetSaved = dataStore:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
		print("Coins")
		save2.Value = GetSaved[2]
		print("Gems")
		save3.Value = GetSaved[3]
		print("Wins")
		save4.Value = GetSaved[4]
		print("XP")
	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
		dataStore:GetAsync(plrid,NumberForSaving)
	end
end)

game:BindToClose(function()
	task.wait(3)
	for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
		dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
	end
end)`

Like this? it doesnt print anything

print the value, not a string. save.Value

if GetSaved then
		save1.Value = GetSaved[1]
		print(save1.Value)
		save2.Value = GetSaved[2]
		print(save2.Value)
		save3.Value = GetSaved[3]
		print(save3.Value)
		save4.Value = GetSaved[4]
		print(save4.Value)

Like this? doesnt print anything

I really appreciate you trying to help me but im kinda confused xD

If it doesn’t print anything it means that GetSaved is nil

So what do i have to do to get this all working, because when im ingame on roblox i give myself 1k coins but when i join back it just didnt save

Try changing this to SetAsync

Still doesnt save i really dont get why this is not working for me