How to make Roblox NPC follow player only, not other NPCs

I would like to know how to make this script and even explaining it would be better. Thanks a lot, any help appreciated. Edit: a script which automatically follows player when a player gets near them


I think the player might have an attribute like "player.player = true", but if not, you can make your own.
Then you can do:
if object player has attribute player.....


(I'm using 'npc' instead of 'Roblox' for my example)

<object name="room">
</object>

<object name="room_2">
</object>

<object name="room_3">
</object>

<object name="player">

  <attr name="parent" type="object">room</attr>

  <attr name="changedparent" type="script">

    npc.parent = player.parent

  </attr>

</object>

<object name="npc">

  <attr name="parent" type="object">room</attr>

</object>

the special 'changedNAME_OF_ATTRIBUTE' Script Attribute is activated whenever the Value of the 'NAME_OF_ATTRIBUTE' Attribute of its Object (for this example: Object: player, and thus the Object.Attribute is: player.parent), changes:

the 'player' is initially set within 'room'
the 'npc' is initially set within 'room'

(so, they're both set/within the same room: 'room')

so, let's say that, the 'player' moves from 'room' to 'room_2', which, such containment/parent-child/"folder-like" heirarchy, is actually controlled by the built-in 'parent' Object Attribute, so the player's 'parent' Object Attribute's Value is changed, from 'room' to 'room_2', and thus this causes the player's 'changedparent' Script Attribute to activate.

now, about the scripting within the player's 'changedparent' Object Attribute:

npc.parent = player.parent

the npc's 'parent' Object Attribute's Value will be set to whatever is the player's 'parent' Object Attribute's Value

so, since the player's 'parent' Object Attribute's Value is now 'room_2', the npc's 'parent' Object Attribute's Value is now also 'room_2', and thus the npc has followed the player, as they're both set within the same room (room_2)

the player then moves to 'room_3', and thus the same stuff happens, setting the npc to be in the same room (room_3) as the player


// they both initially are set to the same room:
player.parent = room
npc.parent = room

// the player moves to a different room:
player.parent = room_2

// the special 'changedparent' Script Attribute of the 'player' is activated, as its Attribute's (parent) Value has changed (from 'room' to 'room_2'):

npc.parent = player.parent

// player.parent = room_2
//
// npc.parent = player.parent
// npc.parent = (player.parent = room_2)
// npc.parent = player.parent = room_2
// npc.parent = room_2
//
// player.parent = room_2
// npc.parent = room_2
// both the 'player' and 'npc' are in the same new room (room_2), the 'npc' has "followed" the 'player' from 'room' to 'room_2'


No, i mean like a script which automatically follows player when a player gets near them, but that script is useful thx! @hegemonkhan


near as in when meet for first time in same room? (same thing as above, except instead of having the 'player.changedparent' existing/created at game start, you'd have it be created upon the player entering the same room as npc for the first time):

// for the npc's initial starting room, for example using, either:

1. the (if it exists) 'when entering room for first time', within its scripting:

player.changedparent => {
  npc.parent = player.parent
}

// or, otherwise, using:

2. 'when entering room' and use the 'firsttime/otherwise' Script/Function, within its scripting:

firsttime {
  player.changedparent => {
    npc.parent = player.parent
  }
} otherwise {
  // blah scripting or leave blank
}

or, near as in, having 'distance/area/volume' Integer Attributes? (a different beast... but not that much harder to do...)


I mean as soon as a player reaches a certain distance between the NPC, I want the NPC to react to that player and follow the player. For example look at the game Ro-Ghoul on Roblox, it shows how the NPC follows the player when reaching certain distance between the NPC and player. Also this is outdoors, in open ground, not a room. This is my current follow script - (it's flaw is that it only follows other NPCs and ignores players) :--
localPlayerTag = instance.new("stringValue")

PlayerTag.Name = "Player"

Game.player.PlayerAdded:connect(function)PlayerTag.Clone().Parent = game.players.localPlayer end)
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Right Arm")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end

while true do
wait(math.random(1,5))
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
local Tag = findNearestTorso(script.Parent.Torso) if Tag.Player ~= nil
script.Parent.Humanoid:MoveTo(target.Position, target)
end

end


What are you using to create the game? (Because it isn't Quest.)


Oh, it's this:

https://www.roblox.com/create

I believe you want to find your answers here: http://wiki.roblox.com/index.php?title=Main_Page

...an here's their forum: https://devforum.roblox.com/


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums