How good/efficient is my following AI code?

  • What does the code do and what are you not satisfied with?
    The code is supposed to make a Dummy follow and kill players in range.

  • What potential improvements have you considered?
    I tried to organise the code by adding sections of used items.

  • How (specifically) do you want to improve the code?
    I want to make it more efficient, I really don’t know how to organise code properly and I need some tips on how to make it work and look better.

Okay, so to make the main thing work is by putting a while wait() loop and letting all of the code run inside of it.

Then I added some variables and functions inside that might be changed in the mid-game, if I want to configure something while developing.

Code:

-- Name: 		Finding Script
-- Written by: 	MARKedGamingR / MarkedThing
-- Usage: 		Pathfinding to nearest player.

-- Originally Created: 	26.3.2021
-- Recreated:			29.3.2021
-- Updated: 			29.3.2021	15:44, Jerusalem Time



--// Services //--
local SERVICES = {
	Tween				=	game:GetService("TweenService")
}



--// Variables //--
--// Core
local Model			=	script.Parent
local Primary		=	Model.PrimaryPart
local ModelHuman	=	Model.Humanoid


--// Other
local Table_Folders = {
	Points	=	Model.Points,
	Config	=	Model.Config,
	Debug	=	Model.Debug
}

local IdleSound		=	Primary.Idle
local ChaseSound	=	Primary.Chase


--// Toggles
local Target_View	=	nil
local Target_Chase	=	nil
local Target_Point	=	nil
local Tweening		=	false


--// Animations
local Table_Anim = {
	Idle	=	ModelHuman:LoadAnimation(script.Idle),
	Walk	=	ModelHuman:LoadAnimation(script.Walk)
}


--// Tweening
local function movePrimary(Target,Studs)
	Primary.CFrame = CFrame.new(Primary.CFrame.Position,Target.CFrame.Position) + Primary.CFrame.LookVector * (Studs * 12)
end


--// Debug
local Display_Kill	=	Table_Folders.Debug.KillRad
local Display_Chase	=	Table_Folders.Debug.ChaseRad



--// Functions //--
Table_Anim.Idle:Play()


local function RemoveTarget(Part)
	if Target_View == Part then
		Target_View = nil
		Target_Point = nil
	end
	
	if Target_Chase == Part then
		Target_Chase = nil
	end
end


