Entity getting stuck or/and stopping by for no reason

Hello

ok, this is my first forum

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

any help is appreciated.

9 Likes

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:

3 Likes

it seems that made the script better, but in some times, the entity don,t despawn after getting into his goal, but at least he don,t stops by

2 Likes

So you want the enemy to basically get destroyed once he reaches the walk point?

3 Likes

yes, because he passes by all the rooms and after he reached he disappears, like rush from doors

2 Likes

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

3 Likes

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

2 Likes

May I see a video of that issue?

2 Likes

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

3 Likes

sure

0:00 Where A-010 starts spawning
0:45 Where the problem starts to show up

if you see some delays is because my internet is kinda bad

4 Likes

hello WHOTEI
this is no longer a problem on my script
but thanks for your solution

1 Like

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.

1 Like

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

1 Like

I also fixed something small in your spawn script;

			
			if SECValue ~= 20 then
				SECValue = SECValue + 1
				wait(1)
			end
		end

2 Likes

Ohhh alright. Could be a position issue then? I’m not sure.

2 Likes

maybe, because:

Captura de tela 2023-08-06 132129

Look at this example, (the grey block is MonsterGoalPoint)
when a player opens a room door the MonsterGoalPoint moves to the last door, like this:

Captura de tela 2023-08-06 132418

and once the server script tells to the entity spawn, he stops by 1 or more rooms away (and also reaching exactly of a room door)

Captura de tela 2023-08-06 132655

Captura de tela 2023-08-06 132725

Captura de tela 2023-08-06 132756

the entity disappears like there is the MonsterGoalPoint’s position

3 Likes

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

1 Like

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)

1 Like

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.