I am trying to achieve getting it to save data in my inventory. I tried many things but nothing has solved my problem i even went to other topics and couldn’t find my answer.
My question that im asking is how would i save a ui to a inventory egg like a imagebutton to the inventory frame so when i join im see it there im new to datastore2 so plz be more forward about your answer.
I usually have all of the players data in a table, then when the player leaves set their data.
I used datastore2 for a bit, and it’s useful. Here is roughly something I do that goes along with your situation, though, having not used datastore2 for a while and not for long, might be a little wrong.
local DataStore2 = require(game.ServerScriptService.MainModule)
local PlayerDataStore = {}
local DefaultData = { -- players default data table
Inventory = {
["Chaos Canyon Suger Egg"] = 0
},
}
local function onPlayerAdded(player)
-- player added stuff
local PlayerData = DataStore2("PlayerData", player) -- gets whole data
PlayerDataStore[player] = PlayerData:Get(DefaultData) -- gets data and if no data gets default
DataStore2("PlayerData", player):Set(PlayerDataStore[player]) -- sets data
end
local function onPlayerRemoved(player)
if PlayerDataStore[player] then
local PlayerData = PlayerDataStore[player]
DataStore2("PlayerData", player):Set(PlayerDataStore[player])
end
end
-- there is probably other ways to cache/set data with datastore2 but not sure
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoved:Connect(onPlayerRemoved)
You can use remote events and remote functions to get the players data to the client.
Hope this serves as an example or helps you in any way!
Another question how would i change the value of whats in the table for example if you have a value of one egg in table and it goes in the inventory gui
Well to get the players data just do local PlayerData = PlayerDataStore[player]
to access inventory PlayerData.Inventory
and to change something in inventory PlayerData.Inventory[item]
and just set the value of the item
Unrelated, but for the purpose of data conservation, I’d recommend making your keys smaller, and having some sort of reference to that information in a module or something.
such as renaming the key CCSE, and having a reference in a module somewhere that lets you know that CCSE is Chaos Canyon Super Egg.
Yeah he can always use an id for the key and have a module to store all the items, but, as of now it wouldn’t hurt and he can always organize and conserve later himself.
Although this may not answer your question, i recommend using number id’s for items. This can be achieved by having module scripts for each item, this way when storing the item count, it’ll be much smaller in usage, as well you can rename items without breaking peoples data, also helps with localization
So example, instead of
Inventory = {
["Chaos Canyon Suger Egg"] = 0
},
Do something along the lines of
Items = {
[1] = {
name = "Chaos Canyon Sugar Egg";
id = 1; -- though you dont need this since you can use the index, i generally do for item ids.
}
}
local UserData = {
Inventory = {
["1"] = 0
}
}
Also a side note to remember, storing ids as string index is easier to deal with, as having say, item 5 and 9 will error if you use number indexes when trying to replicate to client