DataStore is not working!

So I was using this simple datastore script and It was working fine!
not it just decides not to work, help needed!

Thanks in advance

Code:

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“JustDoIt”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "cash"
cash.Parent = leaderstats

local data

pcall(function()
	
	data = DataStore:GetAsync(player.userId)
	
end)

if data then
	
	cash.Value = data
	
else
	
	local Save1 = {cash.Value}
	DataStore:SetAsync(player.userId, Save1)
	
end

end)

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

local Save2 = {player.leaderstats.cash.Value}
DataStore:SetAsync(player.userId, Save2)

end)

It looks like there are several problems with this code, does the output report any errors? What happens when you play?

You saved it in a table, so you basically have to do:

cash.Value = data[1]

when they join the game. I’d also like to point out that it should be UserId and not userId

There are 2 problems with your code. The first problem is that it should be player.UserId, not player.userId. Property names are case sensitive.
image

The reason why it doesn’t error is because it is inside a pcall.

Also, you saved a table, so to set the value of the cash, you should do cash.Value = data[1] instead.

P.S.
I recommend renaming your IntValue to “Cash” instead, as it is common practice to keep leaderstats capitalized.
Also, your code is incorrectly formatted, the first and last lines are outside of the code block.

There are no errors when I play, even if I remove the pcall function.

I changed everything you suggested, still doesn’t work! I tried everything!

I did everything that you gave me a feedback on, nothing still saves!

Where are you changing the cash value from? If it is changed from the client, it won’t replicate to the server, meaning that the change will not save, and will only be visible to yourself.

If you are testing in studio, make sure that API Access is enabled.

My edited script:

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“JustDoIt”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "cash"
cash.Parent = leaderstats

local data = DataStore:GetAsync(player.UserId)

if data then
	cash.Value = data[1]
else
	local Save1 = {cash.Value}
	DataStore:SetAsync(player.UserId, Save1)
end

end)

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

local Save2 = {player.leaderstats.cash.Value}
DataStore:SetAsync(player.UserId, Save2)

end)

Do you have EnableAccessToAPIServices enabled in the game settings for studio? Are you manipulating the data on the client?

Thanks once again but I am experienced in Data-store and I have tried literally everything even turning on access to API and changing values in Server mode. One thing to NOTE, I had been working on global leaderstats and I exceeded the datastore limits quite excessivly but Its been weeks from then and data still does not save!

I already tried doing that man!

Maybe you should also implement a BindToClose function to save values before the server closes, although the request will be throttled:

game:BindToClose(function()
    for _,v in pairs(game.Players:GetPlayers()) do
         local save = {v.leaderstats.cash.Value}
         DataStore:SetAsync(player.UserId, save)
    end
end)
2 Likes

You should re-add the pcalls, and also. Currently, if datastores fail, you will overwrite any existing data.

Use the following code:

local data
local ran, err = pcall(function()
    data = DataStore:GetAsync(player.userId)
end)

if ran then
    if data then
        cash.Value = data[1]
    else
        --save data, use pcalls too
    end
else
    warn(err)
    --handle datastore failure
end

And as mentioned above, it is also possible that your server is shutting down before the data can be saved. BindToClose should fix this.

I have tried everything, But one piece of data saved but unfortunately now it doesn’t save.

This actually works Iv tested it a few times, but anyway to make it save quicker? It takes around 30 secs to just stop testing and return back to the editing mode

@HARD_PHASER I don’t think you can make the request be handled quicker
@coldgeckos How does that help at all? It’s just variables that serve no purpose in the script at all

I see a couple problems with the script, but changing them wouldn’t make a difference. Here is a script to save data stuff. The currency I use is points, but you can just change it.

game.Players.PlayerAdded:connect(function(p)
	local ODS = game:GetService("DataStoreService"):GetDataStore(p.userId)
	for i = 1,#Stats do
		ODS:UpdateAsync(Stats[i], function(old)
			return old or 0
		end)
	end
	local l = Instance.new("Model", p)
	l.Name = "leaderstats"
	for i = 1,#StatsNotToSave do
		S = Instance.new("NumberValue", l)
		S.Name = StatsNotToSave[i]
	end
	for i = 1,#Stats do
		S = Instance.new("NumberValue", l)
		S.Name = Stats[i]
		if Auto_Load then
			S.Value = ODS:GetAsync(Stats[i])
		end
		if Auto_Save then
			S.Changed:connect(function()
				ODS:SetAsync(Stats[i], S.Value)
			end)
		end			
	end
end)

if Save_On_Leave then
	game.Players.PlayerRemoving:connect(function(p)
		local ODS = game:GetService("DataStoreService"):GetDataStore(p.userId)
		for i = 1,#Stats do
			stat = p.leaderstats:FindFirstChild(Stats[i]) 
			if stat then
				ODS:UpdateAsync(stat.Name, function(old)
					return stat.Value
				end)
			end
		end
	end)
end