Pet / Follower system ~ Magnitude?

I’m working on a ‘Pet / Follower’ system & I’m having trouble getting Magnitude / Range to work appropriately and not be jittery.
I’m looking for a way to have the Pet stop when the Player is within a certain range (Base as shown in the video) and when the Player is outside then the Pet is to follow…
However, I’m finding it difficult due to when the Player re-enters the cylinder it stops instantly and abruptly from past attempts.

Code:
	local Core = { ... }
	local Server, Self = game, script
	
	local Player = Server:GetService('Players').LocalPlayer
	local Character = Player.Character or Player.CharacterAdded:Wait(2)
	local HumanoidRootPart = Character:WaitForChild('HumanoidRootPart', 1)
	
	local Pet = { ... }
	Pet.Model = Self.Parent
	Pet.isFollowing = true
	
	local RaycastParam = RaycastParams.new()
	RaycastParam.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParam.FilterDescendantsInstances = { Pet.Model, Character }
	
--||--||--||

	function Core:GetHeight(...)
		local Result = Server:GetService('Workspace'):Raycast(Pet.Model.Base.Position + Vector3.new(0, 2, 0), Vector3.new(0, -100, 0), RaycastParam)
		if Result then
			return Result.Position.Y - 0.5
		end
	end
	
	function Core:CheckObstruction(...)
		local Result = Server:GetService('Workspace'):Raycast(Pet.Model.Base.Position + Vector3.new(0, 5, 0), Pet.Model.Base.CFrame.LookVector * 2, RaycastParam)
		if Result then
			return true
		else
			return false
		end
	end

--||--||--||
	
	Server:GetService('RunService').PostSimulation:Connect(function(dt, ...)
		local X, Y, Z = HumanoidRootPart.Position.X, Core:GetHeight(...), HumanoidRootPart.Position.Z
		
		Pet.Model.Base.CFrame = Pet.Model.Base.CFrame:Lerp(CFrame.lookAt(Pet.Model.Base.Position, Vector3.new(X, Pet.Model.Base.Position.Y, Z)), dt * 5) -- Looks at Player.
		Pet.Model.Base.CFrame = Pet.Model.Base.CFrame.Rotation + Vector3.new(Pet.Model.Base.Position.X, Y, Pet.Model.Base.Position.Z) -- Limits it to Y axis.
		
		if Pet.isFollowing and not Core:CheckObstruction(...) then
			Pet.Model.Base.CFrame = Pet.Model.Base.CFrame:Lerp(HumanoidRootPart.CFrame.Rotation + Vector3.new(X, Y, Z), dt * 2.5)
		end
	end)

--||--||--||
	
	return Core
Current Behaviour:

Intended Behaviour:

You can try to setting it to go behind the player
HumanoidRootPart.Position+ HumanoidRootPart.CFrame.LookVector * -5, or something along the lines of this

Ideally I want it to follow the Players current position as opposed to walking behind. :confused:

Can’t even get this to run … where is script placement and pet placement …
Is this only part of the script?

The “Intended Behavior” would have the same effect as setting it to be right behind the player.

The video you showed makes the pet follow a point behind the player.

1 Like

It doesn’t walk directly to a point behind the Player; that’s just where it stops.
It walks to the HumanoidRootPart and stops when within a certain range.

Think I’ve found a solution!
Two separate Raycasts - One checks for the Green part and another checks for the Red.
If the Player is not touching Red nor green then the Pet will follow, however if the Player is touching Red then the pet will stop.

2 Likes