让 NPC 看起来像 ROBLOX 中的玩家

Make NPC Look Like a Player in ROBLOX

我正在尝试让 NPC 在标题屏幕上看起来像玩家。

我不需要关于如何制作 NPC 的教程;我知道如何让 NPC 看起来像个人。相反,我需要知道如何在脚本中克隆玩家的确切外观。我该怎么做?

我假设您正在尝试让 NPC 看起来像玩家?这可能需要一些工作,但您必须在 game.Workspace 中找到您希望 NPC 看起来像的玩家,然后您可以克隆玩家并将 npc 的零件插入其中,或者您可以将克隆的玩家插入到npc中。

据我所知,如果不在脚本中执行此操作,则无法执行此操作。

首先,可以在 Player's .Character attribute. The .Character attribute is a Model, which, along with most other kinds of objects in the game, has a :Clone() 函数中找到给定玩家的角色模型。您可以使用此功能来制作玩家角色模型的完整副本。例如:

player = path.to.player.here

copy = player.Character:Clone()
copy.Parent = game.Workspace --put the clone into the physical environment

但是,如果您将其用于标题屏幕,请注意:当玩家加入游戏时,他们的角色不会立即加载;相反,它需要几秒钟。如果您尝试玩游戏,然后注意在您真正进入角色之前需要多长时间,您可能会亲眼看到这一点。因此,如果您尝试在某人加入您的游戏时立即使用 .Character您的代码将会崩溃 。为了解决这个问题,您可以使用 ROBLOX 的一个特殊功能,称为 Events.

ROBLOX 有特殊的 objects 称为 Events。 Events 有一个特殊的 :connect() 函数,它允许您将函数连接到那些事件。当您将函数连接到 Event 时,该函数将在 Event 出现时执行。

在你的情况下,你需要 两个 Events:

  1. 一个 Player 加入游戏时
  2. 一个用于当他们的身体 .Character model 实际加载时

首先,让我们谈谈Player何时加入游戏。首先,我们需要得到 Players object—an object that keeps track of information about all of the players. We will use a special function of game called :GetService():

players = game:GetService("Players")

现在,我们将在 Players 中使用一个特殊的 Event,称为 .PlayerAdded:

players = game:GetService("Players")

players.PlayerAdded:connect(function(player)

end)

请注意,实际添加的 Player 将作为参数传递给参数 player。现在,我们将使用一个特殊的 Event of Players 叫做 CharacterAdded:

players = game:GetService("Players")

players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)

    end
end)

注意PlayerCharacter会很方便的作为参数传递给参数character,所以我们甚至不需要使用Player.Character .现在,我们可以从之前的代码中获取我们的克隆代码,并最终将其放入以下代码中:

players = game:GetService("Players")

players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local copy = character:Clone()
        copy.Parent = game.Workspace --put the clone into the physical environment

        --your code here
    end
end)

就是这样!现在您可以将处理克隆的所有代码放在 --your code here.

的位置

最后要注意的是,这需要定期完成 Script, not a LocalScriptScripts 由 ROBLOX 服务器处理,而 LocalScripts 由玩家自己的计算机处理。因为服务器处理添加到游戏中的玩家,所以您必须使用 Script.

希望对您有所帮助!