Pet follow script not working as intended

so basically i don’t know how to make the pet appear behind the player.
script:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		while wait() do
			script.Parent.CFrame= char.Head.CFrame
		end
	end
end)

Im very confused with this script. You intend to have the pet follow you, right?

1 Like

Yes, I want it to follow me. But I want it 3 studs behind me.

1 Like

You are attempting to set a position to a CFrame. You cannot do that, either set a CFrame to a CFrame or a position to a position. Also you can just weld the pet to the player or use an attachment.

1 Like

I would try using a “while true do” and wait 3 seconds. If the pet has a humanoid, use Humanoid:MoveTo(“position of player root part here”) in a loop.

2 Likes

Yes, but how would I get the pet to appear behind me. My script works it’s just that I want the pet 3 studs behind me.

1 Like
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		while true do
			local targCFr = char.Head.CFrame
			script.Parent.CFrame= targCFr + (targCFr.LookVector * -3)
			wait()
		end
	end
end)

I think this is what you’re looking for. Although I don’t really get why you would initialize this with a touched event with no debounce.

1 Like

Will first of all, you are using the wrong function. Touching a part to have a pet follow you? Your first statement makes no sense in this case, you are trying to have a pet follow you. Why have an if statement that checks to see if a humanoid touched a part?

1 Like

I’m just trying to test my code I am going to modify the function to my needs.

This code will not execute correctly tho… you are trying to have a pet follow you, but you are checking if a humanoid touched a part? I am still very confused on why you have this function.

1 Like

‘hit’ is the part that touched, in this case he’s just checking if the part which touched (‘hit’) has a humanoid inside.

1 Like

Why do that though? If he is trying to get a pet to follow him, it would make no sense to do that.


function givePet (player)
	pet.CanCollide = false -- Makes it so the pet can't collide with anything.
	if player then
		local character = player.Character -- Gets character aspect of player.
		if character then
			local humRootPart = character.HumanoidRootPart -- Gets root part of the humanoid.
			local newPet = pet:Clone() -- Clones our existing pet.
			newPet.Parent = character -- The parent is the character.
			
			local bodyPos = Instance.new("BodyPosition", newPet) -- Creates a body position for the pet/
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge) 
			
			local bodyGyro = Instance.new("BodyGyro", newPet) -- Creates the body gyro.
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
			
			while wait() do
				bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
				bodyGyro.CFrame = humRootPart.CFrame
			end
		end
	end
end

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

Here is the code I use for my pets. I have commented out most of it to explain it.

1 Like

It’s inefficient do do it like this. Players typically touch a part 20 times when they walk over it. You don’t want to 20+ connections running.

That’s not the error though, because the code doesn’t make sense when the player touches the part. It won’t cause anything, but errors in the output bar.

I’m literally just testing my code quickly with a touched function to see how it performs.

Are you trying to make it start following you only after you touched it?

I posted the code I use for my development in the topic earlier.

1 Like

I was trying to ask how to make a pet follow 3 studs behind me it’s very simple.

Because it’s a way to verify that the thing which touched is a player/npc (usually a player, not sure though). This is a pretty common practice to ensure random unanchored parts don’t touch the part and break stuff.

As I mentioned earlier though, I agree the setup using a touched event is random and unnecessary.