Pets System: Pet Movment not smooth

So I’ve been trying to get the movmemnt for the pet system I’m working on as smooth as possible. I’m currently using the server for moving the pet. If I add a wait for the length of the tween it becomes a lot less bouncy, but it still isn’t as smooth as I’d like it to be.

Example of “bouncy”:

Code:

function follow(plr,obj)
	
	local prev = Vector3.new(0,0,0)
	
	
	while plr.Character.HumanoidRootPart ~= nil do
		 wait()
		
		if plr.Character.HumanoidRootPart.Position.X > prev.X + 1 or plr.Character.HumanoidRootPart.Position.X < prev.X - 1 or plr.Character.HumanoidRootPart.Position.Y > prev.Y + 1 or plr.Character.HumanoidRootPart.Position.Y < prev.Y -1 or plr.Character.HumanoidRootPart.Position.Z > prev.Z + 1 or plr.Character.HumanoidRootPart.Position.Z < prev.Z - 1 then
			prev = plr.Character.HumanoidRootPart.Position
			print("Hello")
			local g = {}
			local calc = CFrame.new(plr.Character.HumanoidRootPart.CFrame.X,0,plr.Character.HumanoidRootPart.CFrame.Z).p
			local goalback = CFrame.new(obj.CFrame.X, 0,obj.CFrame.Z).p)
			g.Position = (plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,5)).p
			g.CFrame = CFrame.new(calc, goalback)
			
			local ti = TweenInfo.new(0.25)
			local tween = game:GetService("TweenService"):Create(obj,ti,g)
			tween:Play()
	end
	end
	
end

Any ideas of how I could achieve the pet movement being smooth?

P.S: The function is put into a coroutine.

3 Likes

You need to handle this on each client. Doing so can also help with server to client lag.

3 Likes

This script should work.


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, 3)
bodyGyro.CFrame = humRootPart.CFrame
end
end
end
end

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

15 Likes

Hello, i want to say that while wait() do is not recommended, and you should avoid it.

4 Likes

Use AllignPostion and AlignOrientation with Attachments

Basic example

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local somePet = ReplicatedStorage.Pet

local alignPositionTemplate = Instance.new("AlignPosition")
alignPositionTemplate.RigidityEnabled = true

local alignOrientationTemplate = Instance.new("AlignOrientation")
alignPositionTemplate.RigidityEnabled = true
-- configure the template properties to your needs, making it smoother and whatnot

local function playerAdded(player)
	local function characterAdded(character)
		local pet = somePet:Clone()
		
		local charPrimary = character.PrimaryPart
		local petPrimary = pet.PrimaryPart
		
		local charOriAttachment = Instance.new("Attachment", charPrimary)
		local charPosAttachment = Instance.new("Attachment", charPrimary)
		charPosAttachment.Position = Vector3.new(2, 1, 2) -- position offset
		
		local petOriAttachment = Instance.new("Attachment", petPrimary)
		local petPosAttachment = Instance.new("Attachment", petPrimary)
		
		
		local alignPosition = alignPositionTemplate:Clone()
		alignPosition.Attachment0 = petPosAttachment
		alignPosition.Attachment1 = charPosAttachment
		alignPosition.Parent = petPrimary
		
		local alignOrientation = alignOrientationTemplate:Clone()
		alignOrientation.Attachment0 = petOriAttachment
		alignOrientation.Attachment1 = charOriAttachment
		alignOrientation.Parent = petPrimary
		
		pet.Parent = workspace
		petPrimary:GetNetworkOwner(player)
		-- set the network owner to the player, this ensures that the movement is smooth
	end
	
	characterAdded(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(characterAdded)
end

Players.PlayerAdded:Connect(playerAdded)

With this method, you only need to position the charPosAttachment whenever you to change the offset, so if the pet is simple and you only want it to stay behind the player and not move to other locations you only need to set the offset once

it’s far easier and more efficient than having to update the position and orientation in a while loop

Notes: I made this in a rush, please point out any issues or questions, I also only used the RigidityEnabled property in order to test quickly, in actual use you will want to tweak around with the other properties of the aligns and not use RigidyEnabled (unless you want it to be rigid)

14 Likes

I’ve decided to go with @Strongjohnfgamer 's solution to the problem, This gives me a smooth animation where as with Attachments I don’t get any animations. However, It does have to be said that Attachments would stop one from having to use coroutines. Thanks everyone for your help!

Edit: At some point I might let the client handle this as well @Protori

1 Like

you can get smooth movement by tweaking with the properties of AlignPosition and AlignOrientation, but do what’s best for you

you can also get more detail on the properties here New Body Movers as the api can be pretty minimal on the details

2 Likes

Are you using SetNetworkOwner? It makes pets move smoother.

1 Like