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
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!