如何使脚本影响 Roblox LUA 中的所有子项?

How do I make a script affect all its children in Roblox LUA?

我是 LUA 的编程新手,虽然我学过类似的语言,例如 JS。如果我必须通过替换每个脚本来更改组中许多部分的相同脚本,这会令人沮丧,而且我不知道有什么优雅的方法可以做到这一点。相反,我决定将所有部分嵌套在脚本中。我看过一些示例,并尝试对其中一些进行改编,但它们并不完全适用于我想做的事情,我无法让它们发挥作用。

本质上,我想做的是监控所有积木,以便玩家联系它们。我采用了嵌套在每块积木中的原始消失积木脚本并对其进行了修改。如果某个部分(砖块)被触摸,则应调用 onTouch 函数,这将使砖块的透明度随着时间的推移而降低,直到成对循环完成,之后砖块消失并且 CanCollide 被关闭。 2 秒后,它又 returns 恢复正常。我认为问题出在我用来监控部件的编码上,因为我并不真正理解监控多个对象的正确方法。有人可以帮忙吗?谢谢!

文件结构:

function onTouched(brick)
    local delay = .1 -- the delay between each increase in transparency (affects speed of disappearance)
    local RestoreDelay = 2 -- delay before the brick reappears
    local inc = .1 -- how much the brick disappears each time

    -- All characters have a Humanoid object
    -- if the model has one, it is a character
    local h = script.Child:findFirstChild("Humanoid") -- Find Humanoids in whatever touched this
    if (h ~=nil) then -- If there is a Humanoid then
        h.Health = h.MaxHealth -- Set the health to maximum (full healing)
        for x=0,1, inc do
            script.Child.Transparency = x+inc
            script.Child.CanCollide = true
            wait(delay)
        end
        wait(delay)
        script.Child.Transparency = 1
        script.Child.CanCollide = false
        wait(RestoreDelay)
        script.Child.Transparency = 0
        script.Child.CanCollide = true
    else
    end
end

while true do
    local bricks=script:GetChildren():IsA("basic.part")
    for x=1,brick in pairs(bricks) do
        brick.Touched:connect(onTouched(brick)) -- Make it call onTouched when touched
    end
end
end

在大多数情况下,您做对了,但是您有一些语法错误,其中 JavaScript 和 Lua 之间存在不同的约定。

在 JS 中,您可以获取一个对象数组,然后可以立即对其进行过滤,但在 Lua 中,对此的支持有限。所以 JavaScript 行像 :

var bricks = script.GetChildren().filter(function(item) {
   return item === "basic.part"
})

如果没有某些图书馆的帮助,Lua 无法在一行中全部完成。因此,您需要在遍历对象时将检查移动到循环中。

除此之外,唯一需要更改的是 onTouched 处理程序的函数签名。 BasePart.Touched event tells you which object has touched the brick, not the brick itself. But by creating a higher order function,很容易拿到砖头,以及碰到它的东西。

-- create a helper function to access the brick and the thing that touched it
function createOnTouched(brick)
    -- keep track whether the animation is running
    local isFading = false

    return function(otherPart)
        -- do not do the animation again if it has already started
        if isFading then
            return
        end

        local delay = .1 -- the delay between each increase in transparency (affects speed of disappearance)
        local restoreDelay = 2 -- delay before the brick reappears
        local inc = .1 -- how much the brick disappears each time

        -- All characters have a Humanoid object, check for one
        local h = otherPart.Parent:FindFirstChild("Humanoid")
        if h then
            -- heal the player
            h.Health = h.MaxHealth

            -- start fading the brick
            isFading = true
            brick.CanCollide = true
            for i = 0, 1, inc do
                brick.Transparency = i
                wait(delay)
            end
            -- turn off collision for the brick
            wait(delay)
            brick.Transparency = 1
            brick.Anchored = true
            brick.CanCollide = false

            -- turn the part back on
            wait(restoreDelay)
            brick.Transparency = 0
            brick.CanCollide = true

            -- reset the animation flag
            isFading = false
        end
    end
end

-- loop over the children and connect touch events
local bricks = script:GetChildren()
for i, brick in ipairs(bricks) do
    if brick:IsA("BasePart") then
        local onTouchedFunc = createOnTouched(brick)
        brick.Touched:Connect(onTouchedFunc)
    end
end