--// Main
while wait() do
	
	local Studs			=	Table_Folders.Config:GetAttribute("Studs")
	local ChaseRadius	=	Table_Folders.Config:GetAttribute("ChaseRadius")
	local KillRadius	=	Table_Folders.Config:GetAttribute("KillRadius")
	local DebugMode		=	Table_Folders.Config:GetAttribute("DebugMode")

	Display_Chase.Size = Vector3.new(ChaseRadius*2,ChaseRadius*2,ChaseRadius*2)
	Display_Kill.Size = Vector3.new(KillRadius*2,KillRadius*2,KillRadius*2)

	if DebugMode == true then
		Display_Chase.Transparency = 0
		Display_Kill.Transparency = 0
		Primary.Transparency = 0

		Display_Chase.CFrame = Primary.CFrame
		Display_Kill.CFrame = Primary.CFrame
		
		for i,Point in pairs(Table_Folders.Points:GetChildren()) do
			Point.Transparency = 0
		end
	else
		Display_Chase.Transparency = 1
		Display_Kill.Transparency = 1
		Primary.Transparency = 1
		for i,Point in pairs(Table_Folders.Points:GetChildren()) do
			Point.Transparency = 1
		end
	end
	
	
	
	
	for i,Player in pairs(game.Players:GetChildren()) do
		
		local Character	=	Player.Character
		local Humanoid	=	Character:FindFirstChildOfClass("Humanoid")
		local RootPart	=	Character:FindFirstChild("HumanoidRootPart")
		local Light		=	Character:FindFirstChild("Light")
		
		
		if Character and Humanoid and RootPart and Light then
			local RootMag = (RootPart.Position - Primary.Position).Magnitude
			
			
			--// Target View
			if Humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
				if Target_View == nil and Target_Chase == nil then
					Target_View = RootPart
				elseif Target_View and Target_Chase == nil then					
					if RootMag < (Target_View.Position - Primary.Position).Magnitude then
						Target_View = RootPart
					end
					
					--// Target Point
					for i,Point in pairs(Table_Folders.Points:GetChildren()) do
						local PointMag = (Point.Position - Target_View.Position).Magnitude
						
						if Target_Point == nil then
							Target_Point = Point
						else
							if PointMag < (Target_Point.Position - Target_View.Position).Magnitude then
								Target_Point = Point
							end
						end
					end
				end
				
				if RootMag <= ChaseRadius and Light.Value == true then
					if Target_Chase == nil then
						Target_Chase = RootPart
					elseif Target_Chase then
						if RootMag < (Target_Chase.Position - Primary.Position).Magnitude then
							Target_Chase = RootPart
						end
					end
				elseif RootMag > ChaseRadius and Target_Chase == RootPart then
					Target_Chase = nil
					ChaseSound.Playing = false
					if Table_Anim.Walk.IsPlaying == true then
						Table_Anim.Walk:Stop()
					end
				end
			elseif Humanoid:GetState() == Enum.HumanoidStateType.Dead then
				RemoveTarget(RootPart)
			end
			
			
			--// Handler
			if Target_Point and Target_Chase == nil and (Primary.Position - Target_Point.Position).Magnitude > 2 then
				movePrimary(Target_Point,Studs/2)
			elseif Target_Chase then
				movePrimary(Target_Chase,Studs)
				if Table_Anim.Walk.IsPlaying == false then
					Table_Anim.Walk:Play()
				end
				
				if ChaseSound.Playing == false then
					ChaseSound.Playing = true
				end
				
				if Target_Chase == RootPart and Humanoid.Health > 0 and (Target_Chase.Position - Primary.Position).Magnitude <= KillRadius then
					Humanoid.Health = 0
					ChaseSound.Playing = false
					
					if Table_Anim.Walk.IsPlaying == true then
						Table_Anim.Walk:Stop()
					end
					
					local KillSound = Primary.KillSound:Clone()
					KillSound.Parent = Target_Chase
					KillSound:Play()

					local KillAttach = Primary.KillAttach:Clone()
					KillAttach.Parent = Target_Chase
					KillAttach.Emitter:Emit()

					Target_Chase = nil
				end
			end
		end
	end

	Primary.Light.Brightness = Random.new():NextNumber(0,2)
	Primary.Transparency = 1
end

Again, I need some feedback on how to make it look better and if there are any problems, please point it out.

P.S: I’m new on the dev forum, Heya!

1 Like

How many ai do you need? If you only need 1 then mostly anything should work.
Spawn as many ai as you think there is going to be and see if it lags.

2 Likes

There is only 1 AI for the entire game.
And no, it doesn’t lag at all. I just need to know how or if I should improve the code.

If it works it works no need for improvements

1 Like

Hi,

I am not totally inside your code, but I don’t get why this should run all the time. Once the transparency is set, you don’t need to change it again if I’m right. This can save a lot of server power needed, since it only needs to check once, and not continiously.
Try to only run things that needs to be changed every time, every time. A good rule of thumb is to have as little operations as possible!

The reason for that is if I want to activate the Debug Mode inside the game, so I do the following in the developer tab in-game:

game.ServerStorage.DebugMode.Value = true

I thought about using a .Changed event on the value inside the script, but I don’t know if I should do that or just leave the “if” inside the loop since it doesn’t run anyway.
Also I’m going to remake the scripts soon when I’ll have some free time…

Here’s the amoizing game btw
Dairy Product™ - Roblox

You should definitely make that a .changed event. You are right that the if statement is not run, but the else statement is:

This will set every transparency every moment. Altough it won’t actually change the transparency, the code is executed. Especially for the points folder (idk how much points are in there), if there are a lot of points in there, it could seriously ‘eat up’ your game

1 Like

bit off-topic but

are you using a plugin for the info at the top, with the name, written by and other stuff?

No, I only put those in important scripts.
Would be a cool plugin though!

1 Like

Actually you are right, thank you!

1 Like