How can I make this pet follow script less laggy?

  1. What do you want to achieve?

A pet follow script that is less laggy!

  1. What is the issue?

The issue is it is laggy when the pets follow the character!
https://gyazo.com/38c3755b8b2848453ee4c3d11b005ca7
3. What solutions have you tried so far?

Finding a solution!.

local Disabled = false
local Player = game.Players:FindFirstChild(script.Parent.Parent.Name)

if Player then
	while wait() do
		if Player.Character.Health ~= 0 then
			if not Disabled then
				local Pos = script.Parent:WaitForChild("Pos").Value
				local BG = script.Parent.PrimaryPart.BodyGyro
				local BP = script.Parent.PrimaryPart.BodyPosition
				local d = Player.Character.HumanoidRootPart.Position.Y - script.Parent.PrimaryPart.Position.Y
				BP.Position = (Player.Character.HumanoidRootPart.Position + Pos) - Vector3.new(0,Player.Character.HumanoidRootPart.Size.Y/2,0) + Vector3.new(0,script.Parent.PrimaryPart.Size.Y/2,0) + Vector3.new(0,game.ServerScriptService.PetSystem.globalPetFloat.Value,0)
				if Player.Data.isWalking.Value == false then
					BG.CFrame = CFrame.new(script.Parent.PrimaryPart.Position, Player.Character.HumanoidRootPart.Position - Vector3.new(0, d, 0))
				else
					BG.CFrame = Player.Character.HumanoidRootPart.CFrame
				end
			else
				script.Parent:Destroy()
				break
			end
		else
			Disabled = false
		end
	end
end

Thank you in advance!

Because You Are Using wait() Use task.wait or Run Service

I tried task.wait() and it came up with loads of errors

you could deal with it by waiting a function
add this on da loop

Player.Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("MoveDirection"):Wait()

Blasphemy! No way task.wait didnt work… Have you tried Run Service?

Where would i add that, sorry?

I know right wait let me show you the errors

The character has probably not loaded you have to wait for it to load

It is still really laggy is there anyway to fix it?

Try using this instead:

local Player = game.Players:FindFirstChild(script.Parent.Parent.Name)
local playerHealth = Player.Character:WaitForChild("Health")
local playerHumanoid = Player.Character.HumanoidRootPart
local isWalking = Player.Data.isWalking
local getPrimaryPart = script.Parent.PrimaryPart
local newVector3 = Vector3.new
local newCFrame = CFrame.new
local getParent = script.Parent

if Player then
while true do
task.wait(.05)

if playerHealth ~= 0 then
if not Disabled then
local Pos = getParent:WaitForChild("Pos").Value
local BG = getPrimaryPart.BodyGyro
local BP = getPrimaryPart.BodyPosition
local d = playerHumanoid.Position.Y - getPrimaryPart.Position.Y

BP.Position = (playerHumanoid.Position + Pos) - newVector3(0,playerHumanoid.Size.Y/2,0) + newVector3(0,getPrimaryPart.Size.Y/2,0) + newVector3(0,game.ServerScriptService.PetSystem.globalPetFloat.Value,0)

if isWalking.Value == false then
BG.CFrame = newCFrame(script.Parent.PrimaryPart.Position, playerHumanoid.Position - newVector3(0, d, 0))
else
BG.CFrame = playerHumanoid.CFrame
end
else
getParent:Destroy()
break
end
else
Disabled = false
end
end
end

What I did:

  • Indexed all of the important references as a single variable
  • Used task.wait() instead of wait()
  • Added a slightly slower wait time to the while loop

Well, I just recently made a video on my YouTube channel on making pets follow you, the way I did it in the video was without a loop, but rather using attachments, it might help you out!

2 Likes

You could also use this as attachments are way more performant that infinite while loops.

It is still laggy is it probably because of the loop?

Thank you for trying to help though!

The is a problem with this its when the player resets character the pets stay where the player resets and won’t move.