Remote Events Firing Twice

  1. What do you want to achieve?
    When I type !ToggleMic (Username), I want it to pop up with notification for everyone saying that players mic is on, and when I type !ToggleMic (Username) again I want it to pop up that their mic is off. Whenever their mic is one, I want anything they say to pop up as a notification, but when it is disabled I don’t want their chat to pop up in a notification.

  2. What is the issue?
    The issue is that when I do !ToggleMic (username) it turns it on and off at the same time, meaning that something is going wrong with the remote events but I don’t know what.

Result

*Explorer
image

Server Script

--// VARIABLES \\--

local Players = game:GetService("Players")

local Prefix = '!'

local MicStatus = false


--// FUNCTIONS \\--

local function CheckForTarget (Target)	
	
	if not Target then
		
		print("Target is not valid")
		return false
		
	else
		
		print("Target is valid")
		return true
		
	end
	
end



Players.PlayerAdded:Connect(function(player)
	
	
	
	player.Chatted:Connect(function(msg)
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == false then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)
			
			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.MicOnNotifierEvent:FireAllClients(Target)
				
			end
			MicStatus = true
		end
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then

				game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)

			end
			MicStatus = false
		end
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then

			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent:FireAllClients(Target, msg)

			end

		end
		
		
		
	end)
	
	
	
end)

Local Script

game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent.OnClientEvent:Connect(function(Target)
	
	print(Target)
	game.StarterGui:SetCore("SendNotification", {

		Title = "Toggle Mic";
		Text = Target.Name.."'s Mic is off";
		Icon = "http://www.roblox.com/asset/?id=6853258454";
		Duration = "10"

	})

end)

game.ReplicatedStorage.ToggleMicEvents.MicOnNotifierEvent.OnClientEvent:Connect(function(Target)
	print(Target)
	game.StarterGui:SetCore("SendNotification", {

		Title = "Toggle Mic";
		Text = Target.Name.."'s Mic is on";
		Icon = "http://www.roblox.com/asset/?id=257119670";
		Duration = "10"

	})
	
end)

game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent.OnClientEvent:Connect(function(Target, msg)
	print(Target)
		game.StarterGui:SetCore("SendNotification", {
		Title = Target.Name;
		Text = msg;
		Duration = "10"

	})
	
end)

Why don’t you just keep one single remote event and keep a table of toggled players in a table on the server? That way you can just receive the remote event, lookup what the user’s mic is set to and then do value = not value to toggle it.

1 Like

But I only want one player able to toggle it, and if someone else wants to toggle it then the person who already has it toggle will have to do the command again to untoggle it

But aside the fact that I have separate remote events for them all, do you know why it activates two of them?

if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then

				game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)

			end
			MicStatus = false
		end
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then

			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent:FireAllClients(Target, msg)

			end

		end

The problem here is you are catching the Event twice.

1 Like

Could you please point out where I messed up exactly?

You’re setting MicStatus to true, and then checking if it’s true, you need to do:

if msg:sub(1, 11) == Prefix .. "ToggleMic " and MicStatus == false then
  -- Mic on
elseif msg:sub(1, 11) == Prefix .. "ToggleMic " and MicStatus == true then
 -- Mic off
end
1 Like

It’s the ToggleMic, you’are catching the event twice.

1 Like

Hey I am really sorry if I sound silly but, your saying I shouldnt set it back to true?

You’re not silly lol, where the mic on and mic off comments are, you put the code for each, and you set the mic status to true and false respectively

Hey I am really really sorry and I really really appreciate your help but do you maybe think you can write the code for me? I know I shouldnt ask but I just cant seem to see how I set the MicStatus wrong.

Okay:

if msg:sub(1, 11) == Prefix .. "ToggleMic " and MicStatus == false then
	local Target = Players:FindFirstChild(msg:sub(12))
	local Valid = CheckForTarget(Target)

	if Valid then
		game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent:FireAllClients(Target, msg)
	end
	MicStatus = true
elseif msg:sub(1, 11) == Prefix .. "ToggleMic " and MicStatus == true then
	local Target = Players:FindFirstChild(msg:sub(12))
	local Valid = CheckForTarget(Target)

	if Valid then
		game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)
	end
	MicStatus = false
end
1 Like

Hey so I tried it out and it doesnt work, the reason why I have three seperate remote events is cause anytime I type !ToggleMic SteveoAttano I want it to turn on and show the on notification but anytime I type it again it will turn off and show the off notification, and whenever it is on, anything I say will be put as a notifer

This is my script now and this is what happens

Result

Script

Players.PlayerAdded:Connect(function(player)
	
	
	
	player.Chatted:Connect(function(msg)
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == false then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)
			
			if Valid then
		
				game.ReplicatedStorage.ToggleMicEvents.MicOnNotifierEvent:FireAllClients(Target)
				
			end
			MicStatus = true
		end
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true  then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)

			end
			MicStatus = false
		end
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == false then

			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent:FireAllClients(Target, msg)

			end
			
		end
		
		
		
	end)

Again I am really sorry if this seems obvious it is just hard for me.

Update:

Everything works now, OTHER then the chat event. So when the mic is on and I chat it doesnt pop up as a notification

Script

--// VARIABLES \\--

local Players = game:GetService("Players")

local Prefix = '!'

local MicStatus = false

local ReadyToTalk = false


--// FUNCTIONS \\--

local function CheckForTarget (Target)	
	
	if not Target then
		
		print("Target is not valid")
		return false
		
	else
		
		print("Target is valid")
		return true
		
	end
	
end



Players.PlayerAdded:Connect(function(player)
	
	
	
	player.Chatted:Connect(function(msg)
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == false then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)
			
			if Valid then
		
				game.ReplicatedStorage.ToggleMicEvents.MicOnNotifierEvent:FireAllClients(Target)
				
			end
			MicStatus = true
			ReadyToTalk = true
			
		elseif msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then

				game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)

			end
			MicStatus = false
			ReadyToTalk = false
			
		elseif msg:sub(1, 11) == Prefix.."ToggleMic " and ReadyToTalk == true then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then

				game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent:FireAllClients(Target, msg)

			end
			
		end
		
	end)

end)

Bcecause you’re checking if they type “!ToggleMic” when they are ready to talk, it should only be if ReadyToTalk == true