My NPC towers keep flying everywhere after a while when i place them down. How to stop this?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    Im making a tower defense game which is coming on really well.
  2. What is the issue?
    A couple of my towers fly around the room about 20ish seconds after placing them. Its like they start pivoting and then spin around the room hitting walls, ceilings, floors lol. Im afraid that this may occur with further towers that i create.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Ive changed the hip height lots of times. Ive also change the lookVector to x,z position and turned y off. Ive tried turnignx off too. Ive also changed the size of the HumanoidRootPart. What have i missed? My code is below. I appreciate any help, guidance, etc, my code is below.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block
function FindNearestTarget(newTower, range)
	local nearestTarget = nil

	for i, target in ipairs(workspace.Zombies:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		
		if distance < range then
			
			nearestTarget = target
			range = distance

		end
	end

	return nearestTarget
end

function tower.FindTarget(newTower, range)
	
end

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = FindNearestTarget(newTower, config.Range.Value)
	
	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
		newTower.HumanoidRootPart.BodyGyro.CFrame = CFrame.new(Vector3.new(0,0,0), targetCFrame.LookVector * Vector3.new(1,0,1))
		
		
		animateTowerEvent:FireAllClients(newTower, "Attack", target)
		
		target.Humanoid:TakeDamage(config.Damage.Value)
	
		
		if target.Humanoid.Health <= 0 then
			player.Zzzs.Value += 25
		end
		
		task.wait(config.Cooldown.Value)
	end

	task.wait(0.1)

	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end

function tower.Sell(player, model)
	if model and model:FindFirstChild("Config") then
		if model.Config.Owner.Value == player.Name then
			player.PlacedTowers.Value -= 1
			player.Zzzs.Value += model.Config.Price.Value / 1.5
			model:Destroy()
			return true
		end
	end
	
	warn("Unable to sell this tower")
	return false
end
sellTowerFunction.OnServerInvoke = tower.Sell

function tower.Spawn(player, name, cframe, previous)
	local allowedToSpawn = tower.CheckSpawn(player, name, previous)

	if allowedToSpawn then
		
		local newTower
		if previous then
			previous:Destroy()

			newTower = ReplicatedStorage.Towers.Upgrades[name]:Clone()

		else
			newTower = ReplicatedStorage.Towers[name]:Clone()
			player.PlacedTowers.Value += 1
		end
		
		local ownerValue = Instance.new("StringValue")
		ownerValue.Name = "Owner"
		ownerValue.Value = player.Name
			ownerValue.Parent = newTower.Config
			
			local targetMode = Instance.new("StringValue")
			targetMode.Name = "TargetMode"
			targetMode.Value = "First"
			targetMode.Parent = newTower.Config


		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 i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Towers")
				
			end
		end
		
		player.Zzzs.Value -= newTower.Config.Price.Value

		
		coroutine.wrap(tower.Attack)(newTower, player)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

i’d recommend instead of using a BodyGyro to just have the NPC anchored and rotate it manually with CFrames. however, it doesn’t seem like you have anything holding it down hence the flinging. either put a BodyPosition in it to make sure it doesn’t fly around, or anchor it.

1 Like

I can’t anchor it otherwise it won’t work. I’ll try body position tonight hopefully and will let you know if it works

Do the towers move? Why can’t you Anchor them?
And why do you have Humanoids in your towers?

It’s strange because in any tower defense game I’ve seen the towers are usually anchored wherever the player puts them, and the enemies (zombies) move along a path toward a goal.
If the towers need to rotate to face the enemies it can be done with CFraming.
Adding more Humanoids can also cause more lag.

I can’t anchor them because it stops them turning to look at the enemies. How can I do this?

Use CFrame.lookAt so they are Anchored, and will rotate to face the enemy.

CFrames | Roblox Creator Documentation
Object and World Space | Roblox Creator Documentation

Then you don’t need all the extra Humanoids clogging up the Workspace either.

I have got the lookAt working but when I anchor the humanoid root part it stops working. ?

Also body position has been changed to align orientation/position and is quite complex in terms of getting it to work in my script. Spent a whole day trying :flushed:

But get the Humanoid out of the picture completely.
Make the tower model, make sure it has a PrimaryPart set up, then use CFraming to aim the entire model.
CFraming has less lag than the way you are doing it.

Based on the code you have provided, it looks like the issue is that the towers are spinning around due to the BodyGyro being assigned incorrectly. To fix this, you should adjust the CFrame of the BodyGyro so that it is facing the direction you want the tower to be pointing. Here is an example of how you could do that:

newTower.HumanoidRootPart.BodyGyro.CFrame = CFrame.new(Vector3.new(0,0,0), targetCFrame.LookVector * Vector3.new(1,0,1))
1 Like

Thankyou so much for your suggestions. Got look at working properly​:partying_face:. Now I need to make the animations work without the humanoid in my animation script. :face_with_monocle:

I’ve deleted the bodygyro because I couldn’t anchor the humanoid root part due to the physics. I’ve ended up using lookAt and that works.
Now to figure out how to change my script so I can delete my humanoids :sob: