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?