How can I make the NPC interact with the elevator?

I want to ensure that the NPC can use the elevator and go up to the floor where the target is located (there are two floors in the elevator, perhaps there will be more floors). I can achieve this using the Pathfinding system, but I don’t know exactly how it can be implemented.

3 Likes

Do you have an npc set up with pathfinding already?

3 Likes

Yes there is. And it works well, in good condition.

2 Likes

Your elevator just like moves up and down with a tween or whatever right so it doesn’t teleport and drop its contents?

3 Likes

Yes, it works fine with tween.

2 Likes

Well my suggestion would be to put a part in the elevator just on the floor in the middle or something as a pathfinding node and when your NPC needs to transfer floors it can pathfind to the node in the elevator if its on the same floor, once its reached fire a bindable event assuming your elevator runs through the server to tell the elevator to move to whatever floor the player is on and then boom npc using the elevator, i’d be happy to help you write the script if you’d like while I still have time

2 Likes

Yes, you’d better write a script to make it clearer. That’s basically what I imagined, but I didn’t know how to send a signal to the floor where the target is located.

2 Likes

Can you reply with your elevator script and the pathfinding script and I can throw it into my studio and add stuff n whatnot, if you don’t want to send it here feel free to add my discord jitzwazgumlord

or i can use my own and probably throw together a shoddy elevator script and then show you it as a reference if you’d prefer that

I think that as a reference so that I can apply, based on your script, this function.

Sure, i’ll whip it up, it may take a while as I have things to do in about 20 minutes but if I can’t finish it I’ll be able to tomorrow

Okay, that’s settled!!!

If you can provide the NPC and Elevator script I can make it so it creates a new part or you can manually do this, and script that part so when the NPC touches it the elevator script is Ran.

The first option is more sympathetic to me, so that it is more convenient and versatile.

Here’s my janky elevator the NPC node didn’t move with it but you tbh could probably just use the platform’s position +1 on the Y axis or something but I’m out of time so here’s the scripts
Elevator:

local elevator = game.Workspace:WaitForChild("elevator")
local events = game.ReplicatedStorage.events
local tweenService = game:GetService("TweenService")

local moveFloor = function(floor)
	local floors = elevator.floors:GetChildren()
	local info = TweenInfo.new(4)
	local dest = {}
	dest.Position = floors[floor].Position
	print(dest.Position)
	local tween = tweenService:Create(elevator.Platform, info, dest)
	tween:Play()
end

events.npcElevator.Event:Connect(function(floor)
	print("event recieved")
	moveFloor(floor)
end)

NPC:

local torso = script.Parent.Torso
local human = script.Parent.Humanoid
local pathfindingService = game:GetService("PathfindingService")
local choice = math.random(2,3)
local events = game.ReplicatedStorage.events

local walkTo = function(target, humanoid, torso)
	local path = pathfindingService:FindPathAsync(torso.Position,target.Position)
	local waypoints = path:GetWaypoints()
	
	if path.Status == Enum.PathStatus.Success then
		for i,v in pairs(waypoints) do
			humanoid:MoveTo(v.Position)
			humanoid.MoveToFinished:Wait()
			if waypoints.Action == Enum.PathWaypointAction.Jump then
				humanoid.Jump = true
			end
		end
	end
end

walkTo(game.Workspace.elevator.npcNode, human, torso)

events.npcElevator:Fire(choice)

and i used my really old pathfinding script as that’s what I had on hand so its not the most up to date or optimized but it works as a demonstration, sorry for the jankyness but that should show how it could be done

This method is quite simple and it won’t work properly. Hint: Use PathfindingLink and PathfindingService. In my opinion, this is the best thing possible in Roblox.

Like I said it is a very old pathfinding script that I had lying around just so I could show you how it would be done and it does work as a demonstration to show how it is done as per your request.

You could make multiple pathfinding links to each floor, each link originating from the elevator. Label each of these pathfinding links as the floor they go to. Make sure to make all of these links bi-directional.

Seeing that the PathfindingLinks already help calculate the best path, you don’t have to determine what floor the target and the NPC are on.

As the NPC pathfinds towards the elevator, they will likely use the pathfinding link on their current floor. You can then use the label of this link to tell the elevator where the NPC currently is and to go to that destination. If the elevator is not on the current floor, make sure to make the NPC wait before moving toward the node inside the elevator. Once inside the elevator, the NPC will switch to the pathfinding link to the target’s floor. You can use this pathfinding link’s label to tell the elevator where to go.