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