#Mob AI help

1 messages · Page 1 of 1 (latest)

fresh parcel
#

So my code right now looks like this (For the activation of it) I was wondering if there are any mistakes, better methods I could use.
Also if anyone can help me think of ways to prevent mobs from teleporting when I slow them down (Latency issue).
This will be quite long...

task.spawn(function()
    while task.wait(1) do
        for _, Mob in CollectionService:GetTagged("ActiveMob") do

            if Mob.Parent.Name ~= "MobsInRegion" then continue end

            local HumanoidRootPart = Mob:FindFirstChild("HumanoidRootPart")
            local Info = Mob:FindFirstChild("Info")
            local CurrentTarget = Info and Info:FindFirstChild("CurrentTarget")

            if not HumanoidRootPart or not CurrentTarget then continue end

            local ClosestPlayer = nil
            local ShortestDistance = math.huge

            for _, Player in Players:GetPlayers() do
                local Character = Player.Character
                local PlayerHRP = Character and Character:FindFirstChild("HumanoidRootPart")
                if not PlayerHRP then continue end

                local Distance = (HumanoidRootPart.Position - PlayerHRP.Position).Magnitude
                local Aggro = Distance
                if Info.CurrentTarget.Value == Character and Info.FleeOrFight.Value == 2 then Aggro *= 0.3 end
                if Aggro > Info.DetectionRange.Value then continue end
                if Aggro < ShortestDistance then
                    ShortestDistance = Distance
                    ClosestPlayer = Character
                end
            end

            if Info.Aggression.Value == 2 and ClosestPlayer and ClosestPlayer ~= CurrentTarget.Value then
                DisconnectTargetUpdates(Mob, Info)
                CurrentTarget.Value = ClosestPlayer
                local MobDetails = GetMobDetails(Mob)
                StartChaseCoroutine(Mob, MobDetails)
            elseif not ClosestPlayer and Info.AIState.Value ~= 1 then
                DisconnectTargetUpdates(Mob)
                CurrentTarget.Value = nil
                local MobDetails = GetMobDetails(Mob)
                StartWanderCoroutine(Mob, MobDetails)
            end

        end

    end
end)
#

This is just the first segment of the code currently

rugged urchin
#

you can just use Distance

fresh parcel
twilit shoreBOT
#

studio** You are now Level 23! **studio

fresh parcel
#

When it picks on one target

#

It will have bigger aggro on that one

#

Also we will have other variables thrown in later

fresh parcel
#

So any ideas so far in this segment?

#

If there are no improvements I'll move to the next

vernal birch
#

just dont use humanoid.movetofinished:wait() and ur doin a few things right

fresh parcel
#

I meant haven't sent it

#

I'll get to it in a bit

simple prawn
#

Instead of doing while task.wait(1) do, which is an infinite unending loop, make a script that binds to RunService.Heartbeat

local RunService = game:GetService("RunService")
--Stuff inbetween
RunService.Heartbeat:Connect(function()
  --your function here
end)

Instead of getting all tagged mobs each time, just keep a table of all valid mobs.
You can get an event, like heartbeat, and connect to it, when you add or remove tags

local CollectionService = game:GetService("CollectionService")
local tableOfMobs = {}

--Make 2 events for when mobs get added or removed
local mobGotTagged = CollectionService:GetInstanceAddedSignal("YourMobTagHere")
local mobGotUnTagged = CollectionService:GetInstanceRemovedSignal("YourMobTagHere")

--Functions to add and remove mob table
local function addToMobTable(mob)
  table.insert(tableOfMobs, mob)
end

local function removeFromMobTable(mob)
  local mobToRemoveIndex = table.find(tableOfMobs, mob)
  table.remove(tableOfMobs, mobToRemoveIndex)
end

--Now you can connect to those like Heartbeat
mobGotTagged:Connect(addToMobTable)
mobGotTagged:Connect(removeFromMobTable)

--Initiate the mob list
tableOfMobs = CollectionService:GetTagged("YourMobTagHere")

This lets you avoid creating a new table each time to get all tagged mobs, and dynamically adds or removes from your current list.

#

just some minor pokes

fresh parcel
#

Ok these are kind of micro optimizations

#

I'll keep them in mind though

fresh parcel
#

The only thing I'll do is disconnect the mobs from the list by removing their tag

simple prawn
simple prawn
fresh parcel
#

Okay

#

Moving on next segment

#
function StartChaseCoroutine(Mob, MobDetails)
    local AttackRoutine = coroutine.create(function()
        KeepAttackingTarget(Mob, MobDetails)
    end)
    local ChaseRoutine = coroutine.create(function()
        ChaseTarget(Mob, MobDetails)
    end)
    MobDetails.Info.AIState.Value = 2
    ActiveTargetUpdates[Mob] = ChaseRoutine
    coroutine.resume(ChaseRoutine)
    ActiveAttacking[Mob] = AttackRoutine
    coroutine.resume(AttackRoutine)
end

function StartWanderCoroutine(Mob, MobDetails)
    local WanderRoutine = coroutine.create(function()
        Wander(Mob, MobDetails)
    end)
    ActiveTargetUpdates[Mob] = WanderRoutine
    MobDetails.Info.AIState.Value = 1
    coroutine.resume(WanderRoutine)
end

function DisconnectTargetUpdates(Mob)
    
    if ActiveAttacking[Mob] then
        coroutine.close(ActiveAttacking[Mob])
        ActiveAttacking[Mob] = nil
    end
    
    if ActiveTargetUpdates[Mob] then
        coroutine.close(ActiveTargetUpdates[Mob])
        ActiveTargetUpdates[Mob] = nil
    end

    if ActivePaths[Mob] then
        ActivePaths[Mob] = nil
    end

    if ActiveMovements[Mob] then
        coroutine.close(ActiveMovements[Mob])
        ActiveMovements[Mob] = nil

        local Humanoid = Mob:FindFirstChildOfClass("Humanoid")
        local HumanoidRootPart = Mob:FindFirstChild("HumanoidRootPart")
        if Humanoid and HumanoidRootPart then
            Humanoid:MoveTo(HumanoidRootPart.Position)
        end
    end
end
#

I'm not very familiar with coroutines