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

Why does my Key and Value pair print differently in certain contexts?

function DrawCard()
	local Random = math.random(1,7)
	-- Create Switch Case
	local Factions =
	{
		[1] = 'Wisdom',
		[2] = 'Bounty',
		[3] = 'Might',
		[4] = 'Culture',
		[5] = 'Balance',
		[6] = 'Marauders',
		[7] = 'Privateer'
	}
	print(Factions[Random])
	script.Parent.Factions[Random].Value = -1
end

Say that Random = 1

print(Factions[Random])
-- Prints Wisdom to output

Then why does

script.Parent.Factions[Random].Value = -1
-- Throw an exception of "1 is not a valid member of script?

I know I can reverse the table to do this, however I am trying to find a much more efficient way and I think there is a much simpler way to do this if I don't reverse it.

4 comments

0

The word Random is a keyword in roblox. Consider using a different variable name. I am not saying this will fix your problem.

0

I am just saying that using a keyword as a variable is not encouraged.

0

@Plegethon5778 This is wrong. Random is not a keyword, it is only a global variable. A table, to be specific, that holds functions to create Random objects. It's perfectly fine to name a local variable after an existing variable if you do not intend to use it for the duration of the local variable's scope.

2 answers

✓ Accepted answer

Random in your example is a number. You are trying to access your Factions object using this number in square brackets. You need to access your faction by its name, not using a number.

local factions = {
	'Wisdom',
	'Bounty',
	'Might',
	'Culture',
	'Balance',
	'Marauders',
	'Privateer'
}

-- we generate a random number ranging from [1, #factions], where
-- `#factions` is the length of the `factions` table.
local rand = math.random(#factions)
local chosen = factions[rand]	-- we then get the name out of the table
								-- from the random position

-- finally, we get the faction object itself using this name
local faction = script.Parent.Factions:FindFirstChild(chosen)

-- ensure `faction` exists
if faction then
	-- it exists; we can continue on
else
	-- it does not exist :(
end

As a side note, I'd like to encourage you to use different variable names for your Factions object and your factions table, to make your code less ambiguous and easier to read.

3 comments

0

Followup question: Is there a way to use my method(index with value) with it working?

0

I don't understand. The reason your last line of code doesn't work is to retrieve objects from the explorer you need to access them by name.

0

My solution instead uses the random number as a position in a list of names and then uses that name to access the corresponding object. You nearly had all of that down yourself, your only mistake was to reuse the number instead of using the name.

I am going to assume here that your structure is organized in your workspace like so:

Parent
	ThisScript
	Wisdom
	Bounty
	Might
	Culture
	Balance
	Marauders
	Privateer

So you seem to want to access these IntValue objects (I assume they are), depending on what card is drawn. To do this, you should use FindFirstChild rather than indexing using .. This is true for any time you want to index a varying property or child.

You should change the line to:

local chosenFaction = script.Parent:FindFirstChild(Factions[Random])
if chosenFaction then
	chosenFaction.Value = -1
end

Hope this helps! :)

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