Please be more describing, im new to lua. I want the items that the player has in their backpack to save, not the starter pack. This is my current script.
game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) char:WaitForChild("Humanoid").Died:Connect(function() plr.Backpack:ClearAllChildren() for i,v in pairs(plr.Backpack:GetChildren()) do if plr.Backpack:FindFirstChild(v.Name) then return end v:Clone().Parent = plr.Backpack end end) end) end)
Howdy there! Hope you're doing great and welcome to Lua! (Ignore that awkward greeting, I'm having a tough time trying to greet people in this website haha)
Anyways before we get started, next time you ask questions on this website MAKE sure you put your code in a Code Block. Otherwise it looks really unorganized, hard to read and hard for us to help you!
It should look like this (To use a Code Block, when asking your question there should be a Lua icon next to the other text icons)
game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) char:WaitForChild("Humanoid").Died:Connect(function() plr.Backpack:ClearAllChildren() for i,v in pairs(plr.Backpack:GetChildren()) do if plr.Backpack:FindFirstChild(v.Name) then return end v:Clone().Parent = plr.Backpack end end) end) end)
Alright now that we've gotten that out of the way.. I did a bit of experimenting to make your code more efficient and have left comments for you to help understand the code! OH and I did a change to your code making it also save the item the player is holding if that's fine by you.
game.Players.PlayerAdded:Connect(function(plr) local StoredItems = Instance.new("Folder") -- Makes a folder to store the player's items when they die. StoredItems.Name = "StoredItems" -- Changes name StoredItems.Parent = plr -- Parents the folder to the player plr.CharacterAdded:Connect(function(char) local AmountOfStoredItems = StoredItems:GetChildren() -- Get the amount of Items in their StoredItems folder. local humanoid = char:WaitForChild("Humanoid", 10) -- Wait for their humanoid to appear, the 10 is a timeOut number for if the humanoid never appears. So basically it'll check like 10 times after it's done waiting. if #AmountOfStoredItems > 0 then -- Check if the StoredItems folder has more than 0 items. for i, Item in pairs(AmountOfStoredItems) do -- Gets all the items from StoredItems folder. if plr.Backpack:FindFirstChild(Item.Name) then -- Checks if it somehow already finds the same Item inside of the Backpack or Character. Item:Destroy() -- If so, then destroys itself. else Item.Parent = plr.Backpack -- If not, then parent it to the Backpack. end end end if humanoid then -- Checks if the humanoid is nil or not. humanoid.Died:Connect(function() local Tool = char:FindFirstChildWhichIsA("Tool") -- Attempts to find a tool inside the chr. if Tool then -- Checks if there was a tool. Tool.Parent = StoredItems -- Parents it to StoredItems folder. end for i, Item in pairs(plr.Backpack:GetChildren()) do -- Gets all the items in the backpack. Item.Parent = StoredItems -- Parents the item to StoredItems folder. end end) end end) end)
I would like to also explain some things that were unneeded in your code that you put and some things you didn't understand.
For starters, you cleared the player's backpack when they died AND then checked their backpack to see if it had items. Obviously this wouldn't work because you already cleared their items.
Second, checking if the player has the item that's being looped through IN the same table wouldn't work.
Third, returning backs out of the entire function which makes it not run until after you've died again.
Fourth, you cloned the items in the backpack.. to the backpack again?
I personally didn't really understand why you did these but I do have a guess and can help you out.
The first one personally didn't make sense to me as to how you put this here, I guess you just misread your code or something which is okay. It happens all the time to coders.
For the rest of your mistakes, I believe you tried to bring their items back WHILE they died. The problem with this is, THEY DIED . If you bring back the items and clone them back while the person is dead... that won't work. That's why I made a folder in my new code and transferred all the items in the dead person to this folder. Then when they respawned, I switched the parents back to the person.
Hope this really helped you! I also hope you get better at Lua, have a great time! If you need any more help, feel free to comment!
(ALSO I get if you don't understand the code since you're a beginner, that's PERFECTLY fine! I'm actually trying to learn C# and use Unity atm and I'm just lost haha. So please COMMENT if you need help)
You clear everything in the backpack BEFORE you cloned the tools, if you do it the other way around it should work. Also you should add a wait until the humanoid is alive again.