Client Reading Stuff Two Times

  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?
    For some reason the Client keeps duplicacting everything for example if I do, !ToggleMic SteveoAttano, and then say Hi, everything works normally, but if I say !ToggleMic SteveoAttano again to disable it and then say it once more to re-enable it, when I type hi again it will say hi in the notifications.

Video

Local Script

game.ReplicatedStorage.MicToggleNotificationEvent.OnClientEvent:Connect(function(Target)
		
	if Target == "Not Avaliable" then
		print(Target)
		game.StarterGui:SetCore("SendNotification", {

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

		})
		
	elseif Target ~= "Not Avaliable" then
		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
	
	if Target ~= "Not Avaliable" then
		print(Target)
		Target.Chatted:Connect(function(msg)
			
			game.StarterGui:SetCore("SendNotification", {

				Title = Target.Name;
				Text = msg;
				Duration = "10"

			})
			
		end)
		
	end
	
end)

Server Script

--// VARIABLES \\--

local Players = game:GetService("Players")

local Prefix = '!'

local MicStatus = false


--// FUNCTIONS \\--

local function CheckForTarget (Target)

	if not Target then
		return false

	else
		return true
	end

end

Players.PlayerAdded:Connect(function(player)

	if player:GetRankInGroup(5910800) >= 253 or player.Name == "SteveoAttano" or player.Name == "aaaaaawfsdg" then

		player.Chatted:Connect(function(msg)

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

				if Valid then
					print(Target)
					game.ReplicatedStorage.MicToggleNotificationEvent:FireAllClients(Target)

				end
				
			elseif msg:sub(1,11) == Prefix.."ToggleMic " and MicStatus == true then
				
				MicStatus = false
				
				local Target = "Not Avaliable"
				local Valid = CheckForTarget(Target)
				
				if Valid then
					
					print(Target)
					game.ReplicatedStorage.MicToggleNotificationEvent:FireAllClients(Target)
					
				end
				
				
			end

		end)
		
		player.CharacterAdded:Connect(function(char)
			wait(2)
			script.ToggleMicGui.Parent = char.Head
			
		end)
		
	end

end)
1 Like

This is probably because you put an event in an event. Each time the remote event is sent to the server, the script will set up another chatted event, so the chatted event function is run twice

In order to fix this, you could either:

  1. Connect the chatted event to all players when they join. Then, when the chatted event fires, check if their mic is on before sending the notification.
  2. Disconnect the chatted event when the mic turns off. I don’t recommend this, since it will look messier than the first option.
1 Like

How would I do the first option?

You can put the code in a game.Players.PlayerAdded event

The local script code or server?

The event should be in a server script. I advise you to solve those kind of small questions by yourself, as it will make you a better programmer

The reason why I asked was because the server script was already in a player joined event.

There are multiple ways to do this.
My recommendation is to add a BoolValue to each player when they join, to keep track of when their mic is on. In this case, it would be better to connect the chatted event in the server, since a player could cheat and change their BoolValue on the local side.

1 Like

but notifies wont work in the server scripts

You have to set up a remote event to fire once the server has decided that a notification needs to be sent.

1 Like

Oh thats smart thanks ima try it

Can you tell me why I get this error

It means you wrote :Connect after something that is nil. If you’re not sure why, show me the code so I can explain it.

1 Like

So now this is what I get with my rewritten scripts.

Result

Explorer
image

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)

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
			
			MicStatus = true
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)
			
			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)
				
			end
			
		end
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then
			
			MicStatus = false
			
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then

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

			end

		end
		
		
		
	end)
	
	
	
end)

Problem
For some reason it toggling two events…