You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I am making a pathfinding code where the AI checks out a spot by using PathfindingService.
-
What is the issue? Include screenshots / videos if possible!
Well, the code seems to be running too fast. In fact, I have added an extra boolean debounce to see if it would change anything, but no:
local pathfindingConnection:RBXScriptConnection = RunService.Heartbeat:Connect(function(dt:number)
--Handling sounds first here, not relevant to the issue so I've left it out
local nearestSound:Vector3 = getNearestSound(allNearestSounds) --This function is not relevant to the issue at all. all it does is literally sorting out a table and getting the nearest position
if nearestSound and ((currentSoundPath == nil) and (not currentSoundPathDebounce)) then
print("About to SoundPathing", (currentSoundPath == nil), (not currentSoundPathDebounce), (currentSoundPath == nil) and (not currentSoundPathDebounce))
local path:Path = PathfindingService:FindPathAsync(rigHRP.Position, nearestSound) --I know that
--I should use the better and newer version of the pathfinding function, however since I am playing around with pathfinding for the first time I thought I should start out small. as long as I don't fix this issue I don't see a reason to switch methods
currentSoundPath = path
currentSoundPathDebounce = true
if path.Status == Enum.PathStatus.Success then
if not walkingTrack.IsPlaying then walkingTrack:Play() end
--Avoid going to path when in the air
if humanoid.FloorMaterial ~= Enum.Material.Air then
pathfindingDebounce = true
local waypoints:{PathWaypoint} = path:GetWaypoints()
--Visualises the path
for i,waypoint in waypoints do
local waypointPosition:Vector3 = waypoint.Position
local t = Instance.new("Part")
t.Parent = showingMovesFolder
t.Size = Vector3.one
t.Shape = Enum.PartType.Ball
t.CanCollide = false
t.CanQuery = false
t.Anchored = true
t.Position = waypointPosition
t.BrickColor = BrickColor.Random()
end
for i,waypoint in waypoints do
local waypointPosition:Vector3 = waypoint.Position
local waypointAction:Enum.PathWaypointAction = waypoint.Action
if waypointAction == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypointPosition)
humanoid.MoveToFinished:Wait()
if i == #waypoints then
print("Path done!")
currentSoundPath = nil
currentSoundPathDebounce = false
if walkingTrack.IsPlaying then
walkingTrack:Stop()
idleTrack:Play()
end
end
end
end
else
stopPathfinding()
end
return
end
currentSoundPath
is a nil
variable created outside of the Heartbeat function, same with currentSoundPathDebounce
, but that oneâs a boolean set to false
.
As you can see, the ((currentSoundPath == nil) and (not currentSoundPathDebounce))
does basically nothing: somehow, it takes multiple frames, 5 in this case, before the boolean and path are actually set. It shouldnât be the case: as soon as the if statement allows it, the path and debounce being set should immediately, for the next frame, make it impossible to run again before the NPC is done checking out 1 place first. Another very annoying fact is how the Path done!
print statement prints out 5 times in a row, also instantaneously, basically ignoring the MoveToFinished:Wait()
yield. The NPC doesnât move from itâs place at all, MoveToFinished shouldnât fire if the NPC hasnât made a single step!!!
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Checking out anything available, but as usual, no luck there.
Does anyone have enough experience with NPC or just in general to spot what the issue is? Am I doing the debounce in the wrong way? What is it that I should do otherwise instead? Thanks in advance, any comment is welcome: I feel like going crazy right now over this small issue