You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? this script works by saving the tools i want to a folder on player added and playerremove.
What is the issue? The issue is some of the tools are potions and they destroy after using them. If a player drinks a potion, then dies, he has the potion back because the script does not account for players death or respawning. If a player dies he is playeradded to the game again, which loads the last inventory save, which was from the last time he quit, not the last time he died or respawned.
What solutions have you tried so far? I have tried to add in a check for respawning, death, health at zero, anything i can think of that will save the inventory at those checks, but nothing seems to work. Ive tried making a local script in playerscript that tries to save when they die, and that doesnt work because local scripts cannot call on the data store.
Any ideas is greatly appreciated.
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ServerStorage.ToolsFolder
game.Players.PlayerAdded:Connect(function(plr)
local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
for i, toolSaved in pairs(toolsSaved) do
if toolsFolder:FindFirstChild(toolSaved) then
toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear
end
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then
warn(errormsg)
end
end)
by the way when you use the potion, the server count instantly as -1 potion?
try
humanoid.Died:Connect(function()
-- put logic here so when they DIE the server will
--save the actual items of the inventory , even if
--they dind't even used it correctly
end)`
because, according to some logic, what i see in your script is: When the player is Added, he will grab back his data.
THAT means 2 Potions for example
when they reset, the server has no clue on what to do if they die, this is where the issue start
edit2: Also you need Remote Events, Client must warn that the SERVER that he used a potion, (reducing by 1 the amount of Potions)
you need to update the folder of the player, since he just lost a ââToolââ (Potion)
and Save instantly to the Datastore.
That will prevent (if they somehow Reset) To load the previous inventory with the Potion
Well iâm talking like that because i made my own custom Inventory and system, removed Reset system from roblox and their Inventory
local DataStoreService = game:GetService("DataStoreService")
local toolsDS = DataStoreService:GetDataStore("ToolsData")
local toolsFolder = game.ServerStorage.ToolsFolder
local function saveInventory(player)
print("Saving inventory for:", player.Name)
local userId = player.UserId
local toolsOwned = {}
for _, toolInBackpack in pairs(player.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(userId .. "-tools", toolsOwned)
end)
if not success then
warn(errormsg)
end
end
game.Players.PlayerAdded:Connect(function(player)
local userId = player.UserId
print("Player added:", player.Name)
local toolsSaved = toolsDS:GetAsync(userId .. "-tools") or {}
for _, toolSaved in pairs(toolsSaved) do
if toolsFolder:FindFirstChild(toolSaved) then
local toolClone = toolsFolder[toolSaved]:Clone()
toolClone.Parent = player.Backpack
toolClone.Parent = player.StarterGear
end
end
player.CharacterAdded:Connect(function(character)
print("Character added:", player.Name)
character:WaitForChild("Humanoid").Died:Connect(function()
print(player.Name .. " died. Saving inventory...")
saveInventory(player)
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
print("Player removing:", player.Name)
-- Save inventory when player leaves the game
saveInventory(player)
end)
i tried this using your suggestion and it did not update, i had a potion on login, drank it, and had it again if i reset myself or died.
tool.Activated:Connect(function()
-- saveInventoryEvent:FireServer() -- Save the inventory after the potion is consumed
-- the rest of the Potion logic
end)
Ill send you the fixed script since i used the same script a long time ago.
Make a âItemsFolderâ folder in serverstorage and put all of the tools you want to be able to safe in there. like this:
and put this script inside âserverscriptserviceâ
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ServerStorage.ItemsFolder
game.Players.PlayerAdded:Connect(function(plr)
local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
for i, toolSaved in pairs(toolsSaved) do
if toolsFolder:FindFirstChild(toolSaved) then
toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear
end
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
plr.Character:FindFirstChild("Humanoid"):UnequipTools()
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
in-case the script doesnt work. delete the âplr.Character:FindFirstChild(âHumanoidâ):UnequipTools()â line under the PlayerRemoving Function.