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:
1 | local 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:
1 | for i,v in ipairs (Plr.Backpack:GetChildren()) do |
2 | table.insert(Inventory, i, v.Name) |
1 | for _, v in ipairs (Plr.Backpack:GetChildren()) do |
2 | table.insert(Inventory, v.Name) |
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!