Difficulty understanding the chat system

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.

game.Players.playerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        print(message)
    end)
end)

I have tested that method, it no longer works…

It should, Can i see your new script?

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.

Here is a wiki page about chat commands: Player | Documentation - Roblox Creator Hub
This page is also very helpfull when making admin commands: Documentation - Roblox Creator Hub

Here is what happens:

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(mess)
		print(mess)
	end)
end)

Nothing is outputted.

Location of script:
image

Is your script disabled. That should be working.

Nope, because the function in the OP works.
Is it because I haven’t published the game? It’s currently an rbxl place file…

Are you using a custom chat by any chance? I don’t actually know if that’d affect anything, but it seems like it probably could.

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…

Try using my script as I know that script works. Also you could try publishing the game.

Try this
wait(1)
for _,Player in pairs(game.Players:GetPlayers()) do
Player.Chatted:connect(function(S)
print(S)
end)
end

That works for some reason?
Is it because the player added event fails to run when the game first starts up?

Yeah i think thats why something with solo play where the player loads faster than the server or smth

welp i guess this can be marked as solved now

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.

cc @Sebastian_Wilson