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

Cant delete tool in this script. How do you delete tool?

Asked by 3 years ago

Hello, I have made a script which detects when a player has died and removes all of their items. I also have a shop system which keep information about what the player owns, all of that information is located in game.ServerStorage.PlayerTools with each player having their own folder named after them.

here it is:

character:WaitForChild("Humanoid").Died:Connect(function()
            local Item = player.Backpack:FindFirstChildOfClass("Tool")
            print(player.Name .. " has died!")
            while wait() do
                if hum.Health == 100 then -- Waiting for player to respawn
                    player.Backpack:ClearAllChildren()

                    break
                else
                    game.ServerStorage.PlayerTools[player.Name]:FindFirstChild(Item):Destroy()
                    player.Backpack:ClearAllChildren()
                end
            end
        end)

I keep getting the error: attempt to index nil with 'Destroy'. I do not know why or understand what this means.

0
where the destroy script Blackbooks 138 — 3y
0
Oh and also for @Blackbooks question, it on line 10, it is a bit long YazaTheGreat 5 — 3y
0
The error means that the pathway used to allocate the supposed Instance returned nothing. You then attempt to call a function on said nothing. You didn't use give the Item's name when searching for it—that's your issue. Ziffixture 6913 — 3y
0
Your While loop is also redundant. Ziffixture 6913 — 3y
View all comments (3 more)
0
You should also call Humanoid:UnequipTools(), to ensure that the Item ends up in the Backpack; this will also end up with no tools on respawn as intended. Ziffixture 6913 — 3y
0
That being said, you need to use an if-statement before proceeding to verify the existence of "item", as there are cases in which the Player might not have any tools at the time. Ziffixture 6913 — 3y
0
The fix is stated in my answer. Ziffixture 6913 — 3y

3 answers

Log in to vote
0
Answered by
k3du53 162
3 years ago
Edited 3 years ago

The error means that the compiler can't find what game.ServerStorage.PlayerTools[player.Name]:FindFirstChild(Item) refers to, so it's nil.

Try printing Item and see if it exists in PlayerTools[player.Name] via the explorer.

Also, waiting for the humanoid health to be 100 doesn't work, because when a player respawns they get a new character instance (and everything in the character, like the humanoid).

local character = player.CharacterAdded:Wait() would yield the script until a new character is added and assign it to the variable 'character' (aka wait until they respawn)

0
The existence of "Item" is not the problem. Ziffixture 6913 — 3y
0
It's the source of the error. k3du53 162 — 3y
0
I just need the solution to deleting the tool from a folder in ServerStorage.PlayerTools[player.Name] YazaTheGreat 5 — 3y
0
Close. Ziffixture 6913 — 3y
Ad
Log in to vote
-1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

This is an efficient rewrite & fix to the asker's problem. The details are denoted in the post's comments.

--###----------[[SERVICES]]----------###--
local ServerStorage = game:GetService("ServerStorage")

local Players = game:GetService("Players")



--###----------[[VARIABLES]]----------###--
local ToolRepository = ServerStorage.PlayerTools



--###----------[[LOGIC]]----------###--
Players.PlayerAdded:Connect(function(Player)
    ---------------
    local Backpack = Player.Backpack
    ---------------
    Player.CharacterAdded:Connect(function(Character)
        ---------------
        local Humanoid = Character:WaitForChild("Humanoid")
        ---------------
        Humanoid.Died:Connect(function()
            Humanoid:UnequipTools()
            ---------------
            local Tools = Backpack:GetChildren()
            ---------------
            for _, Tool in pairs(Tools) do
                if not (Tool:IsA("Tool")) then continue end
                ---------------
                local StoredTool = ToolRepository[Player.Name]:FindFirstChild(Tool.Name)
                if (StoredTool)
                    StoredTool:Destroy()
                end
            end
            ---------------
            Backpack:ClearAllChildren()
        end)
    end)
end)

The solution you seek is: :FindFirstChild(Item.Name).

0
This script rework doesn't work. Firstly it doesn't even get the children of the player's backpack YazaTheGreat 5 — 3y
Log in to vote
-1
Answered by 3 years ago

Thank you everyone who replied and helped but unfortunately all of these solutions didn't work or had something wrong. I have came up with my own solution:

game.Players.PlayerAdded:Connect(function(player)

    local Character = player.CharacterAdded:Wait()

    Character.Humanoid.Died:Connect(function()
        local ItemInBackpack = game.Players[player.Name].Backpack:FindFirstChildOfClass("Tool")
        print(ItemInBackpack)
        local actualItem = tostring(ItemInBackpack)
        local PlayerTools = game.ServerStorage.PlayerTools[player.Name]

        if PlayerTools:FindFirstChild(actualItem) then
            print(actualItem.." was found in your folder!")
            PlayerTools:FindFirstChild(actualItem):Destroy()
            if game.Players[player.Name].Backpack:FindFirstChild(actualItem) then
                game.Players[player.Name].Backpack:FindFirstChild(actualItem):Destroy()
            else
                print("nothing was found!")
            end

        else
            print("nothing was found!")
        end



    end)

end)

Answer this question