RemoteEvent fires multiple times depending on the number of players

Hello, this is my first time posting something on the Roblox Developer Forum so if I’ve done something wrong then tell me the right thing to do.

I was working on a model and I want it to send a message everytime a player writes on the GUI and clicks the send button.

I encountered 2 issues in my code and I don’t know how to fix them.
First: If there are for example 3 players in the game, when a player clicks the send button the script will send the same message three times instead of one time.
Second: I wanted to put text limits on the TextBox but if someone copies and pastes a large amount of text I get an error in the output that says “Maximum event re-entrancy depth exceeded for Instance.TextChanged”.

The scripts:

ServerScript

local TextService = game:GetService("TextService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.SendMessageToAllClients
local CooldownScript = require(script.Parent.Parent.CooldownBox.Bar.CooldownScript)
local FilteredText = ""
local FilteredTextResult

RemoteEvent.OnServerEvent:Connect(function(Player, Argument)
	if Argument == "ERRLIMIT0022020" then
		script.Parent.Parent.ErrorBox.Text = "The number of characters exceeds the limit of 100."
	elseif Argument == "ERRCOOLDOWN20020200" then
		script.Parent.Parent.ErrorBox.Text = "Please wait until the cooldown ends."
	elseif Argument == "ERRBOUNDS0020020" then
		script.Parent.Parent.ErrorBox.Text = "Text cannot exceed the TextBox's bounds."
	elseif Argument == "ERRBOUNDS0020200" then
		script.Parent.Parent.ErrorBox.Text = "Text deleted due to absolute limit being broken."
	elseif Argument == "ERREMPTY0022002" then
		script.Parent.Parent.ErrorBox.Text = "Please type something in the TextBox."
	else
		local Success, ErrorMessage = pcall(function()
			FilteredTextResult = TextService:FilterStringAsync(Argument, Player.UserId)
		end)
		if Success then
			script.Parent.Parent.ErrorBox.Text = ""
			FilteredText = FilteredTextResult:GetNonChatStringForBroadcastAsync()
			RemoteEvent:FireAllClients(Player,FilteredText)
			CooldownScript()
		elseif ErrorMessage then
			script.Parent.Parent.ErrorBox.Text = "An error occurred while filtering text."
		end
	end
end)

LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local RemoteEvent = ReplicatedStorage.SendMessageToAllClients
local TextBox = script.Parent.Parent.MainTextBox

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.CooldownBox.Bar.CooldownEnabled.Value == true then
		RemoteEvent:FireServer("ERRCOOLDOWN20020200")
	elseif string.len(TextBox.Text) > 100 then
		RemoteEvent:FireServer("ERRLIMIT0022020")
	elseif TextBox.Text == "" then
		RemoteEvent:FireServer("ERREMPTY0022002")
	else
		RemoteEvent:FireServer(TextBox.Text)
		wait()
		TextBox.Text = ""
	end
end)

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	if TextBox.TextFits == false then
		if string.len(TextBox.Text) > 350 then
			TextBox.Text = ""
			RemoteEvent:FireServer("ERRBOUNDS0020200")
		else
			TextBox.Text = TextBox.Text:sub(1, #TextBox.Text - 10)
			RemoteEvent:FireServer("ERRBOUNDS0020020")
		end
	end
end)

RemoteEvent.OnClientEvent:Connect(function(Player, Argument)
	local MessageSettings = {
		Text = "[Message Machine]: "..(Argument);
	}
	StarterGui:SetCore("ChatMakeSystemMessage", MessageSettings)
end)

I’m trying to find out how to fix this error, any help will be appreciated.

3 Likes

I’m not sure if this would help, but I noticed you did not use the FocusLost property for the submission of the player’s text. The first parameter would indicate if the player hit the enter key. This may ensure that it only sends once, but again I may be wrong.

1 Like

The model has a GUI TextButton that fires the event when clicked. However, the problem is probably the fact that all of the LocalScripts in every player’s PlayerScripts execute everytime a player clicks the button and I don’t know how to avoid this.

1 Like

IIRC the MouseButton1Click event is replicated to the server/client which is why it gets fired multiple times, you could try using the .Activated event and check if it still happens.

Yeah that’s right, Activated will only work on the client but MouseButton events can be used on the server too.

@malicuerist

Then you’re probably changing Object.Text after the event fires, this way the event’s supposed to fire again because the text was changed, so it’s just an endless loop.

I replaced .MouseButton1Click with .Activated but I got the same result.

Thank you for replying, I now changed the structure to avoid that problem and now the text works fine.

Where did you put the server script? If it’s somewhere that duplicates for each player, like in a gui or in StarterPlayerScripts, then each of them will react to .OnServerEvent, then each of them will do :FireAllClients

Handle :FireClient and :FireAllClients on a script that doesn’t duplicate. Then, handle the changes in gui, e.g. script.Parent.Parent.Parent.Text = …, on a local script

6 Likes

I separated some components of the GUI and placed them with the server script on the Workspace.
Now everything is working fine, thank you!

1 Like

Hi! I was trying to do this to Basic Admin Essentials in the Essentials Code (Local Script) inside of the admin, and it fires for like everyone.

Hey, I appear to be having the same issue as this guy described. However, in my script, I’m 100% there’s nothing wrong with it. I’ve checked countless times to see if it was being called multiple times, and it is not. The event seems to fire by the number of players, e.g., two players, two fires, three players, three fires, etc.

I know it’s been a while since this thread was active, but that would be awesome if I could get some help.

Details:
The script that receives the event fire is the server in the workspace. the script that sends the call is the client, in a GUI
and form of triggering is by mousebutton1click on a GUI object.

you should make another devforum Topic on that and also include:

  • the scripts
  • where the guis are placed
  • what, if anything, re-parents the guis
  • what, if anything, re-parents or enables the scripts