Also you’re making a loop and saving the items in the loop one after the other so lets say the table looked like this Blue trail, Red trail then it will only save Red trail because it would save the Blue trail but then since Red trail is next it’ll save the Red trail to the same key and since you’re saving a Value it wont save both.
You can make a table and then add all trails to that table then save that table
Example
local TrailsTable = {}
For _,trail in pairs(TrailsFolder:GetChildren()) do
table.insert(TrailsTable,trail.Value)
end
Datastore:SetAsync(player.UserId,TrailsTable)
Also idk if this is in a server script or local script but it’ll only work in a server script
local R_Trail6 = Instance.new("NumberValue", folder2)
R_Trail6.Name = "Silver Trail"
R_Trail6.Value = ds:GetAsync(plr.UserId) or 0
this is the script that saves the players data
game.Players.PlayerRemoving:Connect(function(plr)
local Crate1Table = {}
for _,trail in pairs(plr.Inventory.Trails:GetChildren()) do
table.insert(Crate1Table, trail.Value)
end
ds:SetAsync(plr.UserId, Crate1Table)
end)
You are setting the key (plr.UserId) to v.Value every single iteration in the loop. You are overwriting the last entry with the next.
Another problem with this approach is that datastores have a limit on how many requests in a set amount of time. This means that if 10 players leave at once, the game will start to queue the data. This leaves you prone to data loss if the server crashes/etc.
You should save the data as a table. Save the Bandwith!
local data = {}
for i, v in pairs(plr.Inventory.Trails:GetChildren()) do
table.insert(data, v.Value)
end
ds:SetAsync(plr.UserId, data)
Hopefully this helps!
EDIT: Just realized someone already suggested this! My apologies. The following is still important:
Also, try not to use SetAsync every time you need to save. Use UpdateAsync() instead to prevent data loss. See this thread.
You are trying to set a (?)Value to a table. You need to unpack the table before you can use the data.
My recommendation would be to save a 2-dimensional table in the first place. This allows you to find the data with a key (trail’s name) and pull the necessary value from said table.
{{"ValueName", "Value"}, {"Value2Name", "Value"}}
You can save it like this:
local data = {}
for i, v in pairs(plr.Inventory.Trails:GetChildren()) do
table.insert(data, {v.Name, v.Value})
end
ds:SetAsync(plr.UserId, data)
To unpack this data, you can create a search function that loops through each table inside of the big table. Once you get a match, you can pull the value from said nested table.
I totally understand if this is confusing, so please don’t hesitate to ask if you have any questions.
No what I do is, I make a value and call it the name of a trail, if the value is more then 0 it will show a ui, when the ui is pressed it gives the player a trail
Like I said above, the same principle applies. You can save a big table with item as a table within. The name is the key (Silver Trail/Evil Trail/etc.) and its value as the value. This is important because when you pull the data you need to make sure the value of the monkey trail is exactly what it is supposed to be.
When you loop through and save, there is a chance that it will save out of order. That is why this is important.
EDIT: Now that I’m thinking about it, you can use dictionaries instead.
{[“Monke Trail”] = 0, [Rainbow Trail] = 0}
Correct! In my mind this should work, I’m not able to test it currently haha. Make sure that your trails inventory has been populated before you call the load function, or it will throw an error.