Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script to save items in backpack after death...?

Asked by 2 years ago
Edited 2 years ago

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)

0
hi legobuddie1 0 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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.

  1. 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.

  2. 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)

0
It didnt work..But thanks! I understand alittle bit better but im still stuck. Everything kinda stayed the same other then the fact that there is now a stored items folder in my player, Each time my character resets the items still Disappear.Idk if i made myself clear but I wanted it so that when the player dies, his tools in his backpack save. lpyrite 4 — 2y
0
Oh, apologizes it didn't work for you. It worked for me though, I tested the code before I put it here. It works perfectly fine. You might've messed something up. MAKE sure to completely get rid of everything in the script AND replace it with the code I MADE. If you need more help contact me on discord. LoxiGoose#9553 DocGooseYT 110 — 2y
0
ALSO, DON'T COPY THE CODE FROM THE FIRST CODE BLOCK I DID. I just realized that I don't think I made it clear enough that, that was to show you how to do code blocks on Scriptinghelpers. THAT CODE WILL NOT WORK. ONLY COPY THE CODE FROM THE SECOND CODE BLOCK. DocGooseYT 110 — 2y
0
I used the second code block, still nothing. No idea why, I spawn in my items from a gui, maybe thats why. lpyrite 4 — 2y
View all comments (2 more)
0
Depends, can you add me on discord so we can fix this faster? LoxiGoose#9553. If not, I don't really know how to help you because I need more info such as vids and screenshots. DocGooseYT 110 — 2y
0
alright, I'll add you. lpyrite 4 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

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.

0
Could you show how to arrange it? Im not sure I follow. How would I put and arrange the code lpyrite 4 — 2y

Answer this question