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.
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)
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)
:FindFirstChild(Item.Name)
.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)