什么是等效于参数?

function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
    if (human == nil) then
        return
    end
    human.Health = human.Health - 10
end
script.Parent.Touched:connect(onTouch)

我刚开始学习编写 lua 代码,而且是我第一次使用函数。我想知道“part”等价于什么,这样我就可以找出如何设置 human 变量了。

local human = part.Parent:findFirstChild("Humanoid")

而不使用“part”,像什么可以插入进去,使得它能够在一个循环中顺利工作:

local burnaffect = false
--local a = 0
function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
    if (human == nil and burnaffect == false) then
        return
    end
    a = 0
    burnaffect = true
end
script.Parent.Touched:connect(onTouch)

while burnaffect == true do
    local part = --????
    local human = part.Parent:findFirstChild("Humanoid")
    human.Health = human.Health - 10
end

这段代码可能看起来有些困惑,但我是新手,所以还不太懂什么是最好的。

点赞
用户2001154
用户2001154

我还记得在 Roblox 中使用 Lua 的时候。其中的部分就是游戏中被触碰的部分。你需要引用它,这样你就可以找到它所属的人形对象,或者没有,这样你的代码就可以告诉它是否是被人形触碰的。如果您有任何进一步的问题,请告诉我。

2016-08-08 18:39:51
用户4764729
用户4764729

看起来你想让当玩家接触到某个方块时,玩家会 "着火"。下面的代码实现了这个功能,我会逐行讲解。

function onTouch(part)
    local human = part.Parent:findFirstChild("Humanoid")
    if (human == nil) then
        return
    end
    local fire = Instance.new("Fire", part)
    while (human.Health > 0) do
        human.Health = human.Health - 10
        wait(0.1)
    end
end
script.Parent.Touched:connect(onTouch)

我们来逐行解析一下:

  • function onTouch(part)

我们需要先定义一个函数,并给它一个名字,以便稍后在 Touched 事件中引用它。part 参数是接触到 script.Parent 对象并触发了 Touched 事件的 Part 对象。所以,当有东西接触你的 script.Parent 时,ROBLOX 将自动调用此函数,并自动将接触到的 Part 作为第一个参数 part 输入。

  • local human = part.Parent:findFirstChild("Humanoid")

这将获取接触到该方块的 Part(因为如果玩家正在接触该方块,它不会给我们 Character,它会给我们在 part 变量中的一个 Arm 或一个 Leg,因为那是实际接触到它的 Part,所以我们需要获取该 partParent)。然后,一旦我们有了 Parent,我们想要在 Character 中获取 Humanoid 对象。然后,将该 Humanoid 对象放入 human 变量中(如果我们找不到,否则将 nil 放入该 human 变量中)。

  • if (human == nil) then

我们想要检查 human 是否为空(这意味着我们找不到一种 Humanoid 对象,在此之前的行中,这意味着触摸它的任何东西都不是真正的 Character,所以我们将 return(即立即停止运行该函数)。

  • local fire = Instance.new("Fire", part)

这一行并不是必需的,我添加它是因为我认为如果你想模拟燃烧,这会有所帮助。如果你不想保留它,可以将其省略。它将创建一个类型为 Fire 的新 Instance,并将其放置在 part 中。也就是说,如果玩家的 Leg 接触到该部分,则会在该 Leg 中放置一个 Fire,它将使它看起来着火并冒出火焰。

  • while (human.Health > 0) do

我们要保持循环直到玩家死亡(human.Health 值为 0 或更少),所以我们要告诉循环在 human.Health 大于 0 时继续循环。

  • human.Health = human.Health - 10

我们会将 human.Health 的值减小 10,然后 wait(0.1),这将使脚本等待 1/10 秒(你可以将此更改为其他数字,但保持它很重要,如果将其删除,循环将运行得非常快,立即杀死玩家。如果你希望发生这种情况,可以删除等待,但如果你想立即杀死玩家,你可以直接将 human.Health 设置为 0)。

如果你有任何问题,请随时问!希望这回答了你的问题。

2016-08-10 20:34:58