What do you want to achieve? I want to make an inventory saver
What is the issue? My inventory saver doesn’t work.
What solutions have you tried so far? DevForum and Youtube.
I was making an Inventory system so that when players left or died they could still have their bow/wand. The code seems fine on my end could you please take a second (look) at it please? Thanks to anyone who read this and/or helped!
local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)
game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)
local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")
if ToolData ~= nil then
for i, v in pairs(ToolData)do
if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
ToolFolder[v]:Clone().Parent = Backpack
ToolFolder[v]:Clone().Parent = StarterGear
end
end
end
Player.CharacterRemoving:Connect(function(Character)
Character:WaitForChild("Humanoid"):UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}
for i, v in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
SaveData:SetAsync(Player.UserId, ToolTable)
end
end)
So does it not save when the player leaves the game or does the player not keep the tool once they reset? It would help if there was a more of an explanation to what the problem is.
@apoaddda My goal is to make a Inventory saver so that when the character DIES OR LEAVES THE GAME it would still give the tools that they have had previously. BTW I prefer it if you put @crazyEjai in your replies so I can answer quicker.
So this function removes the tools? (More about the CharacterRemoving function: Player | Roblox Creator Documentation on the developer hub) But basically it fires when the player dies. Could you please explain why you are using this because if a player resets/dies it automatically unequips tools from my experience.
It is supposed to do that because in my code (if done correctly) is unequipping the tools so that it goes into the backpack so that it can be cloned and put into a table which can restore the tools. Because sometimes players don’t put their tools in their backpack making it impossible to clone and store in a table.
You cannot save instances in a datastore, you will need to save the instance (Tools) Names, then duplicate them from server storage to the player’s inventory. Thats probably why your datastore is not saving the tools.
local Players = game:GetService('Players');
local DatastoreService = game:GetService('DataStoreService');
local Datastore = DatastoreService:GetDataStore('SavedTools');
Players.PlayerAdded:Connect(function(plr)
local suc, tools = pcall(function()
Datastore:GetAsync(plr.UserId)
end)
if suc then
local Tools = tools or {};
for i,v in next, Tools do
if game.ReplicatedStorage[v] then
game.ReplicatedStorage[v]:Clone().Parent = plr.BackPack;
end
end
end
end)
Players.PlayerRemoving:Connect(function(plr)
local tools = {}
for i,v in pairs(plr.BackPack:GetChildren()) do
if v:IsA('Tool') then
tools[#tools + 1] = v.Name;
end
end
pcall(function()
Datastore:SetAsync(plr.UserId, tools)
end)
end)
local toolsfolder = game.ServerStorage:WaitForChild("Tools")
Change it to wait for child because the folder may not load immediately after player joins
Btw to check if the folder have the tool names, type print(tooldata)
The table gets printed, check if the printed table have the tools name
Another thing:
Print the saved table when leaving and test in studio as its easier to check the output and in game, after you leave you cannot check table
If the both the tables, after player join printed table and saved table when leaving is same
There shouldn’t be an error but for you its there, so probably you have these wrong or incorrectly saved