Pet Lerping & Relative Positioning Glitch


Getting tons of glitches with the pets glitching out a ton, and I’ve seen it myself.
I’ve looked over the dev wiki, but I didn’t see any solutions…
(The pets are models that lerp every frame to their target CFrame)
Can anybody help me out here?

local Player = game.Players.LocalPlayer
local Bounce = Instance.new("NumberValue")
Bounce.Value = 0
local mode = "up"
local touchEnabled = game:GetService("UserInputService").TouchEnabled

local function UpdatePets()
	if mode == "up" then
		Bounce.Value = Bounce.Value + 0.05
	elseif mode == "down" then
		Bounce.Value = Bounce.Value - 0.05
	end
	if Bounce.Value >= 0.75 then
		mode = "down"
	elseif Bounce.Value <= -0.5 then
		mode = "up"
	end
	if touchEnabled then
		for i,v in pairs(game.Players:GetPlayers()) do
			if v ~= game.Players.LocalPlayer then
				local Character = v.Character
				if Character then
					for i,v in pairs(Character:GetChildren()) do
						if v.Name == "GamePet" then
							v:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,-1000,0)))
							v.Name = "mobileUser"
						end
					end
				end
			end
		end
		local v = game.Players.LocalPlayer
		local Character = v.Character
		if Character then
			local root = Character:FindFirstChild("HumanoidRootPart")
			local humanoid = Character:FindFirstChild("Humanoid")
			if root and humanoid then
				local list = {}
				for i,GamePet in pairs(Character:GetChildren()) do
					if GamePet.Name == "GamePet" then
						table.insert(list,GamePet)
					end
				end
				for i,GamePet in pairs(list) do
					local info = TweenInfo.new(0.05,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut)
					local newpos = root.CFrame.p
					local newcf = CFrame.new(newpos)
					newcf = newcf * CFrame.Angles(0,math.rad((360/#list)*i),0)
					newcf = newcf + (newcf.lookVector*5) + Vector3.new(0,Bounce.Value,0)
					newcf = CFrame.new(newcf.p,newcf.p + root.CFrame.lookVector)
					if humanoid.MoveDirection == Vector3.new(0,0,0) then
						newcf = CFrame.new(newcf.p,root.CFrame.p)
					end
					if GamePet then
						GamePet.PrimaryPart = GamePet.MainPart
						local new = GamePet:GetPrimaryPartCFrame():lerp(newcf,0.1)
						GamePet:SetPrimaryPartCFrame(new)
					end
				end
			end
		end
	else
		for _,v in pairs(game.Players:GetPlayers()) do
			local Character = v.Character
			if Character then
				local root = Character:FindFirstChild("HumanoidRootPart")
				local humanoid = Character:FindFirstChild("Humanoid")
				if root and humanoid then
					local list = {}
					for i,GamePet in pairs(Character:GetChildren()) do
						if GamePet.Name == "GamePet" then
							table.insert(list,GamePet)
						end
					end
					for i,GamePet in pairs(list) do
						local info = TweenInfo.new(0.05,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut)
						local newpos = root.CFrame.p
						local newcf = CFrame.new(newpos)
						newcf = newcf * CFrame.Angles(0,math.rad((360/#list)*i),0)
						newcf = newcf + (newcf.lookVector*5) + Vector3.new(0,Bounce.Value,0)
						newcf = CFrame.new(newcf.p,newcf.p + root.CFrame.lookVector)
						if humanoid.MoveDirection == Vector3.new(0,0,0) then
							newcf = CFrame.new(newcf.p,root.CFrame.p)
						end
						if GamePet then
							GamePet.PrimaryPart = GamePet.MainPart
							local new = GamePet:GetPrimaryPartCFrame():lerp(newcf,0.1)
							GamePet:SetPrimaryPartCFrame(new)
						end
					end
				end
			end
		end
	end
end

game:GetService("RunService").RenderStepped:Connect(UpdatePets)
  1. What do you mean when you say they glitch?
  2. Are you able to provide any code?
  3. Can you provide a screenshot of the structure of the models?

You could try using TweenService instead for smooth animations. But without your codes you won’t get helped as effectively.

Animations are smooth when I use the lerping system, and much less laggy than tweening, but I can’t make it so it doesn’t completely glitch out the parts being welded to the main part (I use SetPrimaryPartCFrame())

What is the desired effect and what is actually happening? A video would be helpful.

Assuming the CFrame math is correct, my guess is that the problem lies with :SetPrimaryPartCFrame(). This function is known to act in undesired ways. This can be fixed by setting the CFrame of the PrimaryPart itself instead of using :SetPrimaryPartCFrame.

Instead of using :SetPrimaryPartCFrame(), you could weld all the other parts to the PrimaryPart, and just update the CFrame of the PrimaryPart.

:SetPrimaryPartCFrame() can be very buggy and can not work as you want it if updated a lot. It is more efficient to just update the CFrame of the PrimaryPart.

2 Likes

So, I might be mistaken, but welds don’t apply to other parts if the main part that the addons are welded to is anchored? Lmk how I would do this without the pet just falling through the ground because it’d have to be unanchored and non can collide

Leave the PrimaryPart anchored, and the rest of the parts unanchored. Then, weld them.

Kk, I’ll try it out