Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Trying to compare items in a table, can't seem to get it right. Any help?

Asked by
Psudar 882 Moderation Voter
5 years ago
Edited 5 years ago

I've been working on my own GUI Module so that I can edit GUI a bit faster in-game. I've made this function inside a module script to change the color of the player's interface. Here it is:

01--//Module
02function GuiModule.ChangeColor(guiTable, ignoreTable, guiColor)
03    local defaultColor = Color3.fromRGB(255, 255, 255)
04    for i, v in pairs (guiTable) do
05        if v:IsA("GuiObject") then
06            if v ~= ignoreTable[i] then --if v (current object) is not in the ignore table then:
07                v.BackgroundColor3 = guiColor or defaultColor
08                v.BorderColor3 = guiColor or defaultColor
09                if v:IsA("ImageButton") or v:IsA("ImageLabel") then
10                    v.ImageColor3 = guiColor or defaultColor
11                end
12            end
13        end
14    end
15end
View all 30 lines...

The basic idea is to have a table of GUI I do want to change, a table that I dont want to ignore, and a color to change them to.

However, when I compare the iteration and value to the ignore table, it still changes the colors of the ones I dont want to change. Its like 5am and Im not too sure what im doing wrong, or if im just really tired. Heres a gif of whats going on; and a gif of whats supposed to happen.

Whats happening:

https://gyazo.com/a91067c7fcd42f974d034806bccbb187

Whats supposed to happen (I removed the :IsA("ImageButton") check:

https://gyazo.com/dd3dbd06ac26c7a7fc7d522653d2c3c4

The reason I have the image button check in the first place, is because this is modularized, its going to be able to work in pretty much every situation. The only items I want to ignore are the ones in the ignore table, the only items I want to change are the ones in the guiTable, you get the idea.

Thanks for any help.

1 answer

Log in to vote
4
Answered by 5 years ago

Nowaha was partially right except his solution was written wrong

1function contains(v, ignoreTable)
2    for index, value in pairs(ignoreTable) do
3        if v == value then
4            return true
5        end
6    end
7    return false
8end

This is basically his function, but he used it wrong in the examples

I tested this in studio soooo idk what will happen if it goes wrong :(

01function GuiModule.ChangeColor(guiTable, ignoreTable, guiColor)
02    local defaultColor = Color3.fromRGB(255, 255, 255)
03    for i, v in pairs(guiTable) do
04        if v:IsA("GuiObject") and not contains(v, ignoreTable) then -- literally just added the "not" compared to his answer.
05            v.BackgroundColor3 = guiColor or defaultColor
06            v.BorderColor3 = guiColor or defaultColor
07            if v:IsA("ImageButton") or v:IsA("ImageLabel") then
08                v.ImageColor3 = guiColor or defaultColor
09            end
10        end
11    end
12end

And... what I tested it with!!!!

01local gui = require(game.ReplicatedStorage.Modules.Gui)
02local interfaceTable = script.Parent.Parent:GetDescendants()
03local colorButtons = script.Parent:GetChildren()
04 
05--[[Hierarchy = {
06    ScreenGui
07        -Frame
08            -LocalScript
09            -ImageButton "a"
10            -ImageButton "b"
11}]]
12 
13for _, button in pairs(colorButtons) do
14    if button:IsA("ImageButton") then
15        button.MouseButton1Down:Connect(function()
16            gui.ChangeColor(interfaceTable, colorButtons, button.ImageColor3)
17        end)
18    end
19end

glhf no downvotes :(

1
apples :) bhqpping 80 — 5y
1
Thanks haha. I added the not in studio before I even looked here, I realized thats why his wasnt working either. GG. Psudar 882 — 5y
0
Yep, good eye Nowaha 459 — 5y
Ad

Answer this question