一番近くにいるプレイヤーを追いかけるNPCのプログラムです。
- ツールボックスからNPCを見つけて挿入します。
プログラムが入っているNPCは正常に動作しなくなる可能性が高いので注意してください。 - NPCにScriptを追加し、以下のプログラムを入力します。
local Players = game:GetService("Players")
local noob = script.Parent
local function findPlayer()
local position = nil
local distance = math.huge -- 最大値を設定しておく
for _,pl in pairs(Players:GetPlayers()) do
local character = pl.Character
if character then
local d = pl:DistanceFromCharacter(noob.PrimaryPart.Position) -- プレイヤーとNPCの距離を取得
if d < distance then -- より近いプレイヤーをターゲットにする
distance = d
position = character.PrimaryPart.Position -- プレイヤーの位置
end
end
end
return position
end
while wait(0.5) do -- waitの値で
local position = findPlayer()
if position then
noob.Humanoid:MoveTo(position) -- NPCの移動
end
end