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

Roblox服务端代码执行器RemoteEvent触发两次问题求助

问题解决:Roblox代码执行器重复执行代码

问题根源

你的客户端代码存在两个关键问题:

  1. FocusLost事件在按下回车时可能因UI交互逻辑触发两次,而你的防抖逻辑有漏洞——Debounce = false写在了if块外部,不管是否进入防抖分支都会重置状态,等于没生效。
  2. 多余的repeat-until循环完全没必要,它除了增加代码复杂度,还可能引发意外的重复调用。

修复后的客户端代码

local textbox = script.Parent
local event = game.ReplicatedStorage.FireCode
local isDebounced = false

textbox.FocusLost:Connect(function(enterPressed)
    if enterPressed and not isDebounced then
        isDebounced = true
        event:FireServer(textbox.Text)
        task.wait(1) -- 保持1秒防抖窗口
        isDebounced = false
    end
end)

服务端代码优化(必改)

你当前的服务端用了全局防抖,会导致所有玩家共用一个锁,其他玩家在0.1秒内无法执行代码。改成按玩家单独防抖才合理:

local event = game.ReplicatedStorage.FireCode
local playerDebounces = {}

event.OnServerEvent:Connect(function(player, code)
    if not playerDebounces[player] then
        playerDebounces[player] = true
        loadstring(tostring(code))()
        task.wait(0.1)
        playerDebounces[player] = nil
    end
end)

核心修复说明

  • 移除客户端冗余的repeat-until循环,简化逻辑避免误触发
  • 将客户端的防抖状态重置操作移到等待之后,确保只有进入防抖流程后才会解锁
  • 服务端改用玩家级别的防抖,避免不同玩家的操作互相干扰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:02:37