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
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()
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
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.
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
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.
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.