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

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

Asked by 2 years ago
Edited 2 years ago

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)

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 TGazza 1336 — 2y
0
TGazza i did that BearGamesZ 4 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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
0
i tried it but it says W001:Unkown global charcater BearGamesZ 4 — 2y
0
and then when i test in in play it says"attempt to index nil with FindFirstChild" BearGamesZ 4 — 2y
0
ah, I forgot. Add local Character = player.Character [I have eddied it into the script.] Jay123abc2 241 — 2y
0
I have edited the script, its night for me and I am tired... so sry about that! Jay123abc2 241 — 2y
View all comments (2 more)
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 BearGamesZ 4 — 2y
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. Jay123abc2 241 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

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

Answer this question