Problems with DataStore

Problems with Datastore

Recently I have been trying to make a game with saved stats, but none of the scripts I try work, so I made a small test place, and still the stats don’t work.

I have tried changing several things in the scripts, and I even made different datastore scripts but none of them work.

I found several different supposed solutions on the DevForum, yet none of them work.

Media

My Stats Script

My Datastore Script

Video proof of the Datastore not working

Scripts

Lua Code Block of my Stats Script

game.Players.PlayerAdded:Connect(function(plr)
	local statscontainer = Instance.new("Configuration")
	local lvl = Instance.new("NumberValue")
	local CR = Instance.new("NumberValue")
	local Rank = Instance.new("StringValue")
	
	statscontainer.Name = "PlrStats"
	statscontainer.Parent = plr
	
	lvl.Name = "Level"
	lvl.Parent = statscontainer
	
	CR.Name = "Credits"
	CR.Parent = statscontainer
	
	Rank.Name = "Rank"
	Rank.Parent = statscontainer
	Rank.Value = "Lorem Ipsum"
end)

Lua Code Block of my DataStore Script

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("savestats")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrKey = "id_"..plr.UserId
	local save1 = plr.PlrStats.Level
	local save2 = plr.PlrStats.Credits
	local	save3 = plr.PlrStats.Rank
	local getSave = DataStoreService:GetAsync(plrKey)
	if getSave then
		save1.Value = getSave[1]
		save2.Value = getSave[2]
		save3.Value = getSave[3]
	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value}
		DataStoreService:GetAsync(plrKey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DataStoreService:GetAsync("id_"..plr.UserId, {plr.PlrStats.Level.Value, plr.PlrStats.Credits.Value, plr.PlrStats.Rank.Value})
end)

– NOTE –

I only listed the datastore script that works the most for me, and I did not take screenshots of the other scripts. If you want the other scripts I used, I will have to go back and find them.

1 Like

Make sure to enable the studio to have access to API Services (If not you can not use datastore)
Data Stores.

    game:BindToClose(function()
        -- your data saving code
    end)

(This also caused some of your issues)I have found a fix. Simply put your saving part of the script into this. The issue was that the player was leaving the server and it closed before saving. to fix it use DataModel | Roblox Creator Documentation so the data should be saved before the server shut down.

Main solution: Also you were using GetAsync to try to save data (GetAsync only Tell you what the data is). SetAsync saves the data. I hope this helps.

game.Players.PlayerRemoving:Connect(function(plr)
	DataStoreService:GetAsync("id_"..plr.UserId, {plr.PlrStats.Level.Value, plr.PlrStats.Credits.Value, plr.PlrStats.Rank.Value})
end)

You’re calling GetAsync() instead of SetAsync() or UpdateAsync() here. GetAsync() attempts to find an entry with a key matching that of the one provided, it returns the value associated with that key. You’re also doing the same here too.

local NumberForSaving = {save1.Value, save2.Value, save3.Value}
DataStoreService:GetAsync(plrKey, NumberForSaving)

I made a datastore system not too long ago which handles everything for you. If you scroll all the way down you should find the resource, and the code to use it. Hope this helps:

Pls stop spam posting ur datatstore module.

oh. ok i was just trying to help people with datastores because thats something I’m good at. And most of them was leaderstats, which my module could support, so I sent it. I am also trying to figure out solutions for other datastore problems since that is what I am experienced at.

Why are you using GetAsync in player removing?

Please mark your post as solved, if it is, to avoid reviving of a thread.