How would i check a player chat, for values 1 and 2?

For this issue, i want to check when the player chats, 1. If the command is used first. and 2. What is the varible they are specifying.

The main problem with this, is when i do this, the output will just print the table ID and won’t go any futher. Also, if i just check if they used the command, it will fail + stop if i say anything else with it. “!Spawn Cheeseburger” (Will fail)

I’ve tried using str:split(" "), but this will still only check for str[1] and will ignore str[2]

P.Chatted:Connect(function(c)
	local SimplifiedChat = c:split(" ")
	print(SimplifiedChat)
. . .
if SimplifiedChat[1] == TCmds["TowerSpawn"] and SimplifiedChat[2] == table.find(TNames, SimplifiedChat[2]) then

failed

This is your problem right here:

Chatted returns an instance, a message, and a color. You are trying to parse the instance which is why it make a table

If you’re using this Chat | Roblox Creator Documentation

2 Likes

I think this is what you want to use.
https://create.roblox.com/docs/reference/engine/classes/Players#PlayerChatted

code segment from docs:

local Players = game:GetService("Players")

local function onPlayerChatted(playerChatType, sender, message, recipient)
	print(sender.Name, "-", message, "-", playerChatType, "-", (recipient or "All"))
end

Players.PlayerChatted:Connect(onPlayerChatted)
2 Likes

Where Did you get this?? Player.Chatted only returns a message and a player object
Screenshot 2023-02-06 at 10.24.24

1 Like

im pretty sure its a npc chatted event not player.chatted

1 Like

Have you tried debugging? Use ipairs, loop through the table of strings and see what’s wrong. Maybe it’s not :split that doesn’t work, but the checks you do in the next lines.

I mean it says

So I doubt they are talking about an NPC

player.Chatted only returns message and receipt

Correct me if I’m wrong but

and SimplifiedChat[2] == table.find(TNames, SimplifiedChat[2])

Should just be

table.find(TNames, SimplifiedChat[2])

Player.Chatted has the parameters message recipient. Chat.Chatted has the parameters part, message, color.

The OP is using Player.Chatted, so their code is correct in that regard.

Split always returns a table of strings. For example:

local splitString = "a b c":split(" ")
-- splitString is now {"a", "b", "c"}

I would print out the split parts of the string to make sure they’re what you want.

This expression also doesn’t make sense:

SimplifiedChat[2] == table.find(TNames, SimplifiedChat[2])

For this to be true, there would have to be a table key that is equal to SimplifiedChat[2] and the same as the key.

For example, if SimplifiedChat[2] was “xyz” this would only return true if the table TNames contained: {xyz = “xyz”}, which doesn’t make sense.

You may be trying to check if the table contains the value, in which case you should use only table.find(TNames, SimplifiedChat[2]).

2 Likes

^
(30 letters)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.