#AI problem

1 messages · Page 1 of 1 (latest)

true crane
#

I made an AI but it start normaly but after a while it dont attack and dont stop in it range:local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")

local Modules = ReplicatedStorage.Modules
local Events = ReplicatedStorage.Events

local AnimationEvent = Events.AnimationEvent

local AttackModules = Modules.Attacks

local troop = {}

local path = nil
local waypoints = nil
local NPC = nil
local Configuration = nil

local isWalking = false

local lastPos = nil

local function Attack(target)
local moduleObj = AttackModules:FindFirstChild(NPC.Name)
if moduleObj then
local module = require(moduleObj)
module.Attack(target, NPC)
return
else
warn("No attack module for " .. NPC.Name)
return
end
end

local function CreatePath(target)
local PlayerPath = PathFindingService:CreatePath({
AgentCanJump = true,
})
local targetHRP = target and target:FindFirstChild("HumanoidRootPart")
if not targetHRP then return nil end

local success, err = pcall(function()
    PlayerPath:ComputeAsync(NPC.HumanoidRootPart.Position, targetHRP.Position)
end)

if not success or PlayerPath.Status ~= Enum.PathStatus.Success then
    return nil
end

return PlayerPath

end

local function GetClosestEnemy()
local closest = nil
local shortestDist = math.huge

for _, enemy in ipairs(workspace.Enemys:GetChildren()) do
    if enemy and enemy:FindFirstChild("HumanoidRootPart") then
        local dist = (enemy.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude
        if dist < shortestDist and enemy.Humanoid.Health > 0 then
            shortestDist = dist
            closest = enemy
        end
    end
end

return closest

end

#

and rest of code:function troop.SetAI(obj)
if obj and obj.Parent and obj:GetAttribute("Side") == "Troops" then
local Humanoid = obj:FindFirstChild("Humanoid")
NPC = obj
lastPos = NPC.HumanoidRootPart.Position
Configuration = obj.Configuration

    task.spawn(function()
        while NPC and NPC.Parent do
            local target = GetClosestEnemy()

            if target then
                path = CreatePath(target)

                if path then
                    waypoints = path:GetWaypoints()

                    for _, waypoint in ipairs(waypoints) do
                        if (target.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude > NPC.Configuration.Range.Value then
                            Humanoid:MoveTo(waypoint.Position)

                            if waypoint.Action == Enum.PathWaypointAction.Jump then
                                Humanoid.Jump = true
                            end
                        else
                            Humanoid:MoveTo(NPC.HumanoidRootPart.Position)
                            path = nil
                            waypoints = nil
                        end
                    end
                end
            else
                -- Jeśli brak celu, NPC stoi w miejscu
                Attack(target)
                Humanoid:MoveTo(obj.HumanoidRootPart.Position)
                path = nil
                waypoints = nil
            end

            RunService.Heartbeat:Wait()
        end
    end)

    -- 🔄 Loop animacji chodzenia
    task.spawn(function()
        while NPC and NPC.Parent do
            local currentPos = NPC.HumanoidRootPart.Position
            local dist = (currentPos - lastPos).Magnitude

            if dist > 0.2 then
                if not isWalking then
                    isWalking = true
                    AnimationEvent:FireAllClients(NPC,"Walk","Play")
                    AnimationEvent:FireAllClients(NPC,"Idle","Stop")
                end
            else
                if isWalking then
                    isWalking = false
                    AnimationEvent:FireAllClients(NPC,"Walk","Stop")
                    AnimationEvent:FireAllClients(NPC,"Idle","Play")
                end
            end

            lastPos = currentPos
            task.wait(0.1)
        end
    end)
end

end

return troop