Hi! I want to know how I can implement this idea into my scripts to save a player’s inventory:
I want to Create an initially empty table that fills with a bunch of strings each representing an item.
(I want to try to use a table, as then I will not have to create a value in the script every time I make a new item. There will be lots of items in the game, so I was thinking I could just have the item name be added to the table when unlocked.)
[ITEMS ARE SIMPLE: JUST A BUNCH OF SWORDS YOU CAN BUY. I only need the NAME of the sword saved to the table.]
I need help implimenting this into my current code. Thanks!
Current Code: (I use Suphi’s datastore module)
Script 1: “DataSaving”
local datastoremodule = require(game.ServerStorage.DataStoreModule)
local template = {
--all my in game values are here for data saving. Deleted them cause they dont rly matter
}
local function StateChanged(state, datastore)
while datastore.State == false do
if datastore:Open(template) ~= "Success" then task.wait(6) end
end
end
game.Players.PlayerAdded:Connect(function(player)
local datastore = datastoremodule.new("Player", player.UserId)
datastore.StateChanged:Connect(StateChanged)
StateChanged(datastore.State, datastore)
end)
game.Players.PlayerRemoving:Connect(function(player)
local datastore = datastoremodule.find("Player", player.UserId)
if datastore ~= nil then datastore:Destroy() end
end)
Script 2: “Stats and Values”
local datastoremodule = require(game.ServerStorage.DataStoreModule)
local keys = {--values from first script}
local keys2 = {--some other values from first script. (i separated them for game reasons)}
--ADD THE NAME OF A NEW CHARACTER TO KEYS LIST THEN ALSO UNDER PLAYERADDED
local function StateChanged(state, datastore)
if state ~= true then return end
for index, name in keys do
datastore.PLRSTATSS[name].Value = datastore.Value[name]
end
for index, name in keys2 do
datastore.PLRCARDSS[name].Value = datastore.Value[name]
end
end
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "PLRSTATSS"
folder.Parent = player
-------------------------------------------------------------------------------------------
local tokens = Instance.new("IntValue")
tokens.Name = "Tokens"
tokens.Parent = folder
--just an example of how for every key in "Keys" theres a value under plrstats and plrcards
local cardfolder = Instance.new("Folder")
cardfolder.Name = "PLRCARDSS"
cardfolder.Parent = player
local myc = Instance.new("StringValue")
myc.Name = "MyCard1"
myc.Parent = cardfolder
------------------------------------------------------------------------------------
local datastore = datastoremodule.new("Player", player.UserId)
datastore.PLRSTATSS = folder
datastore.PLRCARDSS = cardfolder
datastore.StateChanged:Connect(StateChanged)
end)
Other scripts (my method used for whenever I want to update a value. (eg. a player unlocks something)
dsmodule = require(game.ServerStorage.DataStoreModule)
local plr = "(However I end up getting the Player Instance)"
local data = dsmodule.find("Player", plr.UserId)
if data == nil then return end
if data.State ~= true then return end
data.Value.Exp += 100
data.PLRSTATSS.Exp.Value = data.Value.Exp