So basically I want to create a new path each milisecond, because the npc moves to the player constantly.
Well, the output says nothing, but it moves to the previous player position, then when finished it moves to the new position.
I tried modifying the script, for example:
I removed the hum.MoveToFinished:Wait(), but then it wasn’t pathfinding.
I don’t really remember what else I did.
npc.Events.FaceViewed.OnServerEvent:Connect(function(plr)
if not triggered and target == nil and canBeTriggered then
canBeTriggered = false
sounds.Idle:Stop()
triggered = true
sounds.Panick:Play()
wait(18.128)
sounds.Rage:Play()
repeat
wait()
local ps = game:GetService("PathfindingService")
local hum = npc.Humanoid
local dest = plr.Character.HumanoidRootPart.Position
local path = ps:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, dest)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
waypoint = nil
end
until plr.Character.Humanoid.Health == 0
end
end)
The problem here is you are looping the entire waypoints generated array each generation. You will want to only move to the first waypoint and then generate a new path.
Remove the for loop and just use
Well, heres the updated code. For some reason its not working.
npc.Events.FaceViewed.OnServerEvent:Connect(function(plr)
if not triggered and target == nil and canBeTriggered then
canBeTriggered = false
sounds.Idle:Stop()
triggered = true
sounds.Panick:Play()
wait(18.128)
sounds.Rage:Play()
repeat
wait()
local ps = game:GetService("PathfindingService")
local hum = npc.Humanoid
local dest = plr.Character.HumanoidRootPart.Position
local path = ps:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, dest)
local waypoints = path:GetWaypoints()
hum:MoveTo(waypoints[1].Position)
hum.MoveToFinished:Wait()
until plr.Character.Humanoid.Health == 0
end
end)
Sorry about that! waypoints[1] will most likely be too close to the NPC and will be considered “MovedTo”. You can use waypoints[2] instead but Pathfinding can be tricky and sometimes even waypoints[2] will be too close. I will see if I can manage a better solution than that.
I would actually say that the final resulting position will be “close enough” that you wouldn’t notice in 99% of cases (We are talking about < 1 stud differences). So, just use this and you should be fine. Also, remove ps from the loop.
local ps = game:GetService("PathfindingService")
repeat
wait()
local hum = npc.Humanoid
local dest = plr.Character.HumanoidRootPart.Position
local path = ps:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, dest)
local waypoints = path:GetWaypoints()
hum:MoveTo(waypoints[2].Position)
until plr.Character.Humanoid.Health == 0
the for loop inside the repeat until loop means that, the NPC will try complete the path set and then find the new path to fix this i would remove the “for _, waypoints in…” loop and just have hum:MoveTo(waypoint[1].Position
and then the rest
If you are getting this behavior outside of studio testing, I’d assume it’s because of network ownership of the NPC switching back and forth. Set the networkownership of the NPC to nil and that may help out.
It could also be the wait() yielding for a longer time in which you could tie into .Heartbeat:Wait(). Give the networkownership a shot first.
First, the first npc doesn’t use the walkspeed, heres the script:
canBeTriggered = false
sounds.Idle:Stop()
triggered = true
sounds.Panick:Play()
wait(18.128)
sounds.Rage:Play()
local ps = game:GetService("PathfindingService")
local pathParams = {
AgentRadius = 2,
AgentHeight = 7,
AgentCanJump = false
}
repeat
local hum = npc.Humanoid
local dest = plr.Character.HumanoidRootPart.Position
local path = ps:CreatePath(pathParams)
path:ComputeAsync(npc.HumanoidRootPart.Position, dest)
local waypoints = path:GetWaypoints()
hum:MoveTo(waypoints[2].Position)
until plr.Character.Humanoid.Health == 0
Before you ask, it isn’t caused by the “AgentParameters”, because I tried.
Second problem is, I recreated the script for an other npc, this time the output isn’t blank.
The output says:
20:17:04.376 Workspace.SCPs.SCP-999.Script:42: attempt to index nil with ‘Position’ - Server - Script:42
20:17:04.376 Stack Begin - Studio
20:17:04.376 Script ‘Workspace.SCPs.SCP-999.Script’, Line 42 - Studio - Script:42
20:17:04.376 Stack End - Studio
And the script is:
local ps = game:GetService("PathfindingService")
local pathParams = {
AgentRadius = 2,
AgentHeight = 7,
AgentCanJump = false
}
while wait() do
local hum = npc.Humanoid
local dest = closestHRP.Position
local path = ps:CreatePath(pathParams)
path:ComputeAsync(npc.HumanoidRootPart.Position, dest)
local waypoints = path:GetWaypoints()
hum:MoveTo(waypoints[2].Position)
end
By “does not work with walkspeed”, how are you setting walkspeed? Are you modifying it on the server? In studio properties? I’m fairly certain MoveTo utilizes walkspeed internally.
Also, given the server usually runs at 20hz, the higher your move speed is the more likely this method is going to cause stuttering. The interpolation from server->client will not be as smooth the faster it moves.
As for the second issue, which line is 42? There are multiple places with .Position so I am unsure which section of code it is trying to index .Position with.
Just make sure you change the walkspeed on the Server View and not the Client view. The way you have it currently it is Server-sided so the client changing the walkspeed won’t replicate the change in movement speed.
I assume this is because the path is empty. Use the following to make sure that the computed path exists.
if path.Success == Enum.PathStatus.Success then
-- All your MoveTo code here
end
This will make sure a path is successful before trying to move through waypoints.
This should adjust for walkspeed and calculating the correct waypoint to traverse based on two factors
Walkspeed = studs per second
Waypoints are roughly 4 studs apart each waypoints
local waypoint = math.ceil(npc.WalkSpeed/4)
if waypoint > #waypoints then
waypoint = #waypoints
end
npc:MoveTo(waypoints[waypoint].Position)
I don’t foresee any issues with this but please let me know if you encounter any.
The problem previously was that as WalkSpeed increased, the NPC was overshooting the 2nd waypoint during the frame. This should adjust based on how fast he is traveling and choose an appropriate waypoint to match the distance covered.
Ahh yes the npc:MoveTo was entirely my fault. Terribly sorry about that. Glad that everything is sorted and working like a charm now. If you have any more situations arise, feel free to post or message me.