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

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

Asked by 3 years ago

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

Log in to vote
1
Answered by
R_alatch 394 Moderation Voter
3 years ago

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.

0
That worked very well. Thanks! RazzyPlayz 497 — 3y
Ad

Answer this question