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

Roblox游戏中Discord Webhook重复发送消息问题求助

Roblox脚本重复发送消息问题解决方案

问题根源

重复发送的核心原因是在事件回调内部重复绑定新的事件监听器:

  • LocalScript中每次文本框失去焦点,都会新增一个filter2.OnClientEvent的监听器
  • 消息发送脚本中每次点击发送按钮,都会新增一个rem2.OnServerEvent的监听器
    后续触发事件时,所有之前绑定的监听器都会执行,导致发送次数随操作次数递增

修改后的脚本

1. LocalScript(修正监听器绑定位置)

local rem = game.ReplicatedStorage:WaitForChild("Server")
local rem2 = game.ReplicatedStorage:WaitForChild("Client")
local TextService = game:GetService("TextService")
local textbox = game.Workspace:WaitForChild("Part").SurfaceGui.TextBox
local player = game.Players.LocalPlayer
local userid = player.UserId
local filter1 = game.ReplicatedStorage:WaitForChild("FilterServer")
local filter2 = game.ReplicatedStorage:WaitForChild("FilterClient")
local filter3 = game.ReplicatedStorage:WaitForChild("FilteredBAD")
local earlymessage = ""
local message = ""

-- 将filter2的监听器移到外部,只绑定一次
filter2.OnClientEvent:Connect(function(msg)
    message = msg
    rem2:FireServer(message)
    wait(3)
end)

textbox.FocusLost:Connect(function(inputObj)
    earlymessage = textbox.Text
    print(userid)
    tostring(earlymessage)
    print(earlymessage)
    filter1:FireServer(earlymessage)
end)

filter3.OnClientEvent:Connect(function(text)
    textbox.Text = text
end)

2. 消息发送脚本(修正监听器绑定位置)

local http = game:GetService("HttpService")
local cooldown = false
local rem1 = game.ReplicatedStorage:WaitForChild("Server")
local rem2 = game.ReplicatedStorage:WaitForChild("Client")

-- 将rem2的监听器移到外部,只绑定一次
rem2.OnServerEvent:Connect(function(player, message)
    if cooldown then return end
    print(message)
    local Data = {
        ["content"] = message
    }
    Data = http:JSONEncode(Data)
    print(Data)
    http:PostAsync("https://webhook.lewisakura.moe/api/webhooks/keygerekiki", Data)
    cooldown = true
    wait(3)
    cooldown = false
end)

script.Parent.Parent.Parent.Send.SendGui.TextButton.MouseButton1Click:Connect(function()
    if cooldown == false then
        rem1:FireAllClients()
        print("client fired")
    end
end)

3. 过滤脚本(无需修改,保持原有逻辑)

local rem = game.ReplicatedStorage:WaitForChild("FilterServer")
local rem2 = game.ReplicatedStorage:WaitForChild("FilterClient")
local rem3 = game.ReplicatedStorage:WaitForChild("FilteredBAD")
local TextService = game:GetService("TextService")
local finalmessage = ""
local cooldown = false

rem.OnServerEvent:Connect(function(player, startmessage)
    if cooldown == false then
        local success, errorMessage = pcall(function()
            tostring(startmessage)
            local userid = player.UserId
            finalmessage = TextService:FilterStringAsync(tostring(startmessage), userid):GetNonChatStringForBroadcastAsync()
            rem3:FireAllClients(finalmessage)
            tostring(finalmessage)
            rem2:FireClient(player, finalmessage)
            print(finalmessage)
            cooldown = true
            wait(4)
            cooldown = false
        end)
        if not success then
            warn("Error filtering text:", errorMessage)
            rem3:FireAllClients()
        end
    end
end)

额外优化建议

  • 可以在LocalScript中添加文本为空的判断,避免发送空消息
  • 过滤脚本中的finalmessage变量可以放在事件回调内部,避免多玩家并发时的变量污染

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 06:10:11