How can I improve my pet follow system?

I’m making a simulator game as you can guess :smiley: I made a pet follow system as a prototype. I didn’t finish it yet. Not sure if it’s the best way to do it. That’s why I’m here. How can i improve this code?

Server:


local hum = char:WaitForChild("Humanoid")
		
		local pets = {}
		
		for i = 1, 5 do
			local pet = Instance.new("Part")
			pet.Anchored = false
			pet.CanCollide = false
			pet.Massless = true
			pet.Size = Vector3.new(1, 1, 1)
			pet.CFrame = hrp.CFrame
			
			local owner = Instance.new("StringValue")
			owner.Name = "Owner"
			owner.Parent = pet
			owner.Value = plr.Name
			
			local order = Instance.new("NumberValue")
			order.Name = "Order"
			order.Value = i
			order.Parent = pet
			
			pet.Parent = workspace.PetFolder
			
			pet:SetNetworkOwner(plr)
			
			table.insert(pets, pet)
		end
		
		hum.Died:Connect(function()
			for i,v in pairs(pets) do
				v:Destroy()
			end
		end)

Client:


local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")

local floatInc = 0.025
local maxFloat = 2
local minFloat = 0
local float = 0
local increase = true

workspace.PetFolder.ChildAdded:Connect(function(child)
	repeat wait() until child:FindFirstChild("Owner") and child:FindFirstChild("Order")
	if child.Owner.Value == plr.Name then
		local pet = child
		
		local moveLeft = child.Order.Value * 2.5
		
		local Attachment1 = Instance.new("Attachment", hrp)
		local Attachment0 = Instance.new("Attachment", pet)
		
		Attachment1.Position = Vector3.new(7.5-moveLeft, 2, 3)
		
		local AlignPosition = Instance.new("AlignPosition", pet)
		local AlignOrientation = Instance.new("AlignOrientation", pet)
		
		AlignPosition.Attachment0 = Attachment0
		AlignPosition.Attachment1 = Attachment1
		
		AlignOrientation.Attachment0 = Attachment0
		AlignOrientation.Attachment1 = Attachment1
		
		while wait() do
			if hum.Health <= 0 then
				pet:Destroy()
				break
			end
			if float <= minFloat then
				increase = true
			elseif float >= maxFloat then
				increase = false
			end
			if increase then
				float = float + floatInc
			else
				float = float - floatInc
			end
			Attachment1.Position = Vector3.new(Attachment1.Position.X, float, Attachment1.Position.Z)
		end
	end
end)

3 Likes

Set parents of instances after setting their properties, use while true do with a wait() instead of while wait() do.
Also, not sure why you’re looping until a child exists if you can use :WaitForChild() instead.

4 Likes

You can do all of this on the server with CFrame lerping and Heartbeat.

it lags the game alot when i do it on server and cframe looks choppy when you do it on server

It looks choppy when its using NetworkOwnerShip too.

I’m using alignposition on client side so it doesnt.

Hey! I know this is a tad bit old and unsure if you are still looking for a solution on this. I suggest using BodyMovers instead of CFrame. For this I would personally mix BodyGyro with BodyPosition. BodyGryo would be used to have the pet constantly follow you. This might be useful cause the pet will always be able to catch up to you. Here’s how I would personally do it.

local bodyPosition = Instance.new("BodyPosition",petPrimaryPart)
bodyPosition .P = 10000
bodyPosition .D = 500
bodyPosition .MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    	
local bodyGyro = Instance.new("BodyGyro", petPrimaryPart)
bodyGyro.P = 200
bodyGyro.D = 50
bodyGyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
RunService.Heartbeat:Connect(function()
    local newPosition = character.HumanoidRootPart.CFrame * CFrame.new(0,0,5) -- Where you wish the pet to offset from the HumanoidRootPart's position
    bodyPosition.Position = newPosition.p
    bodyGyro.CFrame = CFrame.new(bodyPosition.Position,character.HumanoidRootPart.Position) * CFrame.Angles(math.pi/2,-math.pi/2,math.rad(90)) --- Where you gyro to go. You can remove CFrame.Angles() if you do not wish the pet to turn in sync with the character.
end)
6 Likes