so, i recently was making a rooms inspired game, well, in the part that i needed to program the entities, i choose a random entity for test. well, in the entity movement moment, the entity keep getting stuck or/and stop by for no reason.
i tried many solutions, such as PathfindingService, Repeat Until, and even, Wait until the entity get to his point. but no luck
some of the solutions the entity movement gets wrose, like: disappears earlier
or some of them don,t delete the entity from the workspace
here is the A-010’s full script
local ms = {}
function ms.Monster()
local Monster = script.Parent
local Pathfinding = game:GetService("PathfindingService")
local Humanoid = Monster.Humanoid
Monster:PivotTo(workspace.MonsterPoints.MonsterSpawnpoint.CFrame)
Monster.Effects.Sound:Play()
wait(1)
Humanoid:MoveTo(workspace.MonsterPoints.MonsterGoalpoint.Position)
Humanoid.MoveToFinished:Wait()
wait(1)
Monster:Destroy()
end
return ms
also this script is a Module script, wich is callen when some server script knows is time to spawn that type of monter.
like this:
local Rarity = math.random(1,3)
local CanSpawn = math.random(0,Rarity)
if CanSpawn == 1 then
local Monster = ChoosenMonster:Clone()
Monster.Parent = workspace.UsedMonsters
print("Spawned")
local ItsScript = require(Monster.MonsterScript)
ItsScript.Monster()
if SECValue == not 20 then
SECValue = SECValue + 1
wait(1)
end
end
In the A-010 code, I would recommend replacing the default MoveTo() function with this code instead:
local Distance
function MoveNPC(v)
Humanoid:MoveTo(v.Position)
Humanoid.MoveToFinished:Wait()
Distance = (RootPart.Position - v.Position).Magnitude
if(Distance < 2) then
return
else
return MoveNPC(v)
end
end
Inside of “v”, you just add the point you are trying to move the enemy too.
I used this code before, and it works way better than Roblox’s default MoveTo() code. If this doesn’t help, then I am not sure about what the problem could be.
Also, I would recommend creating another function that would detect when an enemy has stopped MovingTo the given point, just incase you want to move the enemy to another point.
Something like this should maybe work? (idk I’m not near my laptop rn):
function MoveToFinished(p)
Humanoid.MoveToFinished:Wait()
If (Distance < 2) then
print(“Ended.”)
return MoveNPC(p)
end
end
Here is the source I got the MoveTo() function from, so you can learn more:
You’re probably gonna have to experiment with this part of the Move Code;
if(Distance < 2) then
return
else
return MoveNPC(v)
end
One of them detects when the enemy reaches the goal, (I’m pretty sure) I don’t know which line does though, im currently typing all of this on mobile, so I can’t test it out myself.
Maybe try adding a print which says (“Done walking.”) above one of the return lines.
Ex;
if(Distance < 2) then
return
else
print(“Done walking”)
return MoveNPC(v)
end
If it prints that, then that’s probably when the enemy has reached its goal, then you can destroy the enemy from there.
if(Distance < 2) then
return
else
Enemy:Remove()
return MoveNPC(v)
end
that problem was solved, but another problem as showed up, once if you advance a little bit, the monster dissapears in a specific room (usually room A-015), wich is not a position of MonsterGoalPoint
If you are experiencing issues with entities getting stuck or stopping for no reason when using the standard Roblox pathfinding system, here are a few possible solutions:
Adjust the PathfindingService settings:
Open the “PathfindingService” in the Explorer window.
Expand the settings and try adjusting the “AgentRadius”, “AgentHeight”, and “AgentCanJump” properties to better match the size and movement capabilities of your entities.
You can also try changing the “AgentMaxSlopeAngle” property to allow for steeper slopes.
Check for obstacles or incorrect terrain settings:
Make sure there are no obstacles or barriers in the path that could be causing the entities to get stuck.
Verify that your terrain settings are correct, such as ensuring that the terrain is walkable and that there are no gaps or holes.
Use custom waypoints or navigation meshes:
Instead of relying solely on the built-in pathfinding, you can manually create waypoints or a navigation mesh for your entities to follow.
This can give you more control over their movement and help avoid potential issues with the default pathfinding system.
Consider using a third-party pathfinding solution:
If the standard Roblox pathfinding system is not meeting your needs, you may want to explore third-party plugins or libraries that offer more advanced pathfinding algorithms.
These solutions can provide more flexibility and customization options for your entities’ movement.
Debug and analyze the issue:
Use print statements or output messages to track the entity’s position and any error messages that may occur during movement.
Check for any specific patterns or situations where the entity tends to get stuck or stop.
This can help you identify any specific issues or triggers that are causing the problem.
Remember to test and iterate on your solutions to find the one that works best for your specific scenario.
Could be that you misplaced one of the walk points where the enemy is supposed to be going to, or you might of misplaced the spawn location for when the enemy spawns in at the last room you entered.
Or it could be something wrong with one of your scripts.
the waypoint keeps changing position to the last door opened, like: a player opened the door, so the MonsterGoalPoint gets to the closed door, the rooms is randomly generated
i was testing something that delete the roomdoorsplacement, wich when a player opens a door, the script gets the randomly generated room’s door, and place into its position, i tried to delete every time when the room is generated and moved to workspace, i tried this and the monster doesn,t stops exactly on the door, it stopped into the room
a thing that i noticed in my script is that the script really think that there is the MonsterGoalPoint’s position, like, when this problem occurs, i updated my script to loop until to get into his given position, but the problem persists. that because the scripts thinks that is the given position
here is the updated A-010’s full script:
local ms = {}
function ms.Monster()
local Monster = script.Parent
local Humanoid = Monster.Humanoid
Monster:PivotTo(workspace.MonsterPoints.MonsterSpawnpoint.CFrame)
Monster.Effects.Sound2:Play()
Monster.Effects.Sound:Play()
wait(1)
function ms.MoveNPC(v)
repeat
Humanoid:MoveTo(v.Position)
Humanoid.MoveToFinished:Wait()
Humanoid.WalkToPart = workspace.MonsterPoints.MonsterSpawnpoint
local Distance = (Monster.Effects.Position - v.Position).Magnitude
if(Distance > 10) then
return
else
wait(1)
print("Goal Reached")
Monster:Destroy()
return ms.MoveNPC(v)
end
until (Distance > 10)
end
ms.MoveNPC(workspace.MonsterPoints.MonsterGoalpoint)
end
return ms
meaning by that, the problem is not actually the script, and is the given position that the script is invoking to MoveNPC() (actually the script don,t repeat)
i managed to find the solution by myself
there is a part of the script that calls return
ant this part wasn,t returning the function before
local ms = {}
function ms.Monster()
local Monster = script.Parent
local Humanoid = Monster.Humanoid
Monster:PivotTo(workspace.MonsterPoints.MonsterSpawnpoint.CFrame)
Monster.Effects.Sound2:Play()
Monster.Effects.Sound:Play()
wait(1)
function ms.MoveNPC()
local v = workspace.MonsterPoints.MonsterGoalpoint
Humanoid:MoveTo(v.Position)
Humanoid.MoveToFinished:Wait()
local Distance = (Monster.Effects.Position - v.Position).Magnitude
if(Distance >= [Minimum Position Here]) then
return --error spot
else
wait(1)
print(v.Position)
print("Goal Reached")
print(Monster.WorldPivot)
Monster:Destroy()
return ms.MoveNPC()
end
end
ms.MoveNPC()
end
return ms
after
local ms = {}
function ms.Monster()
local Monster = script.Parent
local Humanoid = Monster.Humanoid
Monster:PivotTo(workspace.MonsterPoints.MonsterSpawnpoint.CFrame)
Monster.Effects.Sound2:Play()
Monster.Effects.Sound:Play()
wait(1)
function ms.MoveNPC()
local v = workspace.MonsterPoints.MonsterGoalpoint
Humanoid:MoveTo(v.Position)
Humanoid.MoveToFinished:Wait()
local Distance = (Monster.Effects.Position - v.Position).Magnitude
if(Distance >= [Minimum Position Here]) then
return ms.MoveNPC() --error spot
else
wait(1)
print(v.Position)
print("Goal Reached")
print(Monster.WorldPivot)
Monster:Destroy()
return ms.MoveNPC()
end
end
ms.MoveNPC()
end
return ms
can you notice the difference?
if(Distance >= [Minimum Position Here]) then
return ms.MoveNPC() <--
else
this part was not returning the function
thats why the script don,t loop when not reached the goal