Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

ServerScriptService.Main:87: attempt to index nil with 'Destroy' help?

I have a pet system, and when i try to unequip the pet everything works just fine, the text changes to unequip, but i get an error in the output saying "Main.87 attempt to index nil with 'Destroy'" and it doesnt unequip the pet. I dont know how to fix this pls helppp

game.ReplicatedStorage.Remotes.UnequipPet.OnServerEvent:Connect(function(player, pet) if player.Character.Humanoid.Health ~= 0 then player. Character:FindFirstChild(pet):Destroy() end end)

2 comments

0

Can you post your script. we have almost nothing to work from. Ideally in/around line 87, the part where if breaks at. Then we can help you

0

TGazza i did that

2 answers

This part of the code is causing the problem: Character:FindFirstChild(pet) -- If the pet, is called pet. It would be Character:FindFirstChild("Pet") -- It expects a string to find. If this still does not work, it cant find the pet. Try this:

-- Locate player -- 
local Character = player.Character
if Character:FindFirstChild("Pet") then
local Pet = Character:FindFirstChild("Pet")
Pet:Destroy()
else
warn("Player does not have a pet / pet not found!") -- This will output a warning, if there is no pet
end

6 comments

0

i tried it but it says W001:Unkown global charcater

0

and then when i test in in play it says"attempt to index nil with FindFirstChild"

0

ah, I forgot. Add local Character = player.Character [I have eddied it into the script.]

0

I have edited the script, its night for me and I am tired... so sry about that!

0

so i did that and i bought my pet and everythign worked but i get the "player doedsnt have a pet" warning message and i preses unequip and it doesnt unequip it even tho the pet is right there

0

Sry, for the late responce. But make sure "Pet" is actually what the pets name is, Or. if its located somewere else. Such as in a folder, look for that.

i never use :Destroy(), it often ruins things for no reason. I would make a folder in replicated storage that unused things go in for example:

local folder = game.ReplicatedStorage.Folder
Pet.Parent = folder

This discussion is preserved as it was. New answers, comments, and votes are closed.