My goal is to create a simple admin command system.
However this:
chat.SpeakerAdded:Connect(function(spkrN)
local plr = game.Players:FindFirstChild(spkrN)
if plr and checkIfAdmin(plr) then
local spkr = chat:GetSpeaker(spkrN)
spkr.SaidMessage:Connect(function(message, channel)
print(message.Message)
end)
end
end)
Only prints nil, and documentation on the wiki only yields a confusing “What do I do with this?”
Their command function addition does not state how to add it or the method to add it.
I have searched devforum for any help but I didn’t find satisfactory results.
I’ve never used speakers as a way to detect chats, but you can achieve the same thing with the event Player.Chatted. The only downside to this that I am aware of is that you don’t know what channel the messages were sent in.
It does work. I just used it in a script builder a second ago so I can confirm. Perhaps send your attempt of using Chatted so we can see why it doesn’t work for you.
I have just created a simple shutdown command and a command that gives player admin commands. Make sure you run it from the server in ServerScriptService.
local Prefix = ";" -- The prefix
local Admins = {"waterrunner","","",""} -- All the admins
game.Players.PlayerAdded:Connect(function(Player) -- This allows you to get the player.
Player.Chatted:Connect(function(Message) -- Checks if the player has chatted and gets their message.
for i,v in pairs(Admins) do -- Loops through the admins table.
if Player.Name == v then -- Checks if the player is admin.
-- Shutdown Command
if string.sub(Message,1,9):lower() == Prefix.."shutdown" then -- Make sure the :lower() is lower case otherwise it wont work.
for i,v in pairs(game.Players:GetChildren()) do -- Loops through all the players
v:Kick("The game has been shutdown") -- Kicks the player.
end
end
-- Give admin command
if string.sub(Message,1,6):lower() == Prefix.."admin" then
local NewAdmin = string.sub(Message,7) -- Gets the player name
table.insert(Admins,NewAdmin) -- Inserts the new admin to the admin table.
end
-- New command
end
end
end)
end)
A few tips:
:lower() makes it so the command can be upper or lower case. E.g: ;AdMin waterrunner
string.sub() this is really helpful as you can select what part of the string you want, for example in the give admin command I have done string.sub(Message,7), this returns all the characters after 7 characters in the string.
Nope, all I want to do is detect the chat and see what the player says. If the player is an admin and the player says a command then do something with that command…
The reason this works is because you have some kind of yield method before this event is connected. If your code yields at the beginning, then the PlayerAdded function doesn’t connect immediately. That’s why typically you should create a handler for both new players and ones already in the game.
local Players = game:GetService("Players")
local function Chatted(message, recipient)
-- stuff
end
Players.PlayerAdded:Connect(function (Player)
Player.Chatted:Connect(Chatted)
end)
for _, Player in pairs(Players:GetPlayers()) do
Player.Chatted:Connect(Chatted)
end
It most likely also is your initial problem to begin with. Your code is probably yielding before you connect to PlayerAdded. Waiting at the beginning is not needed before connecting (and you shouldn’t arbitrarily wait to do it either), just tailor your code to handle this case.