快捷脚本、触控功能

Shortcutting script, touch function

我正在尝试使用一种 better/shorter 方法来减少任何延迟并减少工作量,就好像我需要更改脚本中的任何内容一样,我需要对每个脚本进行更改。

有没有更好的做空方法?

我试过让“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 个眼镜,但我会尽快添加更多,这就是为什么我正在寻找更简单的方法。

查看您的代码,很容易发现您使用了大量 的相关函数。

您可以使用循环遍历 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

你也不应该在你的 touched 函数中有一个 return。