Make an NPC pathfind locally

I am creating a Backrooms game and I have a stage where a player will be chased by an NPC down a hallway. I found a pathfinding script, but I need it to work locally. This is because if I have multiple players on 1 stage I want each to have their own monster chasing them. Is there any way to do this?

Current script:

local whilecountdown = 0
local pathservice = game:GetService("PathfindingService")
local path = pathservice:CreatePath()
local hum,hrp = script.Parent.Humanoid,script.Parent.HumanoidRootPart --2 variables at a single line
local EndingPoint = game.Workspace:WaitForChild("EndingPoint")

EndingPoint.Changed:Connect(function()
	local newPath = pathservice:CreatePath()
	newPath:ComputeAsync(hrp.Position,EndingPoint.Position)
	for i,v in pairs(newPath:GetWaypoints()) do
		if v.Action == Enum.PathWaypointAction.Jump then
			hum:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		hum:MoveTo(v.Position)
		hum.MoveToFinished:Wait()
	end
end)

for i, v in pairs(path:GetWaypoints()) do
	hum:MoveTo(v.Position)
	if v.Action == Enum.PathWaypointAction.Jump then
		hum:ChangeState(Enum.HumanoidStateType.Jumping)
		hum.MoveToFinished:Wait()
	end
end
1 Like

I never done a pathfind locally but what you could do is use FireClient(plr, monster, waypoints) to the player the monster is chasing which then the client responds with a computed path (aka the waypoints) and uses FireServer(monster) to then send the information back.

ex.
(Server in monster)

-- I am assuming you already know the player you want to chase in the server
local remote = path.remote
local monster = script.Parent
local hum = monster.Humanoid

local playerChasing = game.Players.ortacise

remote.OnServerEvent:Connect(function(plr, mon, waypoints)
if mon == monster then
for i,v in pairs(waypoints) do
hum:MoveTo(v.Position)
--some other stuff that moves the monster and stuff
end
end
end)

remote:FireClient(playerChasing, monster)

script (client)

local remote = path.remote
local plr = localplayer
local pathfindingService = game:GetService("PathFindingStuff")

remote.OnClientEvent:Connect(function(monster)
-- do some path finding that gets the waypoints
local waypoints = pathfindStuff:Compute
remote:FireServer(monster, waypoints)
end)

I am not really sure if you can pathfind on client I am just assuming you can

Problem is, the monster is spawned locally.

As far as I am aware scripts cannot be run if its spawned locally… I think the network ownership is already on the person who spawned it so you can probably just move the monster to him all in one local script as if it was a script. I think.

I figured out if I spawn the monster through a script in StarterPlayer, and I control the monster through that script, it works. Though the animate script is a server script. Would it work if I convert it to a local script?

If you use set network ownership to the player controlling it then yeah you can play animations to it with a local script

I mean this
image

solved this myself, add me on disco if you’re here looking for a solution

boris#8711

I sent you a friend request on disco