You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Studio兑换码LocalScript报Expected 'end'语法错误求助

错误产生原因
  • 语法层面:第1行script.Parent.Parent.Enter.MouseButton1Click:Connect(function()开启了匿名函数作用域,但全程没有编写对应的end)闭合该函数,Lua解析器遍历到文件末尾仍未找到匹配的闭合标记,因此抛出语法错误。
  • 逻辑层面:你将输入框回车监听、服务端返回监听都嵌套在按钮点击回调内部,会导致每点击一次按钮就重复绑定一次事件,后续触发时会多次执行相同逻辑,产生意料之外的bug。
修复方案

如果你的预期需求是同时支持「点击Enter按钮兑换」、「输入框按回车自动兑换」两种触发方式,把所有事件监听放到最外层即可,修复后代码如下:

local redeemEvent = game.ReplicatedStorage:WaitForChild("RedeemCode")
local inputBox = script.Parent
local enterBtn = script.Parent.Parent.Enter

-- 点击按钮触发兑换
enterBtn.MouseButton1Click:Connect(function()
    redeemEvent:FireServer(inputBox.Text)
end)

-- 输入框按回车触发兑换
inputBox.FocusLost:Connect(function(enter)
    if enter then
        redeemEvent:FireServer(inputBox.Text)
    end
end)

-- 监听服务端返回的提示信息
redeemEvent.OnClientEvent:Connect(function(str)
    inputBox.Text = str
    task.wait(3)
    inputBox.Text = "Enter Code Here!"
end)

如果你确实需要保留原有的嵌套逻辑,仅修复语法错误的话,只需要在代码末尾补上end)闭合第一行的函数即可,但该写法会存在上述重复绑定的逻辑问题,不推荐使用。

内容的提问来源于stack exchange,提问作者Braydon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:48:07