Script works but produces unneeded error

Hey working on a module to spawn a cow but it is being stupid like its making an error but the script works i just want the error gone here is my code

local cow = {} 

    function cow.summon(pos, groupAMM) 
        for _ = 1, groupAMM do 
            local Cow = script.Parent.Mobs.Rig:Clone() 
            Cow.Parent = workspace 
            Cow:SetPrimaryPartCFrame(CFrame.new(pos.X + math.random(-5, 5), pos.Y, pos.Z + math.random(-5, 5))) 

            spawn(function() 
                while true do
                    if cycles == 50 then

                        Cow:Destroy()

                        print("Mob cap hit; despawning and breaking loop.")

                        break 

                    else
                        cycles = 1

                        local PositionToMoveTo = Vector3.new(Cow.HumanoidRootPart.Position.X + math.random(-10, 10), Cow.HumanoidRootPart.Position.Y, Cow.HumanoidRootPart.Position.Z + math.random(-10, 10))

                        Cow.Humanoid:MoveTo(PositionToMoveTo)

                        wait(math.random(0, 2))

                        cycles += 1
                    end
                end
            end) 
        end 
    end 

return cow 

EDIT: i would still like help on this

Supplying the error would be helpful :smiley:

1 Like

say humanoid of rig cannot be found when i despawn it

That’s because your code never ends, it’s always trying to move it regardless of if it was despawned, but by the time your code runs, there’s no longer a HumanoidRootPart or Humanoid to manipulate.

Also, you are setting cycles to 1, then adding 1, so it will always equal 2 after your code runs once. I think I know what you were trying to do, I commented out your “cycles” variables and replaced it with a “totalCows” list, once it hits 50 it deletes the first cow, so it will always cap at 50, and the oldest cow will remove not the newest one:

It’s a good idea to set the variables for the Humanoid & HumanoidRootPart outside of the loop, and make sure it’s still existent before trying to manipulate it:

local cow = {} 
local totalCows = {}

    function cow.summon(pos, groupAMM) 
        for _ = 1, groupAMM do 
            local Cow = script.Parent.Mobs.Rig:Clone() 
            Cow.Parent = workspace 
            Cow:SetPrimaryPartCFrame(CFrame.new(pos.X + math.random(-5, 5), pos.Y, pos.Z + math.random(-5, 5))) 
            
            local Humanoid, HumanoidRootPart = Cow:FindFirstChild"Humanoid", Cow:FindFirstChild"HumanoidRootPart"

            table.insert(totalCows, Cow) -- add the new cow

            if #totalCows >= 50 then -- there's too many! delete the oldest cow!
                 table.remove(totalCows, 1):Destroy() -- remove the oldest cow in the list
            end

            spawn(function() 
                while true do
                    if not Humanoid or not HumanoidRootPart then -- somehow there was no humanoid (or) humanoidrootpart within the cow, abort mission

                        Cow:Destroy()
                        print("Mob Humanoid or Root Part never spawned in! Abort!")
                        break 
                    elseif not Humanoid.Parent or not HumanoidRootPart.Parent then -- the humanoid, or the humanoidrootpart was deleted for some reason, abort

                        Cow:Destroy()
                        print("Mob Humanoid or Root Part was deleted! Abort!")
                        break 
                    else
                        --cycles = 1 -- set it to 1

                        local PositionToMoveTo = Vector3.new(HumanoidRootPart.Position.X + math.random(-10, 10), HumanoidRootPart.Position.Y, HumanoidRootPart.Position.Z + math.random(-10, 10))

                        Humanoid:MoveTo(PositionToMoveTo)

                        wait(math.random(0, 2))

                        --cycles += 1-- ad 1, it will ALWAYS be 2, and it will never hit 50. you need to fix this
                    end
                end
            end) 
        end 
    end 

return cow 

quick edit to fix the “totalCows” table, the above code should work fine

1 Like

thank you i was trying to use tables before spawn but it wasnt working and im having fun dissecting this, thank you

1 Like