【问题标题】:Shortcutting script, touch function快捷脚本,触控功能
【发布时间】:2022-01-10 00:28:35
【问题描述】:

我正在尝试一种更好/更短的方法来减少任何延迟并减少工作量,就好像我需要更改脚本中的任何内容一样,我需要为每一个都做。

有没有更好的做空方法?

我尝试让“Glass1's”和“Glass2's”同名,但它只适用于第一个,我希望我澄清一下。

这是我的代码:

local End = script.Parent.End
local Start = script.Parent.Start
local Glass = script.Parent

--Glass1/1-8 are the glasses that fall if touched and they change color to red

local function TouchedGlass11(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass11
        num.Anchored = false
        num.BrickColor = BrickColor.Red()
        wait(2)
        num:Destroy()
    else return
    end
end

Glass.Glass11.Touched:Connect(TouchedGlass11)

local function TouchedGlass12(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass12
        num.Anchored = false
        num.BrickColor = BrickColor.Red()
        wait(2)
        num:Destroy()
    else return
    end
end

Glass.Glass12.Touched:Connect(TouchedGlass12)

local function TouchedGlass13(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass13
        num.Anchored = false
        num.BrickColor = BrickColor.Red()
        wait(2)
        num:Destroy()
    else return
    end
end

Glass.Glass13.Touched:Connect(TouchedGlass13)

local function TouchedGlass14(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass14
        num.Anchored = false
        num.BrickColor = BrickColor.Red()
        wait(2)
        num:Destroy()
    else return
    end
end

Glass.Glass14.Touched:Connect(TouchedGlass14)

--then I'll do Glass2/1-8 which just turn the brick to green.

local function TouchedGlass21(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass21
        num.BrickColor = BrickColor.Green()
    else return
    end
end

Glass.Glass21.Touched:Connect(TouchedGlass21)

local function TouchedGlass22(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass22
        num.BrickColor = BrickColor.Green()
    else return
    end
end

Glass.Glass22.Touched:Connect(TouchedGlass22)

local function TouchedGlass23(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass23
        num.BrickColor = BrickColor.Green()
    else return
    end
end

Glass.Glass23.Touched:Connect(TouchedGlass23)

local function TouchedGlass24(hit)
    local partParent = hit.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local num = Glass.Glass24
        num.BrickColor = BrickColor.Green()
    else return
    end
end

Glass.Glass24.Touched:Connect(TouchedGlass24)

--Does anyone know a better way?

请注意:“Glass2”和“Glass1”都有 8 个玻璃杯,但我很快会添加更多,这就是为什么我正在寻找一种更简单的方法。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    查看您的代码很容易发现您使用了 很多 触摸函数。

    您可以通过使用循环遍历 script.Parent 中的每个部分的 for 循环来显着压缩这一点。

    local glass = script.Parent
    
    -- iterate through each child and assign the child to the variable object
    for _, object in pairs(glass:GetChildren()) do
        -- Make sure this child of script.Parent is actually a part.
        if object:IsA("Part") then
            object.Touched:Connect(function(hit)
                local partParent = hit.Parent
                local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
                if humanoid then
                    local num = object
                    num.Anchored = false
                    num.BrickColor = BrickColor.Red()
                    wait(2)
                    num:Destroy()
                end
            end)
        end
    end
    

    你也不应该需要在你触摸的函数中有一个 return。

    【讨论】:

    • 谢谢!这有助于很多,因为我不需要添加数百万个函数:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多