Destroy system for pets is attempting to nil with 'Parent' can someone tell me how to fix this?

-- the script
local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local char = player.Character
local count = 0
local pets = player.leaderstats.pets
local value = script.Parent.Parent.Value.Value
local notfound = 0
local destroyeds = 0
local value = 0

local function DESTROYY()
	print(script.Parent.Name)
print(value)
for i,v in pairs(player.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
	if v.Name == "UI" then
		count = count + 1
		print("count")
	end
end
if count == 0 then
	print("nothing")
else
	if count == 1 then
		pets.one.Value = 0
		script.Parent.Parent:Destroy()
		for i,v in pairs(char.pets:GetChildren()) do
		if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
					v:Destroy()
					value = (script.Parent.Parent.Value.Value)
			else
				print("not the parent")
				notfound = notfound + 1
			end
		end
	else
		if count == 2 then
			pets.two.Value = 0
			script.Parent.Parent:Destroy()
			for i,v in pairs(char.pets:GetChildren()) do
		if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
						v:Destroy()
						value = (script.Parent.Parent.Value.Value)
				else
					print("not the parent")
					notfound = notfound + 1
				end
			end
		else
			if count == 3 then
				pets.three.Value = 0
				script.Parent.Parent:Destroy()
				for i,v in pairs(char.pets:GetChildren()) do
		if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
							v:Destroy()
							value = (script.Parent.Parent.Value.Value)
					else
						print("not the parent")
						notfound = notfound + 1
					end
				end
			else
				if count == 4 then
					pets.four.Value = 0
					script.Parent.Parent:Destroy()
					for i,v in pairs(char.pets:GetChildren()) do
		if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
								v:Destroy()
								value = (script.Parent.Parent.Value.Value)
						else
							print("not the parent")
							notfound = notfound + 1
						end
					end
				else
					if count == 5 then
						pets.five.Value = 0 
						script.Parent.Parent:Destroy()
						for i,v in pairs(char.pets:GetChildren()) do
	if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
									v:Destroy()
									value = (script.Parent.Parent.Value.Value)
							else
								print("not the parent")
								notfound = notfound + 1
							end
						end
					else
						if count == 6 then
							pets.six.Value = 0
							script.Parent.Parent:Destroy()
							for i,v in pairs(char.pets:GetChildren()) do
	if (script.Parent.Parent.Value.Value) == v.Value.Value then -- and this is attempting to nil
										v:Destroy()
										value = (script.Parent.Parent.Value.Value)
									print("destroyed pet")
								else
									print("not the parent")
									notfound = notfound + 1
								end
							end
						end
						print("destoyed")
						print(count)
					end
				end
			end
		end
	end
	if notfound == 6 then
		for i,v in pairs(char.pets:GetChildren()) do
			if v.Value.Value <= 0 then
				local destroyeds = 0
				destroyeds = destroyeds + 1
				v:Destroy()
				print("notfound is destroyed")
			else
				print("not notfound")
			end
		end
		print(destroyeds)
		destroyeds = 0
		notfound = 0
	end
	end
end






script.Parent.MouseButton1Click:Connect(function()
	DESTROYY()
	for i,v in pairs(player.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
		if v.Name ~= "UI" then
			if (v.Value.Value) >= value + 1 then
				v.Value.Value = v.Value.Value - 1 
			end
		end
end
end)
-- I hope you can fix the problem in this script and thanks for your help
1 Like

Please hand over the output. :slight_smile:

2 Likes

instead of using destroy() use remove()

2 Likes

The first line is already off you just need to do game.Players.LocalPlayer to get the player assuming this is a local script

3 Likes

the title shows the output Destroy system for pets is attempting to nil with ā€˜Parentā€™

2 Likes

can you tell us where the error occurs?

1 Like

Hi!

I asked for the direct output, since it also tells us which line the error starts at. :slight_smile:

Edit: Which is what you asked for in your next message.

1 Like

hmm yes you got that point. I thinks its cause he is attempting to destroy a nil

1 Like

This is not a local script so Iā€™m trying to use as much as possible scripts because itā€™s running on the client and hackers/cheaters can hack/change their client and server running scripts are impossible to get for hackers/cheaters. so iā€™m looking for a safer alternative and thatā€™s why iā€™m trying to detect pets with number values

Itā€™s not destroying a nil the if statements have ā€˜parentā€™ in it and thatā€™s getting nil for some reason. (Also put some comments in the script to see where it goes wrong).

There is no specific point for the error because itā€™s looping with a count (looks how much pets there are and selecting the right pet ā€˜worksā€™) and in the if statement of the count itā€™s attempting to nil with ā€˜parentā€™

can you show me what it looks like in the explorer?
ā€œv.Value.Valueā€ is looking kinda buggy, its like taking the property ā€œValueā€ of an object. Thats why it returns nil cause there is no such thing as Value.Value

here is an example

local valueObj = Instance.new("StringValue")
valueObj.Value = "A Value"

print (valueObj.Value.Value) -- this will print nil since valueObj.Value is "A Value" and youre taking the Value of "A Value" which doesn't exist.
 --And i dont think you can put an instance as a Value whether its StringValue, IntValue, or BoolValue
 

So you might need to rename some objects or something, since i dont know whatā€™s going on really in your problem.

Thanks,

Iā€™ll rename the objects but Iā€™m not really sure of that will fix the problem because itā€™s attempting to nil with ā€˜parentā€™ and not with ā€˜valueā€™.

Why you getting the player like this?

Use this instead

game.Players.PlayerAdded:Connect(function(player)
      --all your code
end)

I canā€™t get the player out of the Index and Iā€™m trying to only use scripts. But I know itā€™s strange and buggy

game.Players.PlayerAdded:Connect(function(player)
	-- the script
	local char = player.Character
	local count = 0
	local pets = player.leaderstats.pets
	local value = script.Parent.Parent.Value.Value
	local notfound = 0
	local destroyeds = 0
	local value = 0

	local function DESTROYY()
		print(script.Parent.Name)
		print(value)
		for i,v in pairs(player.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
			if v.Name == "UI" then
				count = count + 1
				print("count")
			end
		end
		if count == 0 then
			print("nothing")
		else
			if count == 1 then
				pets.one.Value = 0
				script.Parent.Parent:Destroy()
				for i,v in pairs(char.pets:GetChildren()) do
					if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
						v:Destroy()
						value = (script.Parent.Parent.Value.Value)
					else
						print("not the parent")
						notfound = notfound + 1
					end
				end
			else
				if count == 2 then
					pets.two.Value = 0
					script.Parent.Parent:Destroy()
					for i,v in pairs(char.pets:GetChildren()) do
						if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
							v:Destroy()
							value = (script.Parent.Parent.Value.Value)
						else
							print("not the parent")
							notfound = notfound + 1
						end
					end
				else
					if count == 3 then
						pets.three.Value = 0
						script.Parent.Parent:Destroy()
						for i,v in pairs(char.pets:GetChildren()) do
							if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
								v:Destroy()
								value = (script.Parent.Parent.Value.Value)
							else
								print("not the parent")
								notfound = notfound + 1
							end
						end
					else
						if count == 4 then
							pets.four.Value = 0
							script.Parent.Parent:Destroy()
							for i,v in pairs(char.pets:GetChildren()) do
								if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
									v:Destroy()
									value = (script.Parent.Parent.Value.Value)
								else
									print("not the parent")
									notfound = notfound + 1
								end
							end
						else
							if count == 5 then
								pets.five.Value = 0 
								script.Parent.Parent:Destroy()
								for i,v in pairs(char.pets:GetChildren()) do
									if (script.Parent.Parent.Value.Value) == v.Value.Value then -- this is attempting to nil
										v:Destroy()
										value = (script.Parent.Parent.Value.Value)
									else
										print("not the parent")
										notfound = notfound + 1
									end
								end
							else
								if count == 6 then
									pets.six.Value = 0
									script.Parent.Parent:Destroy()
									for i,v in pairs(char.pets:GetChildren()) do
										if (script.Parent.Parent.Value.Value) == v.Value.Value then -- and this is attempting to nil
											v:Destroy()
											value = (script.Parent.Parent.Value.Value)
											print("destroyed pet")
										else
											print("not the parent")
											notfound = notfound + 1
										end
									end
								end
								print("destoyed")
								print(count)
							end
						end
					end
				end
			end
			if notfound == 6 then
				for i,v in pairs(char.pets:GetChildren()) do
					if v.Value.Value <= 0 then
						local destroyeds = 0
						destroyeds = destroyeds + 1
						v:Destroy()
						print("notfound is destroyed")
					else
						print("not notfound")
					end
				end
				print(destroyeds)
				destroyeds = 0
				notfound = 0
			end
		end
	end






	script.Parent.MouseButton1Click:Connect(function()
		DESTROYY()
		for i,v in pairs(player.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
			if v.Name ~= "UI" then
				if (v.Value.Value) >= value + 1 then
					v.Value.Value = v.Value.Value - 1 
				end
			end
		end
	end)
	-- I hope you can fix the problem in this script and thanks for your help
end)

Just this

1 Like

Does playeradded works in a playergui?

You might need to make your code cleaner to avoid more errors and so its easy to read.

But may i see what it looks like in the explorer?

2 Likes

Yeah you can get PlayerGui with that

local playerGui = player:WaitForChild("PlayerGui")

Okay Iā€™ll check it. Thanks for your help!

1 Like