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.
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)
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
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.
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)