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