Tower rotation resets unexpectedly (Fixed)

robloxapp-20220111-2202367.wmv (1.5 MB)

When the tower rotates towards the enemy, the tower’s rotation resets to the rotation it was set to when you placed it, please help me fix that.

Here’s the part in the script that rotates the tower

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

If you want to the whole script just tell me.

1 Like

Please show the whole script 30chaaaarzzz

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

local events = ReplicatedStorage:WaitForChild("Events")
local SpawnTowerEvent = events:WaitForChild("SpawnTower")

local animateTowerEvent = game.ReplicatedStorage:WaitForChild("Events").AnimateTower

local tower = {}

function FindNearestTarget(newTower)
	local maxDistance = newTower:WaitForChild("Configuration").Range.Value
	local nearestTarget = nil
	
	for i, target in pairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		
		if distance <= maxDistance then
			nearestTarget = target
			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.CFrame = targetCFrame
		
		animateTowerEvent:FireAllClients(newTower, "Attack")
		
		target.Humanoid:TakeDamage(newTower:WaitForChild("Configuration").Damage.Value)
	end
	
	task.wait(newTower:WaitForChild("Configuration").FireRate.Value)
	
	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
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		
		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 index, part in pairs(newTower:GetDescendants()) do
			if part:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(part, "Tower")
			end
		end
		
		coroutine.wrap(tower.Attack)(newTower)
	else
		warn("Requested tower doesn't exist: ", name)
	end
end

SpawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower
1 Like

That’s actually a module script

You set your body gyro CFrame to the humanoid root part CFrame in your spawn function.

In your attack function, you set a new humanoid root part towards the enemy, however, you do NOT set the CFrame for the body gyro towards the enemy. The body gyro responsibility to set the model back to its assigned CFrame, which is the CFrame when you spawned the tower. This is why the tower resets its position.

A solution to this is to also set your body gyro CFrame to point towards the enemy.

Note: In future, please create a better title for your post that roughly explains your problem. “Help fix this please” is not useful information.

That you so much that workeddd