Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Relating to Indexed Values? [UNSOLVED]

This is what I have so far:

local Model = script.Parent
local Settings = script.Parent.Configuration

--index defined administrators
wait()
for index, child in pairs(Settings.AdminList:GetChildren()) do
    print(index, child.Value)
--index current players
for index, child in pairs(game.Players:GetChildren()) do
    print(index, child.userId)
end
end

I need to be able to "compare" to see if the indexed children of current players have any matching userIds within the AdminList and perform an action if there is a match. How would I do this?

2 answers

I think I know what you are saying so here is a shot at what it sounds like.

--For an admin list, I prefer to do this

adminList={
	["Name"]=123456,
	["Name2"]=654321
}

function checkAdmin(p)
	for i,v in pairs(adminList) do
		if p.userId==v then
			return true
		end
	end
end

--To run
game.Players.PlayerAdded:connect(function(user) --Not needed
	if checkAdmin(user) then
		print("It is an admin!")
	end
end)

Also I don't think you can redo the index,value without ending the first one.

EDIT What type of Value are you using to hold userIds? StringValue would be the best.

2 comments

0

I understand that method, however it doesn't achieve the flexibility that it can it it can check if any value of the admin NumberValue matches the userId of a player within the server. Meant for n00bs to use without even opening the script. Those are my parameters...

0

Ahh, well I wouldn't know how to go through a NumberValue.

To see if there are any matches between two tables..

I have two suggestions for you.

Either, make a function, inside the function make a separate table which will consist of the potentially matching values. Iterate through both tables and if there's any matches then use table.insert to insert the matches into the third party table. Then return the table.

function checkMatches(tab1,tab2)
	local matches = {}
	for i,v in pairs(tab1) do
		for a,b in pairs(tab2) do
			if b == v then
				table.insert(matches,b)
			end
		end
	end
	return matches;
end

local a  = {"Hello", "Hi", 5,8,2}
local b = {8,"Derp",false,{1,2,3,4},6}

local matches = checkMatches(a,b)
print(table.concat(matches,", "))
--8

Or you could just have both table values, and use table.concat, with tostring, to check if there are any matches between them.

local a  = {"Hello", "Hi", 5,8,2}
local b = {8,"Derp",false,{1,2,3,4},6}

if table.concat(a," "):match(table.concat(b)," ") then
	print("There's a match!")
end
--There's a match!

This discussion is preserved as it was. New answers, comments, and votes are closed.