Not asking so much for a script but just guidance on what would be a way to script an inventory for each player that can be saved in datastores. What I've done previously is have a value for every item in the game inside the player and change the value if it was purchased or not. This doesn't seem good if you have a game with tons of items and don't want over 100 values in each player. Then I was thinking maybe some sort of string value with every item you owned, but I don't know how good that would be either. Thanks
I love this question :D
What you are looking for are tables (dictionaries specifically). For every player that joins, you can use that player as a key in a dictionary (called PlayerData or something), and then store their data under that key. Then, when you want to retrieve the data stored under that player, you can index the dictionary with the player as the key.
For example, this script would store a table under every player that joins in a dictionary, and then remove that table when they leave:
local PlayerData = {} game.Players.PlayerAdded:Connect(function(Plr) PlayerData[Plr] = {} end) game.Players.PlayerRemoving:Connect(function(Plr) PlayerData[Plr] = nil end)
With this method, each player will have their own unique table stored under their player object. You can use this table as another dictionary to store all aspects of data for the player. For example, if they have a currency and an inventory, you could set it up like this:
local PlayerData = {} game.Players.PlayerAdded:Connect(function(Plr) PlayerData[Plr] = { Currency = 0; --default the currency to 0 Inventory = {}; --default to an empty inventory } end)
Then, to add an item in the players inventory later in the script, you could do something like this:
local Inventory = PlayerData[Plr].Inventory --player's inventory table table.insert(Inventory,"item name here")
This will insert the string "item name here" into the player's inventory array. So with this method, you could store all the names of the items that the player owns. Another bonus about using an array is that the items will maintain their order, letting the players save organised inventories.
Similarly, if you wanted to give them 5 of the currency, you could go:
PlayerData[Plr].Currency = PlayerData[Plr].Currency + 5
Then, to print every item in their inventory, you could go:
for ItemNum,Item in pairs(PlayerData[Plr].Inventory) do print(ItemNum,Item) end
Now, a great thing about this is that it works super easily with datastores. On PlayerRemoving, you can datastore their whole data table in one go, easily saving all their information. Then, on PlayerAdded, you can first check if they have saved data. If they do, you can put it in the PlayerData table; otherwise, give them a blank data table. For example:
local DS = game:GetService("DataStoreService") local PlayerDataStore = DS:GetDataStore("PlayerData") local PlayerData = {} game.Players.PlayerAdded:Connect(function(Plr) local ExistingData = PlayerDataStore:GetAsync(tostring(Plr.UserId)) if ExistingData then PlayerData[Plr] = ExistingData else PlayerData[Plr] = { Currency = 0; --default the currency to 0 Inventory = {}; --default to an empty inventory } end end) game.Players.PlayerRemoving:Connect(function(Plr) PlayerDataStore:SetAsync(tostring(Plr.UserId),PlayerData[Plr]) PlayerData[Plr] = nil end)
(Keep in mind that your save data still has to abide by the Datastore limitations)
Also remember that if you are saving on PlayerRemoving, you will have to delay the server shutting down so that the last player who leaves has time to save their data:
game:BindToClose(function() wait(3) end)
That may have been a bit of a tangent, but I hope it helps!
Locked by User#21908 and mattscy
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?