Why my pet script doesn't work?

I want to make a pet that stays on the side of a player’s head. I have tried but I can’t get it to work. Here is the current script. It is very glitchy. Please help.

local p = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local root = char.Head
		while true do
			local dir = CFrame.new(p.Position,root.Position+Vector3.new(4,0,0)).LookVector
			p.CFrame = p.CFrame + (dir*0.2)
			wait()
		end
	end)
end)

Mainly because a while loop wouldn’t be enough, and won’t keep up with the player, you may wanna use the Heartbeat event (considering that this seem to be done from a server script).

2 Likes

When the character stops moving the pet vibrates very fast, and its orientation does not change.

Can you send a gif or something to describe what’s going on please?

I can’t send a gif right now I don’t know how. It just vibrates very fast up and down.

You can try it out yourself in Roblox studio. Just make a small part and add the script in it. Make sure to anchor the part.

Unrelated, Im not sure how you have that model set or placed. So based on speculation, that would only work with one client. The next time a client joins, that pet will move back and forth from each player since the pet is being told to go to multiple cframes.

Its there just for testing purposes… I test it in play solo…

Use BodyForce and BodyVelocity instead. Its way more effective

1 Like

Actually, not a bad idea. Just get the vector position in world coordinates of where you want the pet to be (beside the head) and set the position in the body position to that vector pos.

1 Like

You can find a solution to your problem here . Just use the script Flubberlusch gave and modify the values of BodyForce and BodyVelocity for your pet. I recommend to parent your pet to the player’s HumanoidRootPart and calculate its position with CFrame (Like Flubberlutsch did.).

4 Likes