#Can anybody tell me why my npc is dumb?

1 messages · Page 1 of 1 (latest)

plush crag
#

I am using pathfinding service, the waypoints are LITERALLY on screen and the npc refuses to follow it, the other parts of the script im still working on. ive asked ai and im quite stumped. Please help

The script is the txt file

#

im a beginner to pathfinding

agile juniper
#

You made it that way

#

Qnd ai wont do shit

plush crag
#

so like what can i do

agile juniper
#

Idfk im on phone rn

#

Someone else gotta help

plush crag
#

dang

old solstice
#

Pathfinding finds the shortest point between your character and the npc

#

and the npc follows it

#

It might be a problem with how you visualized the npc movements

#

But I might be just jumping to conclusion

#

there wasn't any obstacles

#

infront of the npc

#

So if you could try like to make a wall and see if the npc tries to dodge it or not

#

@plush crag

plush crag
quick pikeBOT
#

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

plush crag
#

idk what i did wrong but its never following the waypoint path

#

@old solstice

old solstice
#

OHH

#

Your not waiting till the npc finished the movement

#

you should do

#

Humanoid.MoveToFinished:Wait()

#

@plush crag

plush crag
#

ohh thanks

#

but it kinda gets stuck if it keeps waiting

#

like if a wall suddenly appears its never gonna finish it

old solstice
#

You can add like a Timeout

#

if the npc doesn't reach the player after like some times

#

you recompute the path

plush crag
#

npcHum:MoveTo(waypoint.Position)
timeOut = npcHum.MoveToFinished:Wait()
if not timeOut then
findPath(npc, target)
timeOut:Disconnect()
break

        end

like this?

old solstice
plush crag
#

huh wdym

old solstice
#

after like 40 seconds

#

you check if the npc

#

has reached the player

#

if it hasn't you just recompute the path, using find path

plush crag
#

how do i implement that

old solstice
#

You can just do for example

task.delay(40, function()
  Distance = --Distance between player and npc (magnitude)
  if Distance > 20 and Attempts < 5 then -- You might want to add a certain number of attempts
    Attempts += 1
    findPath()
  end
end)
unborn seal
#

but also you correct, movetofinished is missing so it just instantly skips to the final waypoint 👍

old solstice
unborn seal
#

so that movetofinished can handle any kind of move

#

plus the 8 second timeout innate to humanoid:moveto

old solstice
unborn seal
old solstice
unborn seal
unborn seal
#

you don't have to repeatedly call moveto if you handle movetofinished properly

old solstice
#
    local success, errorMessage = pcall(function()
        path:ComputeAsync(character.PrimaryPart.Position, destination)
    end)

    if success and path.Status == Enum.PathStatus.Success then
        -- Get the path waypoints
        waypoints = path:GetWaypoints()

        -- Detect if path becomes blocked
        blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
            -- Check if the obstacle is further down the path
            if blockedWaypointIndex >= nextWaypointIndex then
                -- Stop detecting path blockage until path is re-computed
                blockedConnection:Disconnect()
                -- Call function to re-compute new path
                followPath(destination)
            end
        end)

        -- Detect when movement to next waypoint is complete
        if not reachedConnection then
            reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
                if reached and nextWaypointIndex < #waypoints then
                    -- Increase waypoint index and move to next waypoint
                    nextWaypointIndex += 1
                    humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
                else
                    reachedConnection:Disconnect()
                    blockedConnection:Disconnect()
                end
            end)
        end

        -- Initially move to second waypoint (first waypoint is path start; skip it)
        nextWaypointIndex = 2
        humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
    else
        warn("Path not computed!", errorMessage)
    end```
#

so this basically

unborn seal
#

yeah that

unborn seal
#

moving to the next waypoint should ideally always be handled in movetofinished.

unborn seal
old solstice
#

So instead of looping through the waypoints, you just use the movetofinished connection

unborn seal
old solstice
#

Now this is interesting, I get how you it might be a newb trap, since all the youtubers do the for loop thing

#

instead of actually doing it correctly

plush crag
#

whats the difference between iterating through it using a for loop, and just moving to the next waypoint?

unborn seal
#

or you're stuck waiting for a while until it reaches the next waypoint and then maybe, so the whole thing runs slow

#

big newb trap, those threads show up all the time

old solstice
unborn seal
#

i'm no expert, but i did spend a few weeks messing with the pathfinding for my first public game, a monster-in-a-maze game

old solstice
unborn seal
#

i've released 2 fingerguns

old solstice
#

But the project I'm working on is really exciting

unborn seal
#

they're not good games, but they are games and completely functional aside from maybe a stray bug here and there

#

i needed the practice :p

old solstice
old solstice
plush crag
#

so i should just index through the next waypoint, right

#

does that work when handling multiple npcs on a single script?

old solstice
unborn seal
#

so a for loop is not the way to go.

old solstice
#

I've seen alot of people with that zombie problem especially

unborn seal
old solstice
unborn seal
# old solstice I've seen alot of people with that zombie problem especially

not surprised. the short of it is you are facing 2 problems, the first is the fact that pathfindingservice takes time to generate a new path, very easy to solve that by just making the npc run directly at the player if they're in line of sight (otherwise use pathfinding). the second part is much, much harder because of latency, your character on your screen has moved by the time an npc (with infinite movespeed) reached your last known position on the server (and this eventually appears on your screen). classic prediction problem.

#

no simple solutions.

#

just having the first part of that solved is usually good enough though, but the hitboxes might be a little off

old solstice
#

and use spatial query or raycasting

#

for the hitdetection

#

instead of touched, though ofcourse nobody really uses it for hitdetection

unborn seal
#

coz while your character moves on your screen, you'll be standing still on the server, so to speak.

old solstice
unborn seal
#

since the server sees the world in the past, and so does your client for everything other than your client and allat

#

idk im not going into it here, prediction is a big subject with no easy answers.

plush crag
#

does having the pathfinding function run continously in a while loop interfere with anything? or is it just the regular way of having active pathfinding

old solstice
#

Maybe not resource heavy, since tbh, I wouldn't know the algorithm for it even if I tried 👀

old solstice
#

Just make sure it doesn't compute a path

#

unless the npc is far from the player

#

You don't want it to endlessly compute a path forever

plush crag
#

does running npcs on a local script desync for other players? Like i have information in the server script of the position it's supposed to go, then firing it to all clients to compute the pathfinding and spawning

quick pikeBOT
#

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

unborn seal
#

the cheapest solution there is to give the player network ownership of the zombie and do the pathing on the client, but that's not always practical

#

...and extremely vulnerable to exploiters

#

lol boss.position=vector3.new(0,-10000,0) --death plane

old solstice
old solstice
#

They wouldn't even think of that line

unborn seal
#

it's a shame that roblox's efforts to lock down the platform and restrict things like access to coregui and debug.getregistry is what enables exploiters to run so rampant unchecked, makes it so much harder to make anticheats

unborn seal
old solstice
#

But ig, that still wouldn't prevent the problem

unborn seal
#

...and probably charge robux for it

old solstice
old solstice
#

though just me saying that would probably get me banned

unborn seal
#

if you're gonna hack anything, http://hackthissite.org or go screw with singleplayer games. keep it out of multiplayer.

old solstice
unborn seal
#

discord has terms against helping anyone with disassembly so i can't give you much more than that

old solstice
#

Thanks anyways

#

Tbh, you could make money out of finding backdoors

#

in game

#

or things that are hackable

unborn seal
#

and people do Thumbs

old solstice
#

Anyways it was nice having this convo with you