DataStore don't work

hi, this is my first post, u can move it anywhere

hi, im a bad scripter and i seen a youtube tutorial about datastores that was made now 4 years, my brother helped me with my script but it still don’t work, my script for Getting data:


function HiddenAddons:GetFromValues(plr,key)
	if DSS:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.GetAsync) > 0 then
		local d = DSS:GetDataStore(key..'__'..plr.UserId):GetAsync(plr.UserId)
		
      for i,v in next,d do
            print(i.." = "..v.." (G)")
      end
      

		if d[1] == nil then
			return table.create(1)
		elseif d[1] ~= nil then
			return d
		end
	end
end


the script to set data:


function HiddenAddons:SetToValues(plr,key,value) 
	AllDNNS[key..'__'..plr.UserId] = value
	wait(1)
	for i,v in pairs(AllDNNS) do
		warn(i.." = "..v)
	end
	
	if DSS:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync) > 0 then
   
		DSS:GetDataStore(key..'__'..plr.UserId):SetAsync(plr.UserId,AllDNNS)
        wait(1)
		local d = DSS:GetDataStore(key..'__'..plr.UserId):GetAsync(plr.UserId)

		for i, v in next, d do -- this works
			warn('(T): '..i..' = '..v) -- this works
		end

	end
end


I recommend reading through this - it gives you all the information regarding data stores. :slight_smile:

this isn’t the problem, the problem is the Getting test in the saving work and the getting in the Get don’t work, what is the problem?

What exactly isn’t working? Do you get any errors? How often are you saving the data because you could be hitting the data store limits(are you saving when the player leaves or when their stats change ect)? Are you running this from a script or a LocalScript? Do you have studio API services enabled if you are attempting to save in studio?

There isn’t anything that I can immediately see wrong with this script that could be causing your script not to fully work. You could be hitting your data store limits. Other than that the only things that I can see wrong is that you aren’t properly using data stores:

First off you shouldn’t give each player their own data store from the :GetDataStore method as shown below. The way you are doing it will cause you to hit your data store limits very quickly because each player could end up having multiple data stores:

DSS:GetDataStore(key..'__'..plr.UserId):SetAsync(plr.UserId,AllDNNS)

Instead you should only use one data store from the :GetDataStore method so that everyone can use it instead of making one for each player. In most cases it is better practice to save the players data under one big table to limit the amount of data store calls you do. Here is an example of what I mean:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Data") -- DataStore for everyone

DataStore:GetAsync(Player.UserId) -- Gets a table of the players data

local Data = {   -- Table of the players data
	Coins = 20,
	Level = 40,
	XP = 200
}

DataStore:SetAsync(Player.UserId, Data) -- Sets the players data

The next you you should do is use pcall to catch and track your errors. Data store calls occasionally error at no fault of the developer so it is recommended you wrap all your data store calls in pcalls. Here is a little example:

local Success, Error = pcall(function()
	DataStore:SetAsync(Player.UserId, {})
end)

if not Success then
	print("Data loaded")
else -- Data did not load correctly
	print(Error) -- Prints the error
    -- Do something to try to get the data again
end
3 Likes

to respond at all u say: it don’t error but it still don’t return nothing, i save the values of the player when he leave and another after 11 seconds (ik i dhoulkd put 6) and i don’t hit the limints.a ModuleScript, it is enabled

i changed what u said

i arleady use tables

and i changed with what u said

i will edit the Reply if it will work

hi, i changed my saving script but still don’t work


function HiddenAddons:SetToValues(plr,key,value) 
	AllDNNS[plr.UserId..'-'..key] = value,table.getn(AllDNNS)
	
	if DSS:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync) > 0 then
        warn('a')
		DSS:GetDataStore(ALLDNS_Save):SetAsync(plr.UserId,AllDNNS)
		
		warn('a2')
		
		warn(AllDNNS[1]) -- nil instead of 100,1
		
	else
		wait(10)
		
			if AllDNNS == nil then
			   AllDNNS = {}
			end
		DSS:GetDataStore(ALLDNS_Save):SetAsync(plr.UserId,AllDNNS)
	end
end

there isn’t errors

Can you try wrapping the

DSS:GetDataStore(ALLDNS_Save):SetAsync(plr.UserId,AllDNNS)

part with print()? Should look like this;

print(DSS:GetDataStore(ALLDNS_Save):SetAsync(plr.UserId,AllDNNS))

When you’re done, post whatever it prints.

it prints nil 30 charssssssssss

Just a note that Datstores will not work on Team Create servers.