Help with SimplePath module

Hey, hello, good morning or night,

I am making a game with a monster NPC, and I have some code I’ve written for it. The game goes in a loop and there is a variable called “Danger Level”. When the danger level reaches a certain number, the monster will spawn and begin to chase down the player. I wrote some code with some help from a colleague, but neither of us could figure it out and I resorted to this place for help.

This is the code we wrote, mostly my colleague. This code uses the module SimplePath:

--(( Loaded Modules ))--
local SimplePath = require(game.ServerScriptService.SimplePath)

--(( Services ))--
local Players = game:GetService('Players')
local PathfindingService = game:GetService("PathfindingService")

--(( Internal Functions ))--
local function onStart()
	while wait() do
		local closestPlayer, closestDistance = nil, math.huge
		local path = SimplePath.new(script.Parent)
		path.Visualize = true

		for _, player in pairs(Players:GetPlayers()) do
			local distance = (script.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude

			if distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end

		path.Blocked:Connect(function()
			SimplePath:Run(closestPlayer.Character.PrimaryPart)
		end)

		path.WaypointReached:Connect(function()
			path:Run(closestPlayer.Character.PrimaryPart)
		end)

		path:Run(closestPlayer.Character.PrimaryPart)
	end
end
onStart()

the main error that happens is caused by this line:

path:Run(closestPlayer.Character.PrimaryPart)

if anyone has some sense of familiarity with SimplePath, you’re free to help, any at all is appreciated

Thank you in advance

Could you as well give the code for what’s in the simple path module? Or have you made sure you used :ComputeAsync when calculating the pathfinding?

1 Like

you can check out the module in #resources:community-resources

i’m not too familiar with pathfinding at all, it kinda sucks lol

So I edited the code slightly to make sure you don’t get your common errors such as nil character, tell me if the NPC walks to the user now.

I as well optimized it to not create separate paths each second like you did in the loop which could use a lot of memory.

--(( Loaded Modules ))--
local SimplePath = require(6336743234)

--(( Services ))--
local Players = game:GetService('Players')
local PathfindingService = game:GetService("PathfindingService")

--(( Internal Functions ))--
local function onStart()
	local closestPlayer, closestDistance = nil, 20
	local path = SimplePath.new(script.Parent)
	path.Visualize = true
	
	while wait() do
		closestPlayer, closestDistance = nil, 20

		for _, player in pairs(Players:GetPlayers()) do
			if player.Character ~= nil  then
				local distance = (script.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude

				if distance < closestDistance then
					closestPlayer = player
					closestDistance = distance
				end			
			end
		end
		
		if closestPlayer ~= nil then
			path.Blocked:Connect(function()
				if closestPlayer then
					SimplePath:Run(closestPlayer.Character.PrimaryPart)
				end
			end)

			path.WaypointReached:Connect(function()
				if closestPlayer then
					path:Run(closestPlayer.Character.PrimaryPart.Position)
				end
			end)
			
			if closestPlayer then
				path:Run(closestPlayer.Character.PrimaryPart.Position)
			end
		end
	end
end

onStart()
1 Like

i edited the script a little bit, i changed the closest Distance to math.huge since the player is gonna be far away from the monster by the time it spawns in

Pretty much the same code
--(( Loaded Modules ))--
local SimplePath = require(6336743234)

--(( Services ))--
local Players = game:GetService('Players')
local PathfindingService = game:GetService("PathfindingService")

--(( Internal Functions ))--
local function onStart()
	local closestPlayer, closestDistance = nil, math.huge
	local path = SimplePath.new(script.Parent)
	path.Visualize = true

	while wait() do
		closestPlayer, closestDistance = nil, math.huge

		for _, player in pairs(Players:GetPlayers()) do
			if player.Character ~= nil  then
				local distance = (script.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude

				if distance < closestDistance then
					closestPlayer = player
					closestDistance = distance
				end			
			end
		end

		if closestPlayer ~= nil then
			path.Blocked:Connect(function()
				if closestPlayer then
					SimplePath:Run(closestPlayer.Character.PrimaryPart)
				end
			end)

			path.WaypointReached:Connect(function()
				if closestPlayer then
					path:Run(closestPlayer.Character.PrimaryPart.Position)
				end
			end)

			if closestPlayer then
				path:Run(closestPlayer.Character.PrimaryPart.Position)
			end
		end
	end
end

onStart()

it won’t move, it just stands still in the spawn room

Are you sure your humanoid works right? Make sure nothing is anchored inside of the dummy that the script goes in

nothing in my dummy is anchored. For some reason, it worked once but it got stuck halfway through. Odd

It will most likely not move if closestPlayer is nil which can occur when the player’s character is not found in workspace.

as a matter of fact, it does not give me any errors,
image

after the danger level reaches 20, it spawns in the monster and gives me the Requiring asset xxxxxxxx line, but no errors, which is quite weird

It should not give any errors because I countered them with selection statements, you can try to put print statements and see if they print within the loop of things like closestPlayer from there you could try to find the problem.

Another source might be if the script is disabled or enabled before the entity spawns.

so it worked…

but not how i expected it to

1 Like

Are there multiple players in there?

2 Likes

no, it’s just me for now, i have 2 others in my team create right now but they’re all offline

1 Like

I meant is there only 1 player playing? Is there anything else that could influence the movement? If you wish I can take a look through the model myself if you send over the humanoid

2 Likes

RoomsMonster.rbxm (8.9 KB)
here is the monster model, it’s just a dummy right now

1 Like

It could be the movement that makes it act like that. When an NPC is extremely fast, at least for me, it overshoots the target. You could make the NPC stop once it is in range of the part, but I don’t know how your module looks since I couldn’t find it.

2 Likes

it doesn’t overshoot though, it just gets stuck in one place. It moves, it just doesn’t follow the player

Oh so the target wasnt on the table? Sorry.
Could you send a link to the module so I can better understand the problem please?

1 Like

of course, here’s the toolbox link:
https://www.roblox.com/library/6744337775/SimplePath-Pathfinding-Module?ViewInBrowser=true&CreatorId=845660356&SearchId=F077FE18-1F37-4874-8834-E872D37082AC&Category=Model&Position=1&SearchKeyword=simplepath&CreatorType=User&SortType=Relevance

the link to the DevForum post is in the description of the model

Alright issue is that you are updating the wanted path too frequently aka the speed of the humanoid is too fast which means it covers more distance in the short amount of time that the script expects, as it also calculates the pathfinding it needs to take to get to you it means it changes each time the entity moves

If you go into the original SimplePath script and update the TIME_VARIANCE to 0.05 and then in the main script paste this, it should work.

--(( Loaded Modules ))--
local SimplePath = require(game:GetService('ServerScriptService').SimplePath)

--(( Services ))--
local Players = game:GetService('Players')
local PathfindingService = game:GetService("PathfindingService")
local LastTick = 0

--(( Internal Functions ))--
local function onStart()
	local path = SimplePath.new(script.Parent)
	path.Visualize = true
	
	while wait(0.01) do
		local closestPlayer, closestDistance = nil, math.huge

		for _, player in pairs(Players:GetPlayers()) do
			if player.Character ~= nil  then
				local distance = (script.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude

				if distance < closestDistance then
					closestPlayer = player
					closestDistance = distance
				end			
			end
		end

	if closestPlayer ~= nil then
			path.Blocked:Connect(function()
				if closestPlayer and closestPlayer.Character then
					SimplePath:Run(closestPlayer.Character.PrimaryPart)
				end
			end)

			path.WaypointReached:Connect(function()
				if closestPlayer and closestPlayer.Character  then
					path:Run(closestPlayer.Character.PrimaryPart.Position)
				end
			end)

			if closestPlayer and closestPlayer.Character then
				path:Run(closestPlayer.Character.PrimaryPart.Position)
			end
		end
	end
end

onStart()

Note: you will need to require the script like you did originally in yours for this to take effect.

5 Likes