Hello,
I thought of making a simple-ish example of saving a table to a datastore before going on to do the good stuff.
Could someone tell me what I am doing wrong here? - It never prints out anything no matter how much I rejoin.
Yes, I have API settings enabled etcetc.
local DS = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
local table1 = {Adam = "Sandler", Lionel = "Messi"}
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.userId
local savetable = table1
local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
savetable.value = GetSaved[1]
else
DS:GetAsync(plrkey, table1)
print(GetSaved[1].Lionel) --Messi
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DS:SetAsync("id_"..plr.userId, table1)
end)
local DS = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
local table1 = {Adam = "Sandler", Lionel = "Messi"}
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.UserId
local savetable
local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
savetable = GetSaved
print(GetSaved.Adam, GetSaved.Lionel)
else
DS:SetAsync(plrkey, table1)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DS:SetAsync("id_"..plr.UserId, table1)
end)
To point out the errors in the op:
It should be UserId, and not userId
You shouldnât do GetSaved[1] because the table is not saved as an array - itâs a dictionary. If you wanted to correctly use GetSaved[1], the dictionary should be saved as an array when youâre saving: DS:SetAsync("id_"..plr.UserId, {table1})
Tables do not have a value
Iâd also suggest that you use BindToClose, because probably the server closes before it gets the chance to save the data:
game:BindToClose(function()
for _,plr in pairs(game.Players:GetPlayers()) do
DS:SetAsync("id_" .. plr.UserId, table1)
end
end)
Yes, it occurs because there is a limit to how many requests can be sent to the same key at a time: each request should have a 6-second cooldown inbetween.
Though, this isnât really a problem, as the datastore request will eventually be handled and taken once the request queue has space, but if itâs filled then it will not handle the requests, but I donât think itâll happen
So to test it, I added two remote functions which change âtable1.Adamâ accordingly. :
(They are fired when a text button is clicked)
The problem is that whenever these remote functions are fired and âtable1.Adamâ changes it works, but whenever you join and you donât click on any buttons to change the âtable1.Adamâ, it returns back to âSandlerâ.
Basically it doesnât persist unless I change it.
Itâs probably because itâs not saving at all, and the line you are questioning seems to reset it because GetSaved is nil. You could try putting a print under the GetSaved variable to see if it gives nil or not.
The line in question is supposed to save the data when the player doesnât have any data saved in the datastore
That will only work until you change the value once, since the value is no longer âSandlerâ .
i.e if you change the value from âSandlerâ it will keep returning nil after.
DS:UpdateAsync(playerkey, function(oldValue)
if oldValue.Adam == oldValue.Adam then --if it's equal to the old value of Adam
return table1
else
return oldValue -- Old table
end
end)