RunService Heartbeat

Hello I’m working with a Pet System, and I have a problem with pet movement, the pet lag, and if I jump it bounces me away
robloxapp-20230112-1824222.wmv (739,8 KB)

local RunService = game:GetService("RunService")
local Character = script.Parent.Parent
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local pet = script.Parent

local function SetPetCFrame()
	pet.PrimaryPart.CFrame = HumanoidRootPart.CFrame * CFrame.new(2, 2, -3)
end -- Hroot is player's HumanoidRootPart

RunService.Heartbeat:Connect(function()
	wait()
	SetPetCFrame()
end)

I searched on google nothing, someone has an idea ?

2 Likes

Are you sure the pet has no collision? If a part’s CFrame was changed to another part CFrame (close enough for them to collide), it has a chance of flinging the other part (that’s why in my games, anything that is fast and can have contact with the player probably has no collision.
To fix the lag, you could lerp the pet position instead of positioning it, here’s an example:

local PetCFrame = pet.PrimaryPart.CFrame
local function SetPetCFrame()
	PetCFrame = PetCFrame:Lerp(pet.HumanoidRootPart.CFrame * CFrame.new(2, 2, -3), 0.5)
	pet.PrimaryPart.CFrame = PetCFrame
end

By the way, one thing I want to note, you don’t need to add that “wait()” in the Heartbeat function as it will barely change anything and will only make it less smooth.

So I have to turn off all the collision of the Pet inluding HumanoidRootPart and Body ?

1 Like

If it is making the player fling, then yes. If not, check the code I sent. (it doesn’t seem to fling the player from what I’ve seen in the video)

okay I’m testing your code and the collision

1 Like

okay I have a question, where is the player HumanoidRootPart CFrame ?? is it the pet.HumanoidRootPart ?

Wait I messed up, I’ll fix it.

local RunService = game:GetService("RunService")
local Character = script.Parent.Parent
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local pet = script.Parent
local petCFrame = HumanoidRootPart.CFrame * CFrame.new(2, 2, -3) -- create the cframe variable

local function SetPetCFrame()
	petCFrame = petCFrame:Lerp(HumanoidRootPart.CFrame * CFrame.new(2, 2, -3), 0.5) -- set the cframe variable to the lerped cframe of the humanoidrootpart multiplied by 2, 2, -3
	pet.PrimaryPart.CFrame = petCFrame -- set the cframe
end

RunService.Heartbeat:Connect(function(DeltaTime)
	SetPetCFrame()
end)

okay I solved the problem, but the pet is a cat and I would like to make it walking, and I know how to do it, but there is a way to make the pet not anchored to the player but walking ?

1 Like

You could make an walking animation, and instead of multiplying the CFrame by 2, 2, -3, you could multiply it by 2, 0, -3 instead.

okay but have u any ideas about a pet movement like Livetopia or Adopt me ?

In Adopt Me they use animations, not sure about Livetopia.

okay I thought they used something like MoveTo() is it possible ?

You can use math.cos(tick()) with CFrame.Angles() by modifying the code I sent.

okay and this makes the pet moves and rotate to me right ?

Yeah, if you multiply the CFrame by CFrame.Angles() then yes. Check this video about camera movement, it may help you on math.cos().

Make sure to mark the post as a solution if it fixed your issue.

okay give me some time to rewrite the code and I’ll let u know

1 Like

okay I have tried to make something like this but the game said to me that argument3 is missed…

local RunService = game:GetService("RunService")
local Character = script.Parent.Parent
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local Body = Character:WaitForChild("Torso", 1)
local pet = script.Parent
local Humanoid = pet.Humanoid
local petCFrame = HumanoidRootPart.CFrame * CFrame.new(2, -2, 3) -- create the cframe variable


local function PetMovement()
	local CurrentTime = tick()
	local XDirection = math.cos(HumanoidRootPart.CFrame.X)
	local YDirection = math.sin(HumanoidRootPart.CFrame.Y)
	
	local Vector = Vector3.new(XDirection, YDirection, 0)
	
	petCFrame = petCFrame:Lerp(Vector, 0.30)
	pet.PrimaryPart.CFrame = petCFrame
end

RunService.Heartbeat:Connect(function(DeltaTime)
	PetMovement()
end)

PS: sorry for all the questions but I’m new so I don’t know a lot

You forgot to set the alpha, :Lerp(Vector, Alpha)