so i’m trying to make my roblox inventory save tools so when players join back they will still have their tools, Can anyone help?
It would works like how you would save coins, gems or level of a player. All you have to do is make a table
called inventory or whatever else you want. Then put the tools name inside and maybe put boolvalue
to say if it’s equipped or not. I am sure you’ll have all the tools in serverstorage replicatedStorage so you probably don’t have to add extra info unless you have something else to add.
as for your loading system, you need to insert the tools on character added onto the player backpack; and then just loop over the tools and serialize them and save it
This is made by using the DataStore Service.
However, since DataStore does not save instances, rather strings, integers, tables and dictionaries. it will take a while to get there.
First you need to have your tools saved in a folder. maybe in a folder in lighting.
You can store it anywhere, the only requirement is all the tools are in the same parent (folder).
Let’s make this magic work!
Create a script in ServerScriptService
Lets add the DataStore service with game:GetService(“service”) and create our datastore with :GetDataStore(“Name”).
ds = game:GetService("DataStoreService"):GetDataStore("Tools")
And now, we want to save the tools when the player leaves, right?
let’s add us a PlayerRemoving event.
A PlayerRemoving event is triggered a moment before the player leaves.
And let’s call our little player here “player” for understanding.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
end)
Let’s get whatever the player had in his backpack at the time.
We will create a what’s known as a “for loop” iterating on every item the player has.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(player.Backpack:GetChildren()) do
end
end)
Awesome! let’s create a variable to store all the item names. And then assign every name to the variable with table.insert(table, value). This variable would be called a Table.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items, item)
end
end)
Would you look at that! we are almost done.
just a few steps to do!
We are back to good ol’ DataStore! let’s save the data with our current DataStore with db:SetAsync(key, value) in which case, the key is the player’s userid and the value is the table of items.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items)
end
db:SetAsync(player.UserId, items)
end)
Okay! we are done saving the backpack. How do we load it now?
Let’s add a PlayerAdded event, this time (as the event says) every time a player joins, the event will be fired.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
end)
Now. we need to recover our data. With the DataStore, get the information by db:GetAsync(key) which we addressed last time as our player’s userid.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerdata = db:GetAsync(player.UserId)
end)
Well, we can’t really know if this player joined before or is new. so we need to add an if statement to make sure we have got a player with past experience.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerdata = db:GetAsync(player.UserId)
if playerdata then
end
end)
If the player hasn’t played the game before, playerdata should become nil which the if statement does not allow.
Now, we should’ve done this in the first place, add another variable telling the script where the tools are stored. in my case they are in game.Lighting.Tools.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
tools = game.Lighting.Tools
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerdata = db:GetAsync(player.UserId)
if playerdata then
end
end)
And finally, with that, lets add a for loop to all the items the player owns so we can add it to his backpack.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
tools = game.Lighting.Tools
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerdata = db:GetAsync(player.UserId)
if playerdata then
for i,v in ipairs(playerdata) do
end
end
end)
And it should try to find the item names in the tool storage.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
tools = game.Lighting.Tools
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items, item)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerdata = db:GetAsync(player.UserId)
if playerdata then
for i,v in ipairs(playerdata) do
if tools:FindFirstChild(v) then
end
end
end
end)
If it doesn’t exist in the storage then it wont give the player the item, but if it is, we should duplicate it and parent it to the player backpack.
db = game:GetService("DataStoreService"):GetDataStore("Tools")
tools = game.Lighting.Tools
game.Players.PlayerRemoving:Connect(function(player)
local items = {
}
for index,item in pairs(player.Backpack:GetChildren()) do
table.insert(items, item)
end
db:SetAsync(player.UserId, items)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerdata = db:GetAsync(player.UserId)
if playerdata then
for i,v in ipairs(playerdata) do
if tools:FindFirstChild(v) then
tools:FindFirstChild(v):Clone().Parent = player.Backpack
end
end
end
end)
And that’s it! finally, after endless steps we have gotten to the end result!
Please do not use this code as it has not been tested and could not work, for your sake, please try to learn lua, if you have then try to make something like it using the steps i took.
I hope you liked my explanation, i could’ve explained more but i took a long time to write it, if i have time tho i will try to update it to explain further on what my script means.
As always: builder here! back for another solution.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.