AI system feedback

Hello, I made an ai system from scratch for the first time.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFolder = ReplicatedStorage:WaitForChild("Remotes")
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local GameClient = ReplicatedStorage:WaitForChild("GameClient")
local Assets = GameClient:WaitForChild("Assets")
local AnimationsFolder = Assets.Animations
local RunService = game:GetService("RunService")
local Walkdebounce = false
local CurrentTarget = nil
local Distance = 45
local AttackRadius = 4

local function GetClosestPlayer()
	local Closest = nil
	for Index,Player in pairs(Players:GetChildren()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local newDistance = (Character:WaitForChild("HumanoidRootPart").Position - HumanoidRootPart.Position).Magnitude
		if newDistance <= Distance and Character:WaitForChild("Humanoid").Health > 0 then
			Closest = Player
		end
	end
	return Closest
end

function CheckInRange()
	if CurrentTarget then
		local Character = CurrentTarget.Character or CurrentTarget.CharacterAdded:Wait()
		local newDistance = (Character:WaitForChild("HumanoidRootPart").Position - HumanoidRootPart.Position).Magnitude
		if newDistance <= Distance and Character:WaitForChild("Humanoid").Health > 0 then
			return true
		end
	end
end

function CheckInAttackRadius()
	if CurrentTarget then
		local Character = CurrentTarget.Character or CurrentTarget.CharacterAdded:Wait()
		local newDistance = (Character:WaitForChild("HumanoidRootPart").Position - HumanoidRootPart.Position).Magnitude
		if newDistance <= AttackRadius and Character:WaitForChild("Humanoid").Health > 0 then
			return true
		end
	end
end

RunService.Stepped:Connect(function()
	local Player = GetClosestPlayer()
	if Player then
		CurrentTarget = Player
	end
	if CurrentTarget and CheckInRange() and not CheckInAttackRadius() then

		if not Walkdebounce then
			Walkdebounce = true
			local Character = Player.Character or Player.CharacterAdded:Wait()
			Humanoid:MoveTo(Character:WaitForChild("HumanoidRootPart").Position)
			local Track = Humanoid:WaitForChild("Animator"):LoadAnimation(AnimationsFolder["Mushroom Move"])
			Track:Play()
			Track.Stopped:Wait()
			Walkdebounce = false
		end

	elseif not CheckInRange() and not CheckInAttackRadius() then

		Humanoid:MoveTo(HumanoidRootPart.Position)

	elseif CheckInRange() and CheckInAttackRadius() then

		if not Walkdebounce then
			if CurrentTarget then
				Walkdebounce = true
				local Character = Player.Character or Player.CharacterAdded:Wait()
				local Track = Humanoid:WaitForChild("Animator"):LoadAnimation(AnimationsFolder["Mushroom Attack"])
				Track:Play()
				Track.Stopped:Wait()
				if CheckInRange() and CheckInAttackRadius() then
					remoteFolder.DamageTaken:FireClient(CurrentTarget,true,GameClient.Assets.Sounds.Punch)
				end
				wait(.5)
				Walkdebounce = false
			end
		end
	end
end)

This is the main script help inside of the Character.
Any feedback to help improve my code is greatly appreciated!
Iā€™m not sure if I should run this every frame?
Are there any better ways of doing it?

1 Like

I read the code but maybe you canmake it a little bit smaller and also maybe you can put the animations in a function to make it les complicated. Overall a fine code I think

Oh also you can make it like run every 0.2 seconds but make it 0 seconds when the zombie gets closer. This can make your code complicated but it may less lag if it lags with too many zombies

Also to make the code shorter you can fuse CheckInAttackRadius into GetClosestPlayer by setting newdistance to AttackRaidus by default at the start.(this should probably work because when I was doing something like this I used this method but I havent tested this one yet)

Also its not an AI its a bot

1 Like

or: how to clutter up local values; it is kind of an unnecessary amount of locals, as you only really need your remote function/event and the basic values; the rest is rather simple to index

1 Like