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

How to compare intValue to a table?

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
}

2 comments

0

Why do you need to compare it? In this case, couldn't you just write 't[intValue.Value]'?

0

Hmm... so I would just do something like intValue.Changed:connect... return(t[intValue.Value])?

1 answer

✓ Accepted answer

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

1 comment

0

Just saw this after commenting on the other post. This helped a lot thanks!

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