Webhook Sending Multiple Messages

My game has a surface gui on a part that connects to a webhook which sends that message, but it is sending it multiple times (1 more each time it is sent to the discord). I looked through my code for errors, but I couldn’t find any so I came here lol.

Script inside the textbox:

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

script.Parent.Parent.Parent.Send.SendGui.TextButton.MouseButton1Click:Connect(function()
	if cooldown == false then
		rem1:FireAllClients()
		print("client fired")
		rem2.OnServerEvent:Connect(function(player, message)
			print(message)
			local Data = {
				["content"] = message
			}
			Data = http:JSONEncode(Data)
			print(Data)
			http:PostAsync("https://webhook.lewisakura.moe/api/webhooks/keykeykeyitykey", Data)
			cooldown = true
			wait(3)
			cooldown = false
		end)
	end
end)

Script inside server script service for filtering that text:

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)

Script inside of startergui:

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 = ""

textbox.FocusLost:Connect(function(inputObj)
	earlymessage = textbox.Text
	print(userid)
	tostring(earlymessage)
	print(earlymessage)
	filter1:FireServer(earlymessage)
	filter2.OnClientEvent:Connect(function(msg)
		message = msg
		rem2:FireServer(message)
		wait(3)
	end)
end)

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

I tweaked it a little bit to optimize, but it still sends multiple messages, one more each time. :frowning:

1 Like

It appears the problem is occurring because a new connection is being made to the rem2.OnServerEvent event handler each time the button is clicked in the MouseButton1Click event. This results in multiple duplicate messages being sent with each click.

To resolve this, move the rem2.OnServerEvent:Connect line outside of the MouseButton1Click event. By establishing the connection only once, it will persist across clicks and prevent the duplication issue.

It still happens. Just to make sure, you are telling me to change the first script to

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

rem2.OnServerEvent:Connect(function(player, message)
	print(message)
	local Data = {
		["content"] = message
	}
	Data = http:JSONEncode(Data)
	print(Data)
	http:PostAsync("https://webhook.lewisakura.moe/api/webhooks/key", Data)
	cooldown = true
	wait(3)
	cooldown = false
end)

Right? This still does the same thing, unless I’m mistaken.

would also exclude this

from focuslost function

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

rem2.OnServerEvent:Connect(function(player, message)
        print(message)
        local Data = {
            ["content"] = message
        }
        Data = http:JSONEncode(Data)
        print(Data)
        http:PostAsync("https://webhook.lewisakura.moe/api/webhooks/keykeykeyitykey", Data)
end)

script.Parent.Parent.Parent.Send.SendGui.TextButton.MouseButton1Click:Connect(function()
    rem1:FireAllClients()
    print("client fired")
end)

Try changing your first script to this / later on implement the cooldown

1 Like

Wow, that fixed it! I guess I was storing the message more than once. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.