BodyGyro Is Not Working

I am following a youtube tutorial to make a game but I have come across a problem where my body gyro does not work can someone help?

Code:

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 = 20
	local nearestTarget = nil
	for i, targat in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (targat.HumanoidRootPart.Position  - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance then
			nearestTarget = targat
			maxDistance = distance
		end
	end

	return nearestTarget
end

function tower.Attack(newTower)
	local target = FindNearestTarget(newTower)
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		animateTowerEvent:FireAllClients(newTower, "Attack")
		target.Humanoid:TakeDamage(25)
	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.Towers
		
		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
		bodyGyro.Parent = newTower.HumanoidRootPart

		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
			end
		end
		
		coroutine.wrap(tower.Attack)(newTower)
	else
		warn("Requested tower does not exist:", name)
	end
end

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower

Bodygyro is deprecated. Use this instead AlignOrientation

2 Likes

Try setting the P of the BodyGyro to something higher.

1 Like

While gyro is indeed deprecated, it isn’t removed nor non-functional.
AlignOrientation would require a different implementation, which for a student who is following a tutorial may be a bit jarring.

As for the query. Can we have a bit more context on what tower is, what it is supposed to do, and what it is currently doing? Perhaps a video or an image?

Question 1.
By the looks of it, I would expect ‘tower’ to be a static object - not moving but rotating?
Do you perhaps have the Tower Anchored?

Question 2.
The tower.Attack method first looks for a target, have you ensured that the ‘target’ exists? (print out your target to validate?)

Question 3.
If the above is correct but your tower is rotating incorrectly, perhaps the targetCFrame is incorrect?

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

just to note - it has been a very long time since I last worked with cframes - so the above may be incorrect

1 Like

try this, not surre

local targetCFrame = CFrame.new(newTower.HumanoidRootPart.Position * target.HumanoidRootPart.Position)
1 Like

The tower was anchored thank you so much