Pet Follow System

Hey Developers! I am trying to make a pet follow system. Now my pet system is not your normal simulator pets, however what I need help on is something that every simulator experience that has pets, has. Which is that pets follow you! (I don’t want an instant Teleport, But I want ythe pet to move from it’s last position, to the newest position after a player moves) I feel like this should be simple… But I am failing awfully at this… Here is some things that I want:

  • There can only be one pet at a time, so no more then one pet out at once,, the pet is fixed at a position from the character’s root part. You can give me an example of this position by a random value

  • All Pets are Models, not parts or unions

  • I would like to use one of these methods: Tweening w/ TweenService, Pivot Of models, linear velocity. If you have other ideas, I would be intrested, but I just know these 3 methods the most

There is no script because all of them have failed :pensive: But I have had some errors like this


Any Help is appreciated!

I don’t think linear velocity would be very useful for this, but when I was first learning how to script, and I was first learning lerping, I accidentally figured out a very good way to make a pet system. Take your model, and every RenderStepped, do this:

local char = game.Players.LocalPlayer.Character
local pet = -- pet
local relativecframe = CFrame.new(0, -2, -3)
local speed = 0.15

game:GetService("RunService").RenderStepped:Connect(function()
    pet.CFrame = pet.CFrame:Lerp(char.HumanoidRootPart.CFrame * relativecframe, speed)
end)

This code, every render stepped, repositions the pet closer and closer to a CFrame relative to the humanoidrootpart. It’s not tested.

5 Likes

If you are trying to make the pet follow the player in a smooth and natural manner, the easiest method would be to use TweenService and tween the position of the pet model from its current position to the player’s position. You can do this by using the TweenService’s: Create() method to create a new tween, setting the Position property to the player’s position, and finally calling the Play() method to start the tween. You will want to make sure you update the pet’s position whenever the player moves so that it continues to follow the player.

Here’s a basic example::

local player = game.Players.LocalPlayer
local pet = workspace.Pet

local TweenService = game:GetService("TweenService")

local function followPlayer()
	local targetPosition = player.Character.RootPart.Position
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear) -- tween time of 1 second, linear easing
	local tween = TweenService:Create(pet, tweenInfo, {Position = targetPosition})
	tween:Play()
end

player.CharacterAdded:Connect(followPlayer)

This is just one way to do it, but there are many other methods you could explore, like using the Pivot of models or linear velocity . Hope this helps!

1 Like

And I would Love to do this. However my pet’s aren’t Parts. They are models So I can’t just do that

If your pets are models instead of parts, you can still use a similar approach as above, but you’ll need to use a different property to move the pet, since Position only works for parts. One option is to use the CFrame property of the model to change its position.

Here’s an updated example to demonstrate this:

local player = game.Players.LocalPlayer
local pet = workspace.Pet

local TweenService = game:GetService("TweenService")

local function followPlayer()
	local targetPosition = player.Character.RootPart.Position
	local currentPosition = pet.PrimaryPart.CFrame.p
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear) -- tween time of 1 second, linear easing
	local tween = TweenService:Create(pet, tweenInfo, {CFrame = CFrame.new(targetPosition, currentPosition)})
	tween:Play()
end

player.CharacterAdded:Connect(followPlayer)

In this example, the CFrame property of the pet model is set to a new CFrame created using the player’s position as its position, and the current position of the pet’s primary part as its second argument. This sets the position of the pet model to the player’s position.

I highly disagree. Using tween service introduces unnecessarily complex programming and computing. tweening is also very TAXXING, so increase amounts of this in your code, especially if your game has a lot of player will result in increased amounts of lag. I don’t recommend this method. Additionally, his script won’t even make the pet follow the player. It’ll only follow it’s original position.

You don’t have to use :PivotTo(). Set the model’s primary part to a part in the center of the model, or create a new invisible one, and then unanchor, but use `WeldConstraints" to weld all the other parts in the model to the invisible one. Then you can simply set the position of the primary part, and all the other parts will follow. :slight_smile:

Your system is working. However I don’t understand Lerp too well, But Some weird things are happening with your system, but should be fixable? Here is a video

(using discord, file is too big)
https://cdn.discordapp.com/attachments/849358545479794712/1072007414573973514/2023-02-05_20-12-11.mp4

2 issues and they are probably my fault and so I’m sorry.

  1. I did use welds and things to hold to egg together, when I get close, it throws me into a wall or moves me far from the egg.

  2. the egg will end up in the void, Because it’s unanchored, and just be removed as a whole because it falls.

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local player = Players.LocalPlayer
local character = player.Character
local primaryPart = Instance.new("Part")
primaryPart.Name = "PrimaryPart"
primaryPart.Anchored = false
primaryPart.CanCollide = false
primaryPart.Size = Vector3.new(1,1,1)
primaryPart.Parent = character

local petModel = script.Parent
for i, part in pairs(petModel:GetChildren()) do
	if part:IsA("BasePart") and part ~= primaryPart then
		local constraint = Instance.new("WeldConstraint")
		constraint.Part0 = primaryPart
		constraint.Part1 = part
		constraint.Parent = part
	end
end

local targetPosition = primaryPart.Position

while true do
	if character and character.PrimaryPart and character:FindFirstChild("HumanoidRootPart") then
		targetPosition = character.HumanoidRootPart.Position + Vector3.new(3,0,3) -- set the target position 3 units away from the player in the x and z axis
		
		local tweenInfo = TweenInfo.new(
			0.5, -- duration
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out
		)
		local tween = TweenService:Create(primaryPart, tweenInfo, {Position = targetPosition})
		tween:Play()
	end
	wait(0.1)
end

This script sets the primary part as an invisible part in the center of the pet model, and then uses the WeldConstraint to weld all the other parts to it. It then sets the target position to be 3 units away from the player’s HumanoidRootPart position. The TweenService is used to smoothly interpolate the position of the primary part from its current position to the target position over 0.5 seconds.

1 Like

The first one is happening because you’re colliding with the egg. I recommend just turning off collisions for the egg. That’s what most games do. Now I don’t know about the egg ending up in the void, but if the character always exists, and the egg isn’t tangible, you should be a-ok.

Turning it off results in this:

Is the egg anchored? It has to be anchored for this to work. At least the primary part does (if you’re using that system).

There we go! Thank you so much again. I had it unanchored because I was welding the egg to the player and tried thing’s that needed it unanchored!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.