【问题标题】:How to create a variable for a function in Roblox (Lua)如何在 Roblox (Lua) 中为函数创建变量
【发布时间】:2021-12-28 20:54:18
【问题描述】:
local colorwheel = script.Parent
local clickdetector = colorwheel.ClickDetector
local barlight = workspace.barlight:GetChildren()

--attempting to establish function as variable--
local rightmouse = function onMouseClick()
        print(" turned the lights off")
        barlight.Transparency = 1
end

local leftmouse = function onMouseClick()   
        print(" turned the lights on")
        barlight.Transparency = 0
end

clickdetector.RightMouseClick:connect(rightmouse)
clickdetector.MouseClick:connect(leftmouse)

我正在尝试创建一个函数,以便在单击模型“colorwheel”时,它会更改“barlight”模型的透明属性。我想为 onMouseClick() 函数建立两个单独的变量,这样我就可以根据单击它的鼠标按钮来更改函数的行为方式。一个用来开灯,一个用来关灯。所有这些都是在服务器脚本而不是本地脚本中完成的(不确定这是否意味着什么

local rightmouse = function onMouseClick()
local leftmouse = function onMouseClick()

当我尝试将 onMouseclick() 设为变量时,它在单词本身的正下方出现红色下划线,并告诉我“Workspace.barlight wheel.Script:5: Expected '(' when parsing function, got 'onMouseClick'" 有什么想法吗?

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    function onMouseClick() end 定义了一个函数。它不会解析为函数值。因此,您不能像在

    中尝试的那样将其分配给局部变量
    local rightmouse = function onMouseClick()
            print(" turned the lights off")
            barlight.Transparency = 1
    end
    

    这是不正确的语法。您可以通过两种方式定义函数:

    local function myFunction() end
    

    local myFunction = function () end
    

    要么

    local rightmouse = function ()
            print(" turned the lights off")
            barlight.Transparency = 1
    end
    

    如果你希望函数名是rightmouse,或者

    local function onMouseClick()
            print(" turned the lights off")
            barlight.Transparency = 1
    end
    

    如果您希望函数名称为 onMouseClick

    【讨论】:

      猜你喜欢
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 2013-06-04
      • 2021-12-23
      • 1970-01-01
      • 2020-04-10
      相关资源
      最近更新 更多