Checking if player said something in table

Alright, the title was a little confusing but basically I’m making a trading system.
The script for sending the request will work if the player says

"!Trade "..[Player you wish to trade with]

My problem is that I need to check if the player said another player’s name.
I have a table called players and every time someone joins it’ll add their name to that table
I want to make it so it checks all the values of that table and if the name the player said is in that table it’ll continue the script

local players = {
	
}

game.Players.PlayerAdded:Connect(function(player)
	
	table.insert(players, player.Name)
	
	player.Chatted:Connect(function(msg)
		if msg == "!Trade ".. then
			
		end
	end)
end)

If that doesn’t make sense please reply asking for more clarification.

TLDR: I’m trying to figure out how to make an if statement checking if the player says something and that message is part of a table

local players = {}

game.Players.PlayerAdded:Connect(function(Player)
	if not table.find(players,Player.Name) then
		table.insert(players,Player.Name)
	end
	Player.Chatted:Connect(function(Message)
		for _,plr in pairs(players) do
            if plr ~= Player.Name then 
                if Message:match(plr) then
				   print(Player.Name.."sent trade request to -"..Message:match(plr))
		   	    end
             else
                warn("Cant trade yourself!")
            end
		end
	end)
end)

Can I change the

if Message:match(plr) then
--blah blah
end

to

if Message == "!Trade "..plr then
--blah blah
end

Without breaking anything.
I just tested it and it worked fine but since I don’t know how match works I’m not sure if it was vital or not.

(Ty btw, it was so obvious once you pointed out the i,v loop)

I think it should be "!Trade"..Message:match(plr.Name)

Second thing, why inserting player in a table when they join? what if they leave the player will be still in the table.

Just do for i,plr in pairs(game.Players:GetPlayers())

I know that, I used her script, to not confuse her.

I was gonna add a player removing but that works fine too

Using “:match()” will check the entire message, and see if that specific word is there.

I would prefer the message to be exactly “!Trade [player name]” so as to avoid accidently trading with someone. Does that mean this works fine?

if Message == "!Trade "..plr then

He btw, avatar is deceiving

Yes, that would work fine aswell.

1 Like