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")
StoredItems.Name = "StoredItems"
StoredItems.Parent = plr
plr.CharacterAdded:Connect(function(char)
local AmountOfStoredItems = StoredItems:GetChildren()
local humanoid = char:WaitForChild("Humanoid", 10)
if #AmountOfStoredItems > 0 then
for i, Item in pairs(AmountOfStoredItems) do
if plr.Backpack:FindFirstChild(Item.Name) then
Item:Destroy()
else
Item.Parent = plr.Backpack
end
end
end
if humanoid then
humanoid.Died:Connect(function()
local Tool = char:FindFirstChildWhichIsA("Tool")
if Tool then
Tool.Parent = StoredItems
end
for i, Item in pairs(plr.Backpack:GetChildren()) do
Item.Parent = StoredItems
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)
1 comment
hi