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.

7 comments
where the destroy script
Oh and also for @Blackbooks question, it on line 10, it is a bit long
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.
Your While loop is also redundant.
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.
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.
The fix is stated in my answer.