Help with ai script

  1. What do you want to achieve?

I’ve recently been scripting my own ai. I’m having problems with the ai calculating the closest enemy.

  1. What is the issue? Include screenshots / videos if possible!

  2. What solutions have you tried so far?

I’ve tried changing the scripts value’s and debugging. But nothing seems to work. I’m avoiding copy/pasting because I want to learn how to do it myself so I can be a good scripter.

local Ai = script.Parent



local function CalculateEnemy(character)
	local NewEnemy = character
	local CurrentEnemy 
	local NewEnemyDistance = (Ai.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude
	if NewEnemy and CurrentEnemy == nil then
		CurrentEnemy = NewEnemy
		
	elseif NewEnemy and CurrentEnemy then
		local CurrentEnemyDistance = (Ai.HumanoidRootPart.Position - CurrentEnemy.HumanoidRootPart.Position).Magnitude

		if CurrentEnemyDistance < NewEnemyDistance then
			CurrentEnemy = CurrentEnemy 
		elseif NewEnemyDistance < CurrentEnemyDistance then
			CurrentEnemy = NewEnemy
		end
	end
	return CurrentEnemy
end

local function MoveToEnemy()

end

local function Attack()

end


local function FindTarget()
	local CalulatedEnemy
	for i,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			local Humanoid = v:FindFirstChild("Humanoid")
			local Playier = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
			if not Playier then
				if v:FindFirstChild("Team") then
					if v.Team.Value ~= Ai.Team.Value then
						 CalulatedEnemy = CalculateEnemy(v)
					end
				end
			end
		end
	end
	return CalulatedEnemy
end


while task.wait(1) do
	local Enemy = FindTarget()
	print(Enemy)
end

Keep in mind this is a server script in a character named “Robot”, which is in the workspace. Also, ignore empty functions as those are a work in progress. I am mainly focusing on making the Ai calculate the closest enemy. The ai decides if a character is an enemy if it is NOT a player, and a strong value in it named “Team” is the same as the Ai’s team(in other words, it just only attacks npc’s that are NOT on its team).

1 Like

What problems are you having exactly, are there any error messages, or is the ai just not acting as expected? Quickly reading through the code there doesn’t seem to be any immediate issues.

(sorry for the long wait and overexplaining, currently in school rn) the ai is just not acting as intended with no errors. When if finds the closest enemy, it will lock onto the enemy. But when there is a enemy that is closer than the original enemy. It WON"T switch targets. The thing I’m trying to do is make it so the ai will switch targets to the closet enemy. In which its not doing that and is instead just locking into the first enemy it finds

I don’t quite understand this part:

local function CalculateEnemy(character)
	local NewEnemy = character
	local CurrentEnemy 
	local NewEnemyDistance = (Ai.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude
	if NewEnemy and CurrentEnemy == nil then
		CurrentEnemy = NewEnemy
	end

wouldn’t this if NewEnemy and CurrentEnemy == nil always be true?

@PeculiarMei is correct

Since you’re defining CurrentEnemy as nil every time this function is run, the condition is always true (assuming character is not nil)

You could either:

  1. Let current enemy be a global variable instead.

Such as:

local Ai = script.Parent
local CurrentEnemy = nil

local function CalculateEnemy(character)
   ...
end

...

Making sure to remove “local CurrentEnemy” from the calculate enemy function.

OR
2) Pass in the current enemy into the calculate enemy function every time:

local function CalculateEnemy(character, currentEnemy)
	local NewEnemy = character
	local NewEnemyDistance = (Ai.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude
	if NewEnemy and CurrentEnemy == nil then
		CurrentEnemy = NewEnemy
		
	elseif NewEnemy and CurrentEnemy then
		local CurrentEnemyDistance = (Ai.HumanoidRootPart.Position - CurrentEnemy.HumanoidRootPart.Position).Magnitude

		if CurrentEnemyDistance < NewEnemyDistance then
			CurrentEnemy = CurrentEnemy 
		elseif NewEnemyDistance < CurrentEnemyDistance then
			CurrentEnemy = NewEnemy
		end
	end
	return CurrentEnemy
end


local function FindTarget()
	local CalulatedEnemy
	for i,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			local Humanoid = v:FindFirstChild("Humanoid")
			local Playier = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
			if not Playier then
				if v:FindFirstChild("Team") then
					if v.Team.Value ~= Ai.Team.Value then
						 CalulatedEnemy = CalculateEnemy(v, CalulatedEnemy)
					end
				end
			end
		end
	end
	return CalulatedEnemy
end
2 Likes

(Apologies for the long wait) Thank you both @PeculiarMei and @81MasterGamer.

2 Likes