I’ve created an inventory script that allows the player to pick up and drop items, these items are stored in IntValues within a folder inside the player. How would I go about saving and loading each of those values?
Create a table and add those items in the table and save the table in DATASTORE.
I’ve answered to something similar there and you shouldn’t have to change the script too much
I just decided to write up a little script for you to achieve this.
How would I go about saving both the name of the value and the value of it?
Good point, that makes things a little trickier (storing a dictionary of tables) but here:
I’m getting this error for just under game:BindToClose(function())
ServerScriptService.Script:40: invalid argument #1 to ‘pairs’ (table expected, got Instance)
Uh, that’s not the script I posted.
I changed a few things to suit the Inventory I have, it’s inside a folder called Inventory inside of player.
I had to remove the “local player = players.LocalPlayer or players.PlayerAdded:Wait()” because it was saying “Player.PlayerAdded” isn’t apart of player.
Oh, that was just a mistake on my part, should have been players.PlayerAdded
instead.
Add the bits again to this script & repost it. The error was in relation to something you had added, so I’ll likely need to see how everything is organised on your end.
So in this one I’ve just changed storage to where my inventory is.
Can I see how everything is organised your end?
Yeah of-course!
Have a wee look, sorry for all the bother.
Oh never mind, I see the change at the top. I’ll get to it shortly.
Just noticed everything had “player.PlayerRemoving” too it should have been “players.PlayerRemoving” I’ve fixed that too.
Should Weight be inside inventory?
No weight can stay inside PlayerData. It’s not saving/loading for me any ideas why?
Okay, so the DataStore is working, now you just need to make it so that the inventory stuff is correctly given to the user.
I’ve sent you a message containing a modified .rbxl file.
local DSService = game:GetService("DataStoreService")
local DS = DSService:GetDataStore("DataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local storage = player:WaitForChild("PlayerData"):WaitForChild("Inventory")
local load = DS:GetAsync("User_"..player.UserId) or {} --load data from datastore
if type(load) == "table" then --expected type of value
for i, v in pairs(load) do --cycle through dictionary of tables
for name, value in pairs(v) do --cycle through those tables
local intVal = Instance.new("IntValue")
intVal.Name = name
intVal.Value = value
intVal.Parent = storage --load all the intvalue instances back into storage
--perform other code
end
end
else
load = {} --empty table indicates no intvalues
--perform other code
end
end)
players.PlayerRemoving:Connect(function(player)
local data = {}
local storage = player:WaitForChild("PlayerData"):WaitForChild("Inventory")
for i, v in pairs(storage:GetChildren()) do --cycle through all of the intvalue instances
if v:IsA("IntValue") then
local nameAndVal = {} --make new table to store name and value
nameAndVal[v.Name] = v.Value --insert name and val into table as single item
table.insert(data, nameAndVal) --insert the value of the intvalue instance into the table
end
end
local res = DS:SetAsync("User_"..player.UserId, data) --save data to datastore
--perform other code
end)
game:BindToClose(function() --bindtoclose executes whenever the server shuts down
for i, player in pairs(players:GetPlayers()) do
local data = {}
local storage = player:WaitForChild("PlayerData"):WaitForChild("Inventory")
for i, v in pairs(storage:GetChildren()) do --cycle through all of the intvalue instances
if v:IsA("IntValue") then
local nameAndVal = {} --make new table to store name and value
nameAndVal[v.Name] = v.Value --insert name and val into table as single item
table.insert(data, nameAndVal) --insert the value of the intvalue instance into the table
end
end
local res = DS:SetAsync("User_"..player.UserId, data) --save data to datastore
--perform other code
end
end)