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

Why does my script give me my tools but not the children of the tool?

Asked by 5 years ago

After going server-side with my backpack save after death script I found another issue with it, It kinda works now but I have an issue that my script clones all the items in my backpack back when I die, but it doesn't clone the children of the tools

game:GetService("Players").PlayerAdded:Connect(function(player)
    repeat wait() until player.Character
    local char = player.Character

    char:WaitForChild("Humanoid").Died:Connect(function()
        local tools = player.Backpack:GetChildren()

        player.CharacterAdded:Connect(function()
            wait(1)

            for i,v in pairs(tools) do
                if v:IsA("Tool") then
                    local clonedTool = v:Clone()
                    clonedTool.Parent = player.Backpack
                end
            end
        end)
    end)
end)
0
Any purpose for a tool to be inside of a tool? Can't they just be seperate? Use a folder if you're organizing things inside it. xPolarium 1388 — 5y
0
no i have things inside my tools HilyrHere 79 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

Here's a nifty solution for you. While I don't know what exactly was wrong with your original code, this modified code will do what you're hoping for.

Essentially, this will rename tools which you own to your tool, and then move them to ServerStorage, transporting them back to you after you respawn. If you need tools to be named something specific and this will interfere with something, I'm sure you can come up with a different method to sift through the tools in ServerStorage - such as a StringValue inside each tool.

I also made it so it will return the tool you are holding as well - which I moved by using FindFirstChildOfClass() inside of Character.

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(Character)
         for _,v in pairs(game.ServerStorage:GetChildren()) do
            if string.sub(v.Name, 1, string.len(player.Name)) == player.Name then
                v.Parent = player.Backpack
            end
         end

        Character:WaitForChild("Humanoid").Died:Connect(function()
            for _,v in pairs(player.Backpack:GetChildren()) do
                v.Parent = game.ServerStorage
            end
            if Character:FindFirstChildOfClass("Tool") ~= nil then
                Character:FindFirstChildOfClass("Tool").Name = (tostring(player).."'s tool")
                Character:FindFirstChildOfClass("Tool").Parent = game.ServerStorage
            end
        end)
     end)

    player:WaitForChild("Backpack").ChildAdded:Connect(function(tool)
        tool.Name = (tostring(player).."'s tool")
    end)
end)
0
Oh my god, finally a fix to this problem I had for a while, cheers xoxo HilyrHere 79 — 5y
Ad
Log in to vote
-2
Answered by 5 years ago

you have to do v:getchildren() its the only way

0
as a fix to this myself I'd say I'd do tools = player.Backpack:GetDescendands and then loop through the descendants, check what it's parent is and then parent it idk, but i have no idea why this doesnt work HilyrHere 79 — 5y

Answer this question