How to make something like Pandemonium from Pressure

So i want to make something like Pandemonium from Pressure, so if the npc sees your he will pathfind a way to you. Does anybody know how to do that? Also does it work using Raycasting?

2 Likes

If you want to make an NPC that will try to go to the player every time he is triggered, you can do that by using PathfindingService calling an appropriate method for the detection which may be Raycasting for instance. Here’s a basic approach to accomplish this:

  1. Set Up The NPC: Make sure your NPC has a ‘Humanoid’ and a ‘HumanoidRootPart’.
  2. Raycasting to Detect Players: Use Raycasting to determine whether or not the NPC can see the Player.
  3. Movement of an NPC: Call PathfindingService to move NPCs to the player.

Here’s a simple script that demonstrates these processes:

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local NPC = script.Parent
local DetectionRange = 50

local function isPlayerInSight(player)
    local character = player.Character
    if character and character:FindFirstChild("HumanoidRootPart") then
        local playerPosition = character.HumanoidRootPart.Position
        local npcPosition = NPC.HumanoidRootPart.Position
        
        local ray = Ray.new(npcPosition, (playerPosition - npcPosition).unit * DetectionRange)
        local hitPart = workspace:FindPartOnRay(ray)
        return hitPart and hitPart:IsDescendantOf(character)
    end
    return false
end

local function moveToPlayer(player)
    local character = player.Character
    if character and character:FindFirstChild("HumanoidRootPart") then
        local path = PathfindingService:CreatePath({
            AgentRadius = 2,
            AgentHeight = 5,
            AgentCanJump = true,
            AgentJumpHeight = 10,
            AgentMaxSlope = 45,
        })
        
        path:ComputeAsync(NPC.HumanoidRootPart.Position, character.HumanoidRootPart.Position)
        path:MoveTo(NPC)
        path:MoveToFinished:Wait()
    end
end

while true do
    wait(1)
    for _, player in ipairs(Players:GetPlayers()) do
        if isPlayerInSight(player) then
            moveToPlayer(player)
        end
    end
end

Explanation:

  • Raycasting: The algorithm called ‘isPlayerInSight’ creates a ray from the NPC to the player for determining if it strikes the humanoid’s ‘HumanoidRootPart’ or not.
  • PathFinding: Should the player be sighted, the function moveToPlayer makes NPC to follow Player’s position by using ‘PathfindingService’ to find the path for NPC to take towards player.
  • Loop: Every second while checking for players after a sighting, player motion is started wherever feasible.

Additional Notes:

  • The DetectionRange can be used to modify the maximum range of the sprite to follow the player.
  • The Pathfinding Agent properties must be edited and adapted to the NPC size and its characteristics.
  • It would be prudent to add additional barriers to ensure that repeated path finding is not requested if the NPC is already in motion to approach the player.