簡體   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