Jump to content

[HELP] Reading a table within a table within a table


..:D&G:..

Recommended Posts

Hi, so I'm trying to make a script where I store some group names, ids of players within that group and the RGB for the outputChatBox. Here's my table:

local groups = {

	fondatori = {
		name = "Fondator|Dev |", 
		ids = {1, 3, 54, 85}, -- ID Of Players
		RGB = {0, 153, 255} -- RGB
	},
	
	fondatori2 = {
		name = "Fondator|Hosting",
		ids = {2}, 
		RGB = {0, 153, 255} B
	},
	
	hosteri = {
		name = "Hoster |",
		ids = {9}, 
		RGB = {204, 0, 102} 
	},
	
	scripteri = {
		name = "Scripter |",
		ids = {46},
		RGB = {204, 0, 102} 
	},
	
	admini = {
		name = "Admin |",
		ids = {},
		RGB = {255, 0, 0} 
	},
}

So I want to loop through every group and if the player is in one of those groups have an outputChatBox like so:

outputChatBox("Fondator|Dev | ..:D&G:.. says hello!",0, 153, 255)

I know how to loop through a table, but not a table withing a table within a table... I tried and it's so confusing and my head hurts, maybe someone smarter than me can do this :P

Link to comment
local groups = {
	fondatori = {
		name = "Fondator|Dev |",
		ids = {1, 3, 54, 85},
		RGB = {0, 153, 255}
	},
	fondatori2 = {
		name = "Fondator|Hosting",
		ids = {2},
		RGB = {0, 153, 255}
	}
}

	
for _,group in pairs(groups) do
	local r,g,b = unpack(group.RGB)
	outputChatBox( group.name )
	outputChatBox( r..", "..g..", "..b )
end

 

Edited by DNL291
Link to comment
6 minutes ago, DNL291 said:

local groups = {
	fondatori = {
		name = "Fondator|Dev |",
		ids = {1, 3, 54, 85},
		RGB = {0, 153, 255}
	},
	fondatori2 = {
		name = "Fondator|Hosting",
		ids = {2},
		RGB = {0, 153, 255}
	}
}

	
for _,group in pairs(groups) do
	local r,g,b = unpack(group.RGB)
	outputChatBox( group.name )
	outputChatBox( r..", "..g..", "..b )
end

 

But how about the IDs of the players from ids = {}?

I need to check if the player has his ID in any of the groups because if the player is not in one of the groups, the outputChatBox should be like this:

for _,group in pairs(groups) do
	local r,g,b = unpack(group.RGB)
  if (playerID == (one of the ids from ids = {}) then
	outputChatBox( group.name.." ..:D%G:.. says: Hello World", r, g, b )
    else
    	outputChatBox("Civilian | ..:D%G:.. says: Hello World", 255, 255, 0 )
    end
end

Thanks.

Edited by ..:D&G:..
Link to comment

Try this:

for _,group in pairs(groups) do
	if isPlayerIDInGroup( playerID, group.ids ) then
		outputChatBox( group.name.." ..:D%G:.. says: Hello World", r, g, b )
	else
		outputChatBox("Civilian | ..:D%G:.. says: Hello World", 255, 255, 0 )
	end
end

function isPlayerIDInGroup( id, IDsTable )
	for i=1, #IDsTable do
		if IDsTable[i] == tonumber(id) then
			return true
		end
	end
	return false
end

 

Link to comment

If the user's ID is in one of the groups, it shows the outputChatBox with the name of that group, but also Civilian...

Life, I have 3 groups ("Founder", "Admin" and "Scripter") and if I put my ID in "Scripter" the chatbox is like this:

Civilian| ..:D&G:.. says: Hello world! -- Founder Group
Civilian| ..:D&G:.. says: Hello world! -- Admin Group
Scripter| ..:D&G:.. says: Hello world! -- Scripter Group (My ID is in this group only)

 

  • Like 1
Link to comment

E:

local groups = {
	fondatori = {
		name = "Fondator|Dev |",
		ids = {1, 3, 54, 85},
		RGB = {0, 153, 255}
	},
	fondatori2 = {
		name = "Fondator|Hosting",
		ids = {2},
		RGB = {0, 153, 255}
	}
}

function isPlayerIDInGroup( id, IDsTable )
	for i=1, #IDsTable do
		if IDsTable[i] == tonumber(id) then
			return true
		end
	end
	return false
end

local groupFound = false
for _,group in pairs(groups) do
	if isPlayerIDInGroup( playerID, group.ids ) then
		groupFound = group
		break
	end
end

if groupFound then
	local r,g,b = unpack(groupFound.RGB)
	outputChatBox(groupFound.name.." ..:D%G:.. says: Hello World", r, g, b )
else
	outputChatBox("Civilian | ..:D%G:.. says: Hello World", 255, 255, 0 )
end

 

Edited by Jayceon
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...