Help with PET AI

Hi, I am trying to make a pet that follows you, but it’s not working as expected.
Sometimes it stops, and sometimes it’s not really smart and can’t get trough some stuff.


code;

while (true) do
			wait()
			if (not Pet) then
				break
			end
			coroutine.wrap(function()
			local npcpos = Pet.Torso.Position
			local charpos = Player.Character.HumanoidRootPart.Position
				if (Player.Character.HumanoidRootPart.Position - Pet.Torso.Position).Magnitude < 6 then
					return
				end
			npcpath:ComputeAsync(Pet.Torso.Position, Player.Character.HumanoidRootPart.Position - Vector3.new(0,0,2))
			local target
			if npcpath.Status == Enum.PathStatus.Success then
					local waypoints = npcpath:GetWaypoints()
				target = waypoints[2].Position
				else
				target = npcpos
			end
				Pet.Humanoid:MoveTo(target)
				end)()
		end

Something must be in your script. An underlined statement or an error in the output of the script. Under the script thing, there should be something that says like “Missed statement 404: Workspace.Parent = SoundService”. Look in the error output and a source error.

Don’t use pathfinding, I usually do this. Keep in mind this code is kind of old, so it has flaws.

local function updatePets(pets, part)
	local i = 0
	for r =  360 / #pets, 360, 360 / #pets do
		i = i + 1
		local value = pets[i]
			
		local model, float, rotate = value.Model, value.Float, value.Rotate
		local size = #pets / 1.5
		
		local toPet = model.Body.Position - part.Position
		toPet = Vector3.new(toPet.x, 0, toPet.z).unit
		local ghostAt = part.Position + toPet*3 + Vector3.new(0, 2 + 1*math.sin(tick()), 0)
		
		local Under = CFrame.new(ghostAt):toWorldSpace(CFrame.new(Vector3.new(0,-1,0)))
		local Raycast = Ray.new(ghostAt, (Under.p-ghostAt).unit*100);
		local StandOn = game.Workspace:FindPartOnRay(Raycast, part.Parent);
		
		float.Position = Vector3.new(size * 2 * math.cos(math.rad(r)) + part.Position.X, StandOn.Position.Y+(StandOn.Size.Y/2)+(model.Body.Size.Y/2), size * 2 * math.sin(math.rad(r)) + part.Position.Z)
		rotate.CFrame = part.CFrame
	end
end

local running = {}

local function followPets(player, character, pets)
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	coroutine.wrap(function()
		while humanoid.Health > 0 and running[player.Name] do
			wait()
			updatePets(pets, humanoid.RootPart)
		end
		
		for _, pet in ipairs(pets) do
			pet.Model:Destroy()
		end
	end)()
end

local function createPets(player, character)
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	

	local petList = {}

	local petModel = Path.To.Pet:Clone()

	local float = Instance.new("BodyPosition")
	float.Name = "Float"
	float.Position = humanoid.RootPart.Position - Vector3.new(1,1,1)
	float.MaxForce = Vector3.new(2e+06, 2e+06, 2e+06);
	float.D = 1250
	float.P = 10000
	float.Parent = petModel.Body

	local rotate = Instance.new("BodyGyro")
	rotate.Name = "Rotate"
	rotate.CFrame = humanoid.RootPart.CFrame
	rotate.MaxTorque = Vector3.new(2e+06, 2e+06, 2e+06)
	rotate.P = 3000
	rotate.D = 500
	rotate.Parent = petModel.Body

	petModel.Parent = character
		
	table.insert(petList, {
		Model = petModel,
		Float =  float,
		Rotate = rotate
	})
		
	petModel.Body:SetNetworkOwner(player)
	
	running[player.Name] = true
	followPets(player, character, petList)
end
4 Likes

You can make the Pet CanCollide=False to fix the thing about ity not going through stuff.