0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Roblox:追いかけてくるキャラクターを作る

Posted at

自分用メモ

以下記事から必要な部分だけコピー
https://qiita.com/PrivateUeno/items/61fd45583f740e00a2ed

script.lua
-- ほかく者のオブジェクト
local Figure = script.Parent
-- ほかく者のHumanoidオブジェクト
local Humanoid = Figure:FindFirstChild("Humanoid")

-- ずっと繰り返す
while true do
    -- ちょっと待つ
    wait(math.random(2, 6))

    -- 追いかける相手を決めるためにつかう変数
    local targetPlayer

    -- オンラインプレイで複数プレイヤーがいる場合、その中からランダムに追いかける相手をえらぶ
    -- しかし、GetService("Players")ではプレイヤーの名前やIDはわかっても、座標がわからない
    -- なぜかというと、GetService("Players")で取得しているのは、Workspace内のプレイヤーオブジェクトではなく
    -- workspaceと並んでいるPlyersというフォルダの中のプレイヤーオブジェクトのため。
    -- このプレイヤーオブジェクトは名前やIDしか持っていない。
    local targetPlayer_list = {}
    local Players = game:GetService("Players")
    for i, player in pairs(Players:GetPlayers()) do
        targetPlayer_list[i] = player
    end 
    targetPlayer = targetPlayer_list[math.random(1, #targetPlayer_list)]

    -- GetService("Players")で取得したプレイヤーの名前をつかって
    -- workspace内のプレイヤーオブジェクトを探している
    local targetPlayer_obj = workspace:FindFirstChild(targetPlayer.Name)

    -- workspace内のプレイヤーオブジェクトの場所へむかって動く
    Humanoid:MoveTo(targetPlayer_obj.HumanoidRootPart.Position)
end
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?