#Broken AI for pathfinding and chasing player.

1 messages · Page 1 of 1 (latest)

autumn trout
#

I have a AI script for a character that detects players and does some patroling from parts to parts randomly. The problem I noticed is that over time the AI and chasing becomes laggy for the npc.

#

this one is the chasing

#

here's the script for the ai

#

it has many functions

noble frost
#

so the problem is like

#

he sometimes bugs?

autumn trout
#

not sometimes

#

but when the script continues running after a long time the ai and chase starts to bug out

noble frost
#

uhh so

#

your using runservice.heartbeat by overloading it by using it too much

autumn trout
#

that's what's causing it to bug out?

noble frost
#

your running the detection logic every single frame like 60+ times per second which includes the distance calculations for all the players and fov angle calculations using math.acos i think

#

thats like

#

expensive

#

or

#

also our main loop calls patrol() or chase() continuously without proper delays, potentially creating new paths constantly

#

your

#

wtf

#

@autumn trout

autumn trout
#

what

noble frost
#

your main loop calls patrol() or chase() continuously without proper delays, potentially creating new paths constantly

#

also

mental sluiceBOT
#

studio** You are now Level 1! **studio

fervent raft
#

congrats

autumn trout
#

so i should give it like a wait time for these functions

noble frost
#

your calling chase() both in hearbeat detection and in the main loop btw

autumn trout
#

so what should i do?

noble frost
#

hmm

autumn trout
#

i got to a conclusion that the functions need a wait time of eg 0.5 sec?

noble frost
#

no wait

#

replace the hearbeat logic with timed loop

#

like

#

spawn(function()
while true do
-- your detection script functions
task.wait(0.2)
end)

#

end)

#

smth liket his

#

also fix ur main loop

#

spawn(function()
while true do
if not chasing then
patrol()
end
task.wait(0.1) -- add this for proper delay
end
end)

autumn trout
#

this is all i need to do then?

noble frost
#

add a pathcooldown

#

idk try those

autumn trout
#

pathcooldown? as in a function that makes a cooldown whenever the model trouches a part of the patrol parts?

noble frost
#

give me like

#

10 mins

#

ima check the script

#

and fix it for u

autumn trout
#

alr thx

noble frost
#

try this

#

@autumn trout

#

anything?

autumn trout
#

imma put it in

#

i think you forgot to close it?

noble frost
#

ops

noble frost
#

its closed

#

add end)

autumn trout
#

let me try again

#

alr

noble frost
#

anything?

mental sluiceBOT
#

studio** You are now Level 2! **studio

autumn trout
#

this is the chase

#

i was hoping to make it like piggy ai tho piggy ai knows where the player is

noble frost
#

o

#

i still have no idea with pathfinding

#

i suck with those

#

but

#

i can borrow you mine