#Can anybody tell me why my npc is dumb?
1 messages · Page 1 of 1 (latest)
so like what can i do
dang
From what I can see in the video, your pathfinding script, is doing what its supposed to do
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
** You are now Level 1! **
OHH
Your not waiting till the npc finished the movement
you should do
Humanoid.MoveToFinished:Wait()
@plush crag
ohh thanks
but it kinda gets stuck if it keeps waiting
like if a wall suddenly appears its never gonna finish it
just recompute the path
You can add like a Timeout
if the npc doesn't reach the player after like some times
you recompute the path
npcHum:MoveTo(waypoint.Position)
timeOut = npcHum.MoveToFinished:Wait()
if not timeOut then
findPath(npc, target)
timeOut:Disconnect()
break
end
like this?
Bro timeout in like time
huh wdym
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
how do i implement that
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)
this is a huge, massive newb trap. recommend this only to debug stuff, but it should never be left that way, always do it properly
but also you correct, movetofinished is missing so it just instantly skips to the final waypoint 👍
Lol I don't mess with pathfinding much, what is the correct way
you need to do this annoying state-sensitive thing it's kinda long
so that movetofinished can handle any kind of move
plus the 8 second timeout innate to humanoid:moveto
Oh so there is an 8 second timeout anyways
specifics and code samples found here https://create.roblox.com/docs/characters/pathfinding
I see, so thats inside of the docs ye?
the page i just linked shows a full and complete pathfinding pattern
https://create.roblox.com/docs/reference/engine/classes/Humanoid#MoveTo
The reach goal state of a humanoid will timeout after 8 seconds if it doesn't reach its goal. This is done so that NPCs won't get stuck waiting for Humanoid.MoveToFinished to fire. If you don't want this to happen, you should repeatedly call MoveTo so that the timeout will keep resetting.
you don't have to repeatedly call moveto if you handle movetofinished properly
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
yeah that
this is probably the most important part lua 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
moving to the next waypoint should ideally always be handled in movetofinished.
getting a constantly changing path into this function is a bit annoying and kinda state-sensitive so it's kinda long
So instead of looping through the waypoints, you just use the movetofinished connection
yea and that is what does the looping, since it essentially calls itself
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
whats the difference between iterating through it using a for loop, and just moving to the next waypoint?
yeah you can't change how the npc moves in the middle of a pathfind
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
I see what you mean now, I see you texting about this newb trap all the time, but since I don't use pathfinding much, I never really got what you mean, anyways thanks for explaining
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
I'm yet to release a game 😭
i've released 2 
But the project I'm working on is really exciting
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
So basically, it just prevents delays caused by being blocked randomly which is the issue you had highlighted before
Tbh, its better than nothing, finishing a project helps you gain alot of experience, though I can't really say since my hours on studio where either working with percentages with other groups, and like small commisions here and there
so i should just index through the next waypoint, right
does that work when handling multiple npcs on a single script?
Using humanoid:movetofinished, yes
you can't interrupt the movement like when the path updates (like the player moves) or you end up with other funky issues where attacks are slow or something because it's one gigantic loop with really long waits in the middle of it. it causes a whole range of problems.
so a for loop is not the way to go.
What I'm wondering though is, lets say the player is moving and the zombie is faster than the player, will the zombie be able to reach the player, or because there is a delay, especially if you doing pathfinding through the server, the zombie will never reach the player
I've seen alot of people with that zombie problem especially
that's a different problem, and much harder to solve 
mhm, how would you go about solving it
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
You'd probably have to give the zombie longer range won't you
and use spatial query or raycasting
for the hitdetection
instead of touched, though ofcourse nobody really uses it for hitdetection
naw the zombie will hit you, it's just, it'll appear as though you're being hit from a larger distance than you might expect.
coz while your character moves on your screen, you'll be standing still on the server, so to speak.
Ye because it takes time for the character to replicate on the server
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.
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
Tbh, predicting where the player is about to move, is going to be kind of tough, and resource heavy if you want it to be accurate
Maybe not resource heavy, since tbh, I wouldn't know the algorithm for it even if I tried 👀
Not a problem I believe
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
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
** You are now Level 2! **
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
ahh exploiters, can't we just delete the from the earth
Like most of the exploiters in roblox are braindead
They wouldn't even think of that line
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
nah they would i assure you
But I think that most exploiters use already made scripts really
But ig, that still wouldn't prevent the problem
if your game gets big enough, someone will eventually make instantbosskill.lua for the skiddies
...and probably charge robux for it
For your first question, the npc would not even move on the other player's screen beside your own client, now for the replication idea its not bad, though I do not know if the npc will follow the same path on each player's screen
Like I find hacking into games really interesting actually, like I would like testing different script of my own, to do some finicky stuff in game
though just me saying that would probably get me banned
if you're gonna hack anything, http://hackthissite.org or go screw with singleplayer games. keep it out of multiplayer.
Yup, wouldn't wanna ruin the experience for other people
discord has terms against helping anyone with disassembly so i can't give you much more than that
Thanks anyways
Tbh, you could make money out of finding backdoors
in game
or things that are hackable
and people do 
Anyways it was nice having this convo with you