Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a hide feature and have the NPC go away?

Is it possible to make a place where a player can hide from my pathfinding NPC and the NPC will go away when the player is in that spot?

Heres the NPC pathfinding script:


game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(cha****racter)
if character then
local function movetonpc()
local vecPoint = workspace.NPC.Humanoid.Position
local closestPlayer, smallestMagnitude = nil,math.huge

for _, player in pairs(game.Players:GetPlayers()) do
     local distance = player:DistanceFromCharacter(vecPoint)
     if distance < smallestMagnitude then
          smallestMagnitude = distance
          closestPlayer = player

NPC.Humanoid:MoveTo(player.Character.Humanoid)
     end
end
end
while wait(.01) do
 movetonpc()
end
end
end)
end)

2 comments

0

Hey bud, can you indent your lines of code so that it's easier to read? Thanks! :)

0

?

1 answer


game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
if character then
player = notsafe
local Safepart = workspace.SafePart
local function onTouched(part)
player = safe
end
Safepart.Touched:Connect(onTouched)
local function onTouched(part)
player = safe
end
local function offTouched(part)
player = notsafe
end
Safepart.TouchEnded:Connect(offTouched)
local function movetonpc()
local vecPoint = workspace.NPC.Humanoid.Position
local closestPlayer, smallestMagnitude = nil,math.huge

for _, player in pairs(game.Players:GetPlayers()) do
     local distance = player:DistanceFromCharacter(vecPoint)
     if distance < smallestMagnitude then
          smallestMagnitude = distance
          closestPlayer = player
if player == notsafe then

NPC.Humanoid:MoveTo(player.Character.Humanoid)
else
      print(“Player is safe”)
end
     end
end
end
while wait(.01) do
 movetonpc()
end
end
end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.