繁体   English   中英

当 NPC 死亡时,如何复制 object?

[英]How do I make a clone of an object when an NPC dies?

我试图做到这一点,这样你就必须杀死一个 NPC 才能将代码保存到保险库中。 这是我第一次在 roblox studio 中制作任何东西,如果这看起来很愚蠢,请原谅我。

看了API文档后,我做了这个。 我基本上只是复制并粘贴了一些东西并替换了我认为需要替换的东西:

`

local Humanoid = script.parent:WaitForChild(“Guard”)

(This is not part of the original code:) I put^ the name of the NPC I'm using, in this case the Guard.
local original = workspace.VaultCode

(This is not part of the original code:) Here^ I replaced something like part or whatever with the part name VaultCode. It doesn't seem right, but idk.

local copy = original:Clone()
Humanoid.Died:Connect(function()
    copy.Parent = original.Parent
    copy:SetPrimaryPartCFrame(CFrame.new(0, -300, 0))
end)

` 我预计它会在 NPC 守卫死亡时创建 Vaultcode 部分的克隆,但什么也没发生。

我也尝试查找这个问题并做了类似的事情,但我删除了它并忘记了我做了什么。 我所知道的是它没有用。

在你的代码中对我来说最突出的是,

  • 看起来你找到了 NPC 的角色 model,但没有找到他们的 Humanoid,
  • 您只需克隆 VaultCode object 一次,并且
  • 你在地下很远的地方产生了 object。

如果 object 没有锚定,它可能会立即掉落到杀伤平面(大约 -500 个螺柱)并立即被摧毁。 由于您只克隆了一次,因此您正在尝试设置不再存在的 object 的 position。

要解决此问题,请尝试在 NPC 死亡的地方生成 object,然后每次都克隆一个新的。

我假设这个脚本位于工作区,而不是 NPC 角色 model。

-- find the VaultCode object
local VaultCode : Model = workspace.VaultCode

-- find the NPC and its humanoid
local Humanoid : Humanoid = script.Parent:WaitForChild(“Guard”).Humanoid

-- listen for when the Humanoid dies
Humanoid.Died:Connect(function()
    -- create a copy of the vault code where the NPC died
    local npcCFrame = Humanoid.Parent:GetPivot()

    local copy = original:Clone()
    copy:SetPrimaryPartCFrame(npcCFrame)
    copy.Parent = original.Parent
end)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM