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

How to check if all the values in the table are a same?

Alright, I have a table, and I want to see if all the values are the same. If all the values are the same, I want it to output the value, and if they aren't, I want nothing to happen.

If this helps. It's a territory control script. If all the IDs in the script are the same, it goes to that group. If they're not all the same, nothing happens.

while true do
	local factions = {} --Create an empty table to store the factions
	
	--Add the values to the table
	for i, child in pairs(script:GetChildren()) do
    	factions[i] = child.Value
	end
	
	--Do whatever I gotta do to check if they're all the same.	
	
	wait(0.5) --Update twice a second
end

1 answer

✓ Accepted answer

Here is what you would do:

while true do
    local factions = {} --Create an empty table to store the factions

    --Add the values to the table
    for i, child in pairs(script:GetChildren()) do
        factions[i] = child.Value
    end

	if #factions > 0 then
		local check = factions[1]
		--USE TAB TO INDENT COME ON NOW MY CODE IS GONNA BE MISALIGNED
		local success = true
		for _, compare in pairs(factions) do
			if compare ~= check then
				success = false
				break
			end
		end
		--do something with the success value.
	end
    wait(0.5) --Update twice a second
end

So what this does, is grab the first value of the table, and compare if the rest of the table is equivalent to the first value of the table. Also, READ MY BIO. Hope this helps!

5 comments

0

Lol, every once in a while I see this guy saying, "Read my bio." It's so funnyXD

0

I'll test this in the morning. And, I follow everything in your bio! ;)

0

I am pretty sure that you can access a UI using a server script... @hiimgoodpack

2

You cannot with FE.

0

You are able to if the script exists as a descendant of the GUI element, though it should be avoided, it's still possible.

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