Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I Can't Seem to stop my Remote from firing more than once?

Hi, I'm New to both Lua and This site, I just started working with remotes, I'm confused about what is happening with my code, I am making a global Announcement to use Across my Game's servers, What is happening is say I type '/Global Bob' It'll output '[Global] Bob' Into Chat, but then if I then use the command again, let's say '/Global Hello World!' it Would output [Global] Hello World!' Two times, and if I was to do it again, it would output the same way, but three times, so on and so fourth. And I Honestly Have no Idea how to figure it out, I put in a Count to Determine that the Event is Indeed being fired More than Once, but I am stuck, and I would appreciate any help.

Script in Server Script Service:

local messagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local chatService = game:GetService("Chat")
local remoteEvent = ReplicatedStorage:WaitForChild("GlobalEvent")
local count = 1

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        local messageCmd = "/global "
		local toSend
		local toFilter
		if string.sub(msg,1,8):lower() == messageCmd then
			toFilter = string.sub(msg,8)
			toSend = chatService:FilterStringForBroadcast(toFilter, player)

			local success, errorMessage, connection = pcall(function()
	
				messagingService:SubscribeAsync("GlobalAnn", function(message)
					remoteEvent:FireClient(player, message.Data)
					count = count + 1
				end)
				warn("I Don't Know Why, but We Fired Client ", count, " Times for no Reason :)")
			end)
			wait(2)
			local success, errorMessage = pcall(function()
				messagingService:PublishAsync("GlobalAnn",toSend)
			end)
		end
  	end)
end)

Thanks for the Help!

1 comment

0

So the Amount it is firing seems to be directly correlated to the amount of times I've done '/global' so I am wondering if the problem is caused by the script looking through my chat history too, and not just my last message

1 answer

I Found the Solution, It was Due to the fact that I had the Remote Firing in:

local success, errorMessage, connection = pcall(function()
        messagingService:SubscribeAsync("GlobalAnn", function(message)
			remoteEvent:FireClient(player, toSend)
		end)

Which I had in the Chatted Function, So Whenever I chatted It would subscribe me to the "GlobalAnn" messaging service then fire the event, this would cause me to be subscribed to "GlobalAnn" more than once, and fire the event accordingly, which was causing the Error to Occur, So now I fire the Event like this:

local success, errorMessage = pcall(function()
                	messagingService:PublishAsync("GlobalAnn",toSend)
                   		remoteEvent:FireClient(player, toSend)
           		end)

So, I will only be Subscribed to it Once, and it will only fire the Remote after I Chat. (Which Fixes the Error in it's Entirety)

This discussion is preserved as it was. New answers, comments, and votes are closed.