【问题标题】:What is the paramater equivilant to?参数相当于什么?
【发布时间】:2016-12-13 12:18:10
【问题描述】:
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 编码的新手,这是我第一次使用函数。我想知道“部分”等于什么,以便了解如何设置人类变量

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

不使用“部分”,比如我可以插入什么,这样甚至不需要设置部分就可以工作,因为我想在循环中用它做一些事情:

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

代码可能看起来很混乱,但我还很新,所以我还不知道什么是最好的。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    看起来你想要做的是让玩家在触摸特定砖块时“着火”。下面的代码就是这样做的,后面我会逐行解释。

    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 对象。因此,ROBLOX 会在您的script.Parent 触碰时自动调用此函数,并自动输入触碰它的Part 作为第一个参数part

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

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

    • if (human == nil) then

    我们想检查 human 是否为 nil(这意味着我们无法在该对象之前的行中找到 Humanoid 对象,这意味着任何接触到的对象都不是真正的 Character,所以我们'会想return(这意味着立即停止运行该函数)。

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

    这行不是必须的,我添加它是因为我想如果你想模拟燃烧,这会有所帮助。如果你愿意,你可以把它排除在外。它将创建一个Fire 类型的新Instance,并将其放在part 内。也就是说,如果玩家的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首先。

    如果您有任何问题,请随时提问!希望这能回答您的问题。

    【讨论】:

      【解决方案2】:

      我记得我曾经在 Roblox 上使用 Lua。部分是游戏中被触及的部分。您需要引用它,以便您可以找到它属于或缺少的类人对象,以便您的代码可以判断它是否是触摸它的类人对象。如果您有任何其他问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 2016-09-24
        • 2013-11-21
        • 2021-11-11
        • 2019-04-21
        • 2015-03-28
        相关资源
        最近更新 更多