Working on pet following script, two problems at the moment

YOOOOO
I’m sorry for the long wait,
I got a bit carried away with other stuff.
Anyways I fixed lag and also added multiple pet.
I updated the system a bit. Basically you have Vector3Value inside the pets, call them ‘Offset’, this is so you can have different pets in different positions (this is just one of the ways to do this).
image
To fix the lag, I tweaked the script a little and made it a LocalScript inside StarterCharacterScripts.

local players = game.Players
local tweenService = game:GetService('TweenService')
local RunService = game:GetService('RunService')

local pets = {workspace:WaitForChild('Pet1'), workspace:WaitForChild('Pet2')}

local player = players.LocalPlayer
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart


for i, pet in pairs(pets) do
	local newPet = pet:Clone()
	newPet.Parent = character
	
	local offset = {newPet.Offset.Value.X, newPet.Offset.Value.Y, newPet.Offset.Value.Z}

	RunService.Heartbeat:Connect(function()
		local targetCFrame = humanoidRootPart.CFrame 
			+ humanoidRootPart.CFrame.LookVector * offset[3] -- IDK A MORE EFFICIENT WAY TO DO THIS BIT, IF YOU KNOW, PLEASE POINT IT OUT XD
			+ humanoidRootPart.CFrame.RightVector * offset[1]
			+ humanoidRootPart.CFrame.UpVector * offset[2]

		local tween = tweenService:Create(
			pet, TweenInfo.new(.5),
			{
				CFrame = targetCFrame
			}
		)

		tween:Play()
	end)
end

This works real smooth

If you wanna give the player more pets while the game is running, just use remove events or something to update the pets table. Also you may wanna increase the tween time (0.5 is too quick as running it on a localscript is really smooth), maybe try 0.75 or something

hold up, there is an issue with this, you can see the pets following you on your screen, but on other people’s screens, they wont be following you.
Im trying to fix it

@Realgoodguyalways

Ok so I took @iATtaCk_Noobsi 's adivce into consideration and did something with Align Position (This is still laggier on the server but idk how to fix it :frowning: ):


It’s not as smooth as the tween one, but you could probably play around with the properties to make it better.

Here’s how you set it up:

Add AlignPosition and Align Orientation to the pet:
image
Set the MaxForce and MaxTorque to a really really big number.

Then you add an attachment and offset it however you want:

Then add this script to the pet, it basically just adds an attachment to the character’s hrp and sets the AlignPosition and AlignOrientation attachments:

local players = game:GetService('Players')
local pet = script.Parent

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
		local newPet = pet:Clone()
		newPet.Parent = character
		
		if not humanoidRootPart:FindFirstChild('PetAttachment') then
			local petAttachment = Instance.new('Attachment')
			petAttachment.Parent = humanoidRootPart
			petAttachment.Name = 'PetAttachment'
		end
		
		local petAttachment = humanoidRootPart:FindFirstChild('PetAttachment')
		
		newPet.AlignPosition.Attachment0 = newPet.Attachment
		newPet.AlignPosition.Attachment1 = petAttachment
		newPet.AlignOrientation.Attachment0 = newPet.Attachment
		newPet.AlignOrientation.Attachment1 = petAttachment
	end)
end)

1 Like

Wow, thank you! this works great!