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

Pet amount check not working? attempt to compare number and Instance

Hello.

I am currently attempting to make a barrier where you need a specific amount of pets to go through. I thought this would work by making it go through a for loop, however i get the error attempt to compare number and Instance. Not sure what is caused by this, and would like some help. Here's my script:

local Player = game:GetService("Players").LocalPlayer
local PetFolder = Player:WaitForChild("Pets")

local Barrier = workspace:WaitForChild("5 Pets Barrier").Barrier:FindFirstChild("Main Barrier")

for _, PetAmount in pairs(PetFolder:GetDescendants()) do
	if PetAmount >= 5 then
		Barrier.CanCollide = false
		Barrier.BrickColor = BrickColor.new("Lime green")
	end
end

I am stuck on this and would like some help. Thanks

1 answer

✓ Accepted answer

PetAmount in this case will be the actual pet object. You can't compare an object to a number. To fix this, just do:

if #PetFolder:GetChildren() >= 5 then
	Barrier.CanCollide = false
	Barrier.BrickColor = BrickColor.new("Lime green")
end

#PetFolder:GetChildren() gets the length of the array of pets in the folder.

1 comment

0

That worked very well. Thanks!

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