Datastores not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want my Datastores to work.

  2. What is the issue? Include screenshots / videos if possible!

The Datastores won’t work.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have made a separate variable for the player. And that is it. And I have looked for solutions on the Developer Hub. But gone worked.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is the code.

local ds = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
local p =  game:GetService("Players")

p.PlayerAdded:Connect(function(plr)

	wait()
	local plrkey = "id_"..plr.UserId	
	local s = {plr.leaderstats.Taps, plr.leaderstats.Gems, plr.leaderstats.Rebirths}

	local sd = ds:GetAsync(plrkey)
	if sd then 
		s[1] = sd
		s[2] = sd
		s[3] = sd
	else
		local nfs = {s[1].Value, s[2].Value, s[3].Value}
		ds:GetAsync(plrkey, nfs)
	end

	print(sd)

end)

game.Players.PlayerRemoving:Connect(function(plr)

	ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Taps.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Rebirths.Value})

end)

the script looks fine, the problem may be that you didnt enable API services, to do this go to

-Game settings

-Security

-Enable API services

image

Screenshot 2022-02-06 162320

I believe it’s because you don’t put any pcall function

1 Like

You should probably wrap your functions in a pcall so you know if your code ran successfully or not. Shows you how to use them in a datastore here: Data Stores

1 Like

It’s SetAsync(), because you’re setting a value rather than getting it.

Also, you forgot to add .Value to each of your stats and index the saved table’s values.

if sd then 
	s[1].Value = sd[1]
	s[2].Value = sd[2]
	s[3].Value = sd[3]
else
	local nfs = {s[1].Value, s[2].Value, s[3].Value}
	ds:SetAsync(plrkey, nfs)
end

By the way, I’d recommend using BindToClose() to prevent data loss on server shut down.

game:BindToClose(function()
	for _, plr in ipairs(Players:GetPlayers()) do
		-- Save data
	end
end)
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("CoinsDataStore")
local p =  game:GetService("Players")

p.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local taps = leaderstats:WaitForChild("Taps")
	local gems = leaderstats:WaitForChild("Gems")
	local rebirths = leaderstats:WaitForChild("Rebirths")

	local data = ds:GetAsync("id_"..plr.UserId)
	if data then 
		taps.Value = data[1] or 0
		gems.Value = data[2] or 0
		rebirths.Value = data[3] or 0
	end
end)

p.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Taps.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Rebirths.Value})
end)