Need help with pulling strings out of a table in a table

Okay so this is what I want to do, give custom classes to administrators for a group game.

local Players = {
{UserId = 7327342, Class = "Dragonborn", Abilities = {"Dragon Shout", "Ice Slash"} },
{UserId = 7327342, Class = "Necromancer", Abilities = {"Skeleton Summon", "Zombie Attack"} },
{UserId = 7327342, Class = "Elven King", Abilities = {"Nature Heal", "Tree Coverage"} }
}

now what i want to do is be able to check the Abilities table to see if they have the ability so that I can confirm before allowing them to execute it, doing the following:

for i,v in pairs(Players.Abilities) do
  if v == "Nature Heal" then
    print("He has the gift to heal")
  end
end

However when i do it, it will only return one value of the table or will error me this:

  16:54:46.932 - ServerScriptService.MasterScript.MainHandler.OtherScripts.PersonalSpawner:52: bad argument #1 to 'match' (string expected, got table)

Any suggestions?

1 Like

It’s kind of hard to help when we’re only seeing a portion of your script.

If you could please post the entire script it’d be much appreciated and’ll allow us to help you more efficiently. Thanks. :smile:

1 Like

Example

local Players = {
Admin1 = {ID = 0, Ability = {"Clean","Other"}},
Admin2 = {ID = 1, Ability = {"Ahhh","Jump"}},
}


for i,v in pairs(Players) do
if v.Ability[1] == "Clean" then
	print(v.ID..' can clean')
else
	print(v.ID..' cannot clean')		
end
end

Another way you can see what ‘ Abilitys ’ a player has is by doing this:

for i,player in pairs(Players) do
for i,Name in pairs(player.Ability) do
print(player.ID..' has '..Name)
end
end

Edit: I believe this is what you may be looking for.

local Players = {
{UserId = 7327342, Class = "Dragonborn", Abilities = {"Dragon Shout", "Ice Slash"} },
{UserId = 321414321, Class = "Necromancer", Abilities = {"Skeleton Summon", "Zombie Attack"} },
{UserId = 84541, Class = "Elven King", Abilities = {"Nature Heal", "Tree Coverage"} }
}

game.Players.PlayerAdded:connect(function(nP)
	for _,v in pairs(Players) do
		if nP.UserId == v.UserId then
			nP.Chatted:connect(function(msg)
------------------------------------------------------------------
				if msg == "fus ro dah" then
					local state = false
					Validate(v.Abilities, state, "Dragon Shout")

					if state == true then
						print("Summon dragon shout")
					end
				end
-------------------------------------------
			end)
		end
	end
end)

function Validate(v, state, obj)
	for i=1, #v do 
		if string.match(v, obj) then
			state = true
		end
	end
end

There are a few solutions

  1. Restructure your table in the following format
        Abilities = {
          DragonShout = false,
          IceSlash = true,
          ZombieAttack = false,
        }

Set the value of each ability to true or false indicating if the player has access to that ability. Just access Abilities.DragonShout to determine if they have access.

  1. If you want to commit to the current format, here’s a generic function to determine if an index exists in a table
local function IsInTable(table, value)
  for _, v in pairs(table) do
    if v == value then return true end
  end

return false
end
1 Like

I found the solution:

local state = Validate(Players.Abilities, "Poker")
					
if state == true then
	print("Execute Powers here")
end

function Validate (v, obj)
	local state = false 
	
	for i=1, #v.Abilities do 
		if tostring(v[i]) == obj then
			state = true
			return state
		end
	end
	
	if state == false then
		return state
	end
end

Can’t you just:


local state = Validate(Players.Abilities, "Poker")
					
if state then
	print("Execute Powers here")
end

function Validate (v, obj)
	local state = false 
	
	for i = 1, #v.Abilities do 
		if tostring(v[i]) == obj then
			state = true
			return state -- kept this because it ends the loop
		end
	end

	return state
end