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

How to compare intValue to a table?

Asked by 8 years ago

I am working on a GUI that allows a player to select a character. When a character is selected before they are spawned a value is sent to an intValue within the GUI to track which character is currently selected. How would I go about comparing that intValue to the table provided below? Since intValue changes when ever the player clicks the character as a currently selected to bring up profile information, I'm assuming it would need a .changed:Connect

local t =
{
    [1] = function() return game.ReplicatedStorage.C1 end,
    [2] = function() return game.ReplicatedStorage.C2 end,
    [3] = function() return game.ReplicatedStorage.C3 end,
    [4] = function() return game.ReplicatedStorage.C4 end
}
0
Why do you need to compare it? In this case, couldn't you just write 't[intValue.Value]'? BlackJPI 2658 — 8y
0
Hmm... so I would just do something like intValue.Changed:connect... return(t[intValue.Value])? DaRtHSpeeDy 10 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

To check for something in a table, you need to search for it's key, for example:

local table = {
    [1] = true,
    ["entry"] = true
}

print(table[1],table["entry"]) -- would print "true true"

So, your code would be:

local t = {
    -- entries here
}
local val = GUI.IntValue
val.Changed:connect(function()
    t[val.Value]() -- calls the function
end)

Hope I helped!

~TDP

0
Just saw this after commenting on the other post. This helped a lot thanks! DaRtHSpeeDy 10 — 8y
Ad

Answer this question