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

Tables values won't set to true or false?

Asked by 7 years ago

My last post got flooded away... and there is no way to bump a post.. don't flip your lids I need help.

So I made a gui with 'check boxes' and when you click one 'box' it should change the corisponding values to true or false, I'll include a model so you can see it for yourself. I don't get any errors, it just won't work. https://www.roblox.com/Thanks-ScriptingHelpers-item?id=456562045 ^^ Model thanks

local faces = script.Parent.Face
local FacesTable = {
    Back = false,
    Bottom = false,
    Front = false,
    Left = false,
    Right = false,
}
for index, child in pairs(faces:GetChildren()) do
    child.Display.MouseButton1Click:connect(function()
        CheckVars()
        local var = nil
        local vartab
        for i, v in pairs (FacesTable) do
            if i == child.Name then
                print(i .. " = " .. tostring(v))
                print("Value found!")
                var = v
                vartab = i
        if v == false then
            print("var was false")
            v = true
            print(vartab .. " = " .. tostring(v))
            child.Display.Style = 1
            return
            elseif v == true then
            print("var was true")
                    v = false
                    print(vartab .. " = " .. tostring(v))
                    return
                end
            end
            child.Display.Style = 2
            return
            end
        end)
end
function CheckVars()
    for i, v in pairs (FacesTable) do
        print(tostring(i) .. " = " .. tostring(v))
    end
end
0
Could you describe your hierarchy? And is nothing working? Does the CheckVars() at the top work, etc.? (Where does it stop working?) honeygold13843 136 — 7y
0
@honeygold13843, No errors, it just won't set true or false when I click the buttons, look at the model I gave. xXharvest109Xx 25 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Okay, there are two major errors with your script that I can find. I'll go through them.

Return statement in the for pairs loop

On line 34, you are ending the event handler (that you created on line 10) after the first iteration of the for pairs loop (where you're trying to loop through the FacesTable). If the first child is not the one your clicked on, it will not do anything, since the loop has ended.

I'm not sure why that is there at all, so I just removed it.

Checking a dictionary

This is not actually an error, but if you looking for a specific index in a dictionary (a table with string keys, like 'FacesTable'), there's no need to loop through the table.

if FacesTable[child.Name] then -- Checks if the 'child.Name' index of FacesTable has a value.
    -- code
end

FacesTable[child.Name] = true -- Sets the 'child.Name' index of FacesTable to 'true'.

Setting table values

This is the other major error.

When looping through a table with the pairs function, you do not set a new value by changing 'v'. 'v' is local to the loop only, and does not index the table (it is only the key value). If you want to change a table value, you have to index it with something like FacesTable[i] = true.

0
Thank you so much! <3 xXharvest109Xx 25 — 7y
Ad

Answer this question