How do I check if a player typed something in a table?

I’m working with admin commands, and I’m trying to see if the player types “I”, “self” or “me”
instead of making a separate “if” for each one, I tried putting them in a table and see if the player typed something in that table. I tried different solutions but none worked or were making the code too long, as I don’t know much regarding tables

I’ve got a working solution here, but I want to make it shorter, it feels weird to make
“args[n] == selfcmd[x]” for each one. Also, I’d like to see if the player typed something that’s in “stats”

--// relevant part
local selfcmd = {"me", "i", "self"}
local stats1 = {"bucks", "gems"}
local stats2 = {"level", "xp"}
commands.give = function(player, args)
	if args[1] and not args[2] then return end
	if not args[3] then warn("No 3rd argument given. Try putting a number?") return end
	if args[1] == selfcmd[1] or args[1] == selfcmd[2] or args[1] == selfcmd[3] then
		if args[2] == "bucks" then
			player.leaderstats.Bucks.Value += args[3]
		elseif args[2] == "gems" then
			player.leaderstats.Gems.Value += args[3]
		elseif args[2] == "xp" then
			player.XP.Value += args[3]
		elseif args[2] == "level" then
			player.Level.Value += args[3]
		end

	elseif args[1] == "all" then
		for _, v in pairs(Players:GetPlayers()) do
			if args[2] == "bucks" then
				player.leaderstats.Bucks.Value += args[3]
			elseif args[2] == "gems" then
				player.leaderstats.Gems.Value += args[3]
			elseif args[2] == "level" then
				player.leaderstats.Level.Value += args[3]
			elseif args[2] == "xp" then
				player.XP.Value += args[3]
			end
		end
	else
		for i, v in pairs(Players:GetPlayers()) do
			if string.lower(v.Name) == args[1] then
				if args[2] == "bucks" then
					player.leaderstats.Bucks.Value += args[3]
				elseif args[2] == "gems" then
					player.leaderstats.Gems.Value += args[3]
				elseif args[2] == "level" then
					player.leaderstats.Level.Value += args[3]
				elseif args[2] == "xp" then
					player.XP.Value += args[3]
				end
			elseif not args[2] then warn("invalid args or player doesn't exist") end
		end
	end
end

If you don’t understand what I’m saying, I want to make something like

if args[1] --[[the player]] == selfcmd --[[check if it's in the table]] then
-- do stuff
end

and

if args[2] --[[the stat]] == stats1 --[[check if it's in the table]] then
   player.leaderstats.args[2] += args[3] -- the amount
end

if args[2] --[[the stat]] == stats2 --[[check if it's in the table]] then
   player.args[2] += args[3] -- the amount
end

You can simply use table.find(), here’s how you would use it in your case

if table.find(selfcmd, args[1]) then
 --do stuff
end

you’re a life saver! I’ve tried using table.find but apparently I was using it wrong and that’s why it didn’t work. Thank you!

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