Why is this pet not following me?

Hello,

So I have made a quick and simple pet script where it should follow you. All works fine at the start (the pet appears next to u) but when you move the pet does not follow. I am not sure why though.

local pet = script.Parent

function givePet (player)
	if player then
		local character = player.Character
		if character then
			local humRootPart = character.HumanoidRootPart
			local newPet = pet:Clone ()
			newPet.Parent = character

			local bodyPos = Instance.new("BodyPosition", newPet)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

			local bodyGyro = Instance.new("BodyGyro", newPet)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

			while wait() do
				bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, -1)
				bodyGyro.CFrame = humRootPart.CFrame
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		givePet(player)
	end)
end)

I don’t see anything wrong with the script so my guess is it could be one of the settings on the object but not sure what would be stopping this (anchored is off).

Have you made sure your pet has a correct rig? Show me how it looks like in the explorer tab

Also, your script makes it so that when it clones a pet, that script also gets cloned as well. So you should delete that cloned script in the cloned pet model.

Some formatting stuff:

Remove the space between “givePet” and “(player)”

Remove the space between “pet:Clone” and “()”
These aren’t big issues, but formatting is still pretty important.

Regarding your main issue, could you provide us screenshots/video of the pet (in-test) and the stuff in explorer?

I know I just made it quick. I will do a better verision when I script it for real.

image

Also in regards to the cloning thing I know its just for now for testing. I will change it later on but I don’t think it should cause an issue like this.

Could you provide a video of this? Is it just not moving at all, or is it moving in a wrong direction, or is it moving oddly, etc etc?

Like I stated in the post it is not “not following me” which indicates it is not moving/following me.

It just appears next to me (goes to me) but then just stays where it spawned

So when you test it, it appears next to you, but it doesn’t move after that, correct?

Correct.

Here is a vid if you still don’t understand… (for some reason in the vid it does not go to me but just stays up there.

Came up with an idea:
Constantly check for the player’s position, and update the pet’s position relative to that position

Example

--this would be a script within the pet
while true do
       wait(0.01)
       local OriginPointX = character.RightLeg.Position.X
       local OriginPointY = character.RightLeg.Position.Y
       local OriginPointZ = character.RightLeg.Position.Z
       
       local PetPositionX = OriginPointX - 0.6
       local PetPositionY = OriginPointY - 0.15
       local PetPositionZ = OriginPointZ + 0.1

       script.Parent.Position = (PetPositionX, PetPositionY, PetPositionZ)   
end

Um thats exactly what I am doing in my script…

I am just doing it in one step to reduce the amount it has to deal with which will in turn makes it more efficient.

The only difference is I am doing it via the rootpart and u are doing it via the leg which I feel is not that good really.

Its getting the position and then adding the value to make it at the side not right in the middle.

You’re gonna want to use AlignPosition and AlignOrientation objects for this, specifically on TwoAttachment mode. The way these work is they attempt to match the position/rotation of attachments, which would cut down on the performance impact since it’s physics based.

I’d imagine you would add an attachment in the character that is off to the side of the player, then create one at the center of the pet, like so:

Read more here (these pages probably would do a better job explaining than I can):

https://developer.roblox.com/en-us/api-reference/class/AlignOrientation
https://developer.roblox.com/en-us/api-reference/class/AlignPosition

But that would make it just stay right next to the player which is not what I want. I want it so it looks like it is following the player.

This script which I made should work so not sure why not.

Why not decrease the responsiveness of them? This would make them reach their goals a bit slower

yea but also I want to make it so the pet will hover up and down a little when they are still which if I am correct should not be possible via the way ur saying??

You could use TweenService to move the attachment up and down forever (by setting the repeat count of a TweenInfo to -1), which would probably be what you’re looking for.

My bad, didn’t notice that. I don’t see the point of having the bodyGyo.CFrame = humRootPart.CFrame

Also, try to use less variables. Sometimes, if you use too many variables, you can accidentally change something that is a value and not the actual thing you want. I don’t know if you understand what I mean, but I hope you do. Try replacing all instances of humRootPart.CFrame with character.HumanoidRootPart