Hi there

Background

So I'm building a battle arena game, where two teams fight each other to score the highest kills.

The map also includes towers on each side of the team's map - which can be destroyed. I've done this by creating the towers as NPCs.

Each tower has a server script which checks for nearby human players. If it finds one, it checks if the tag on the player is the same as it's own team, or not.

This coding works perfectly and I can get the towers to print an output to say that an enemy has entered the range of the tower.

The problem

I'm struggling to get the towers to shoot a missile that will seek the player it detected - and only stop seeking it once it hits the player. The plan is to make the missiles to move at a speed that means the player wouldn't be able to outrun them - and the missile would hit within a couple of seconds no matter how soon you start running.

I want the missiles to act in the same way that you see the towers in DOTA2 shoot players.

I've tried putting the missile in both the replicated storage, and as a model attacked to the tower itself. I've tried both a server script and a local script.

So far the only real success I've had is as a server script attached to the missile in replicated storage. But not matter what I do, the missile either shoots towards the middle of the map, and then stops (looks like it's changing direction as it gets to the middle like it's drawn to the other missiles at the middle or something else - but there is nothing there), or if I lower the values it just appears and falls on the floor at the tower.

The missile is currently and invisible part. Attached to it is a fire, the server script, value (Speed), Bodygryo, and BodyPosition. I can't work out why it's not targeting the player that is nearest to it at the moment it's created.

This is the script:

local part = script.Parent

function moveTo(target)
	local dir = (target.Position - part.Position).unit
	part.BodyGyro.cframe = CFrame.new(0, dir)
	part.BodyGyro.MaxTorque = Vector3.new(9000, 9000, 9000)

	part.BodyPosition.position = target.Position
	part.BodyPosition.MaxForce = Vector3.new(10000, 10000, 10000) * part.Speed.Value
end

function findNearestTorso(pos)
	local nearest = {
		torso = nil,
		distance = math.inf
	}
	for i,v in ipairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		if human and torso and human.Health > 0 and isInstanceAttackable(human) then
			local this = {
				torso = torso,
				distance = (torso.Position - pos).magnitude
			}
			if this.distance < nearest.distance then
				nearest = this
			end
		end
	end
	return nearest.torso, nearest.distance
end

function isInstanceAttackable (targetInstance)
	if game:GetService("CollectionService"):HasTag(targetInstance.Parent, "Team1") then
		return false
	else
		return true
	end
end

while true do
	local torso = findNearestTorso(part.Position)
	if torso ~= nil then
		moveTo(torso)
	end
	wait()
end

Any help would be much appreciated. I'm only about 1 month into coding in LUA but have experience in general logic coding etc.

Cheers

Phil