I want the bodyposition to always be behind the character

Code:

local runService = game:GetService("RunService")

local character = script.Parent
local rootPart = character.HumanoidRootPart
local pets = {}
local spacing = 4

runService.Heartbeat:Connect(function(deltaTime)
	local columns = math.floor(math.sqrt(#pets))
	local offset = Vector3.new(-columns / 2 * spacing + spacing / 2, 0, 4)
	for i, pet in ipairs(pets) do
		i -= 1
		local x = i % columns
		local z = math.floor(i / columns)
		local targetPosition = rootPart.Position + offset + Vector3.new(x, 0, z) * spacing
		
		if pet:FindFirstChild("BodyPosition") then
			pet.BodyPosition.Position = targetPosition
		end
		
		if pet:FindFirstChild("BodyGyro") then
			pet.BodyGyro.CFrame = rootPart.CFrame
		end
	end
end)

workspace.PlayerPets:WaitForChild("AbyssalBl4de").ChildAdded:Connect(function(Child)
	Child:SetPrimaryPartCFrame(rootPart.CFrame)
	table.insert(pets, Child.PrimaryPart)
	
	local bodyPosition = Instance.new("BodyPosition")
	bodyPosition.MaxForce = Vector3.new(4000, 4000, 4000)
	bodyPosition.P = 2000
	bodyPosition.D = 100
	bodyPosition.Position = Child.PrimaryPart.Position
	bodyPosition.Parent = Child.PrimaryPart
	
	local bodyGyro = Instance.new("BodyGyro")
	bodyGyro.MaxTorque = Vector3.new(4000, 4000, 4000)
	bodyGyro.P = 3000
	bodyGyro.CFrame = Child.PrimaryPart.CFrame
	bodyGyro.Parent = Child.PrimaryPart
end)

workspace.PlayerPets:WaitForChild("AbyssalBl4de").ChildRemoved:Connect(function(Child)
	table.remove(pets, table.find(pets, Child.PrimaryPart))
end)

Hello,
I want the bodyposition to always be behind the character, how can i make that?

To make it be behind you can use CFrames.

local distance = 10
bodyPosition.Position = (HumanoidRootPart.CFrame * CFrame.bew(0, 0, distance)).Position

Like so.

It is also suggested to use AlignPosition.

And also can you check the full script and answer again please?

local targetPosition = (rootPart.CFrame * CFrame.new(offset + Vector3.new(x, 0, z) * spacing)).Position

Try this.

YESS! It’s working. Thank you so much man. You are a LIFESAVER!

Yup, no worries! Have a good day.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.