Player msg after chatted event dont work

it dont work, and i dont know why

here script

local Players = game:GetService("Players")

local admins = {
	[1] = true,
	[2] = true,
	[3] = true,
	[4] = true
}

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "/cmd" and admins[player.UserId] then
			print("yes")
		end
	end)
end)

admins list work, but msg dont

Unless your Roblox account’s the Roblox account (which would honestly be awesome), John Doe, Jane Doe or whoever id 4 is, you’re going to have to enter your own user id as its own index in the dictionary. You can find a user’s id by going to their profile, and looking at the url.
player_user_id
17514438 is my user id, so I would add [17514438] = true to the admins dictionary.

I hope this helped. If you have any questions please don’t hesitate to ask. :slight_smile:

1 Like

i said admins list work, but msg dont.
and i make this [1,2,3,4] to hide admin friends

so msg still dont work

here is original script

--services
local Players = game:GetService("Players")
local SerStorage = game:GetService("ServerStorage")

local admins = {
	[34485603] = true
}

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == "/cmd" and admins[player.UserId] then
			print("yes")
		end
	end)
end)

i only deleted my friends from the admins list

Have you tried doing msg:match("/cmd")?

2 Likes

Try this.

--services
local Players = game:GetService("Players")
local SerStorage = game:GetService("ServerStorage")

local admins = {
	[34485603] = true
}

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg:lower():sub(1,4) == "/cmd" and admins[player.UserId] then
			print("yes")
		end
	end)
end)

This works for me.

Ok lol, I didn’t see the last bit. My apologies.

There’s a couple of things that can be tried to see what’s happening.

  1. Check if PlayerAdded is picking up the player; I would expect that it would, but ya never know.
  2. Try changing the command prefix to a colon : or exclamation point !, I recall there being some issues back then with command scripts using the forward slash /.

If neither of those work, lemme know.

EDIT
I wouldn’t recommend going with the match approach, as doing that doesn’t take into account any additional arguments that a command may have.
A better solution would be to try @Darkmist101’s solution, or to use find and get the additional arguments after the prefix.
For example

local Prefix = ":";
local Msg = "!kill plr";

-- In code
local Pos = Msg:find(Prefix);
if (Pos ~= nil) then
    Msg = Msg:sub(1 + Pos);
    if (Msg:sub(1, 5) == "kill ") then
        -- code
    end
end

I hope this helped. :slight_smile:

1 Like

Could be related to roblox breaking chat input, make sure to sanitize any chat:

msg = string.gsub(msg, "%c", "")
1 Like

oh, it’s now work.

it used to work with
msg == "/cmd"
but studio just said big NO to me

1 Like