Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Trying To Save A Table Onto A Data Store Is Not Saving?

Asked by 3 years ago
Edited 3 years ago

Basically I Save The Items In a Backpack And Then When They Rejoin It Gives Them Those Items But A Problem Is That When They Return The Table Is Blank Even Though When They Leave The Table Does Save

Here Is My Script

01local DataStore = game:GetService("DataStoreService")
02local Data = DataStore:GetDataStore("MS_PopItTrading_Inventory_Data")
03 
04game.Players.ChildAdded:Connect(function(Plr)
05    local Key = Plr.UserId
06 
07    if Data:GetAsync(Key) == nil then
08        local Inventory = {"Blank"}
09        Data:SetAsync(Key,Inventory)
10        print(Data:GetAsync(Key))
11    else
12        for i, value in pairs(Data:GetAsync(Key)) do
13            local Pop = game.Lighting.PopIts:FindFirstChild(value)
14            if Pop then
15                local Clone = Pop:Clone()
View all 40 lines...

1 answer

Log in to vote
1
Answered by
Wiscript 622 Moderation Voter
3 years ago

There are a few things that I recommend.

DataStore strings should be keys DataStore keys must be strings. I recommend you even do something like:

1local Key = "Inventory_" .. tostring(Plr.UserId)

Limit your use of GetAsync() Next point I'd like to make is the amount of times that you're calling GetAsync(). There are certain limits which can be easily exceeded if you are not careful. Make use of variables, as they're extremely useful. The player data you retrieve at one point is highly unlikely to change less than a second later.

Use the PlayerAdded event I noticed you used game.Players.ChildAdded. This works, but it's not optimal, as ROBLOX offers a much more secure event: Player Added

Simplify your table.insert() On line 30 of your code, I notice how you make use of a variable Amount to add items to the list. This of course works fine, but you have two simpler options:

1for i,v in ipairs(Plr.Backpack:GetChildren()) do
2    table.insert(Inventory, i, v.Name)
3end
1for _, v in ipairs(Plr.Backpack:GetChildren()) do
2    table.insert(Inventory, v.Name)
3end

The last option is in my opinion preferable, as if the second argument in table.insert() is your value, the function will simply append the item to the table. You may have also noticed I changed pairs to ipairs. Although the difference is small, ipairs has been proven to be roughly 20% faster. You can read a great answer by sleitnick on how loops work here!

Good luck!

Ad

Answer this question