Converting BodyGyro to AlignOrientation

I need some help with converting BodyGyro to AlignOrientation.

Recently, I’ve been trying GnomeCode’s Tower Defense tutorial, even adding some of my code for a bit of flashy effects.

Then came the deprecated things.

First was Collision Groups, which I managed to fix on my own.
However, I can’t seem to figure out AlignOrientation.

Here is the script using BodyGyro:

local test = Instance.new("BodyGyro")
test.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
test.D = 0
test.CFrame = newTower.HumanoidRootPart.CFrame
test.Parent = newTower.HumanoidRootPart

And here is my script, which doesn’t work, using AlignOrientation.

local alignOrientation = Instance.new("AlignOrientation")
		alignOrientation.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
alignOrientation.Responsiveness = 200
alignOrientation.Mode = 0
alignOrientation.CFrame = newTower.HumanoidRootPart.CFrame
alignOrientation.Parent = newTower.HumanoidRootPart

There is also these other lines of code that might relate to this.

local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

If anyone can help it would be greatly appreciated!

AlignOrientation has two modes. The default being TwoAttachment, which means it will orient Attachment0 to Attachment1’s orientation. The other one being OneAttachment, which will orient Attachment0 to the provided CFrame. It looks like you want it to be OneAttachment, in which case you would need to run this code:

alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment

Nevermind, I see you already changed it to oneattachment.

The problem is that you never set the Attachment0 property of the AlignOrientation to anything. You need to create an Attachment parented to the Part you are trying to move, and set the Attachment0 property to it.

I just tried setting Attachment0 to HumanoidRootPart.RootAttachment, it didn’t work out.

I don’t really understand what your script is supposed to do though. Are you trying to align the Character with itself?? What is the purpose of it?

Would it help if I sent the full script?

Sure, but I’m really confused about this part here;

The BodyGyro is parented to HumanoidRootPart, but also has its target CFrame set to it, so it will essentially do nothing.

I assume this version with the AlignOrientation will do the same, and that’s why it still didn’t work, even when you set Attachment0 to an attachment.

When the tower attacks an enemy, as its meant to do in a tower defense, its CFrame is meant to change then.

local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

(Also, yes, I changed BodyGyro to AlignOrientation)

All right, well if it still isn’t working, could you send the full script?

local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")

local tower = {}

function FindNearestTarget(newTower)
	local maxDistance = 10
	local nearestTarget = nil

	for i, target in ipairs(workspace.EnemiesFolder:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance and target.Humanoid.Health >= 1 then
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget
end

function tower.Attack(newTower)
	local target = FindNearestTarget(newTower)
	if target then
		
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		newTower.HumanoidRootPart.AlignOrientation.CFrame = targetCFrame
		
		animateTowerEvent:FireAllClients(newTower, "Attack")
		
		target.Humanoid:TakeDamage(5)
	end
	
	task.wait(1)
	
	tower.Attack(newTower)
end

function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.TowersFolder
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		
		--[[
		local test = Instance.new("BodyGyro")
		test.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		test.D = 0
		test.CFrame = newTower.HumanoidRootPart.CFrame
		test.Parent = newTower.HumanoidRootPart
		]]
		
		local alignOrientation = Instance.new("AlignOrientation")
		alignOrientation.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		alignOrientation.Responsiveness = 200
		alignOrientation.Mode = 0
		alignOrientation.Attachment0 = newTower.HumanoidRootPart:FindFirstChild("RootAttachment")
		alignOrientation.CFrame = newTower.HumanoidRootPart.CFrame
		alignOrientation.Parent = newTower.HumanoidRootPart
		
		
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Tower"
			end
		end
		
		coroutine.wrap(tower.Attack)(newTower)
		
	else
		warn("nuh uh // Tower " .. name .. "does not exist!")
	end
end

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower

I found the problem.

The MaxTorque property is supposed to be a number, not a Vector3. Not sure why it doesn’t error though.

It worked!

Thank you so much for helping out! Not sure why it had to be a number though, since with BodyGyro it was originally Vector3, but it’s good to know for next time!

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