The title speaks for itself, how would I print a value from the table ( 1 to be exact ) that is random?
You can simply generate a random number that’s between 1 and your table length. You can get the tables length by doing #TABLEHERE
. Then, to return the random value simply do TABLEHERE[randomVal]. It should look like this:
local num = math.random(1, #TABLEHERE)
print(TABLEHERE[num])
Generate a random number between 1 and the number of occupied indices in the table (the amount of values in the table) and then index the table with that random number:
local value = tbl[math.random(#tbl)]
local table = ["value 1", "random"]
print(table[math.random(0, #table)])
confusion, how does num’s parent automatically == TABLEHERE
Is their a way I can do this without having to make a new value/variable?
Table[int] returns the value correspondent to that index.
Why did I get this error?
13:19:32.982 Players.NubblyFry.PlayerGui.ScreenGui.TextButton.LocalScript:14: invalid argument #2 to 'random' (interval is empty) - Client - LocalScript:14
13:19:32.983 Stack Begin - Studio
13:19:32.984 Script 'Players.NubblyFry.PlayerGui.ScreenGui.TextButton.LocalScript', Line 14 - Studio - LocalScript:14
13:19:32.984 Stack End - Studio
local part = game.Workspace:WaitForChild("Part")
local colorTable = {
Red = Color3.new(1,0,0);
Orange = Color3.new(1, 0.666667, 0);
Yellow = Color3.new(1,2,0);
Green = Color3.new(0.682353, 1, 0.435294);
Blue = Color3.new(0.458824, 0.556863, 1);
Purple = Color3.new(0.635294, 0, 1);
Pink = Color3.new(1, 0.333333, 0.498039)
}
script.Parent.MouseButton1Click:Connect(function()
part.Color = Color3.new(colorTable[math.random(1,#colorTable)])
end)
This happens because the table is a dictionary, not an array.
Just do:
local part = game.Workspace:WaitForChild("Part")
local colorTable = {
Color3.new(1,0,0);
Color3.new(1, 0.666667, 0);
Color3.new(1,2,0);
Color3.new(0.682353, 1, 0.435294);
Color3.new(0.458824, 0.556863, 1);
Color3.new(0.635294, 0, 1);
Color3.new(1, 0.333333, 0.498039)
}
script.Parent.MouseButton1Click:Connect(function()
part.Color = colorTable[math.random(1,#colorTable)]
end)
is their a nicer way for me to layout tables then? I didn’t know that was a dictionary.
@greipster Lua array indices start at 1, in contrast with many other languages
@StarJ3M In response to your reply to my post, you don’t necessarily need to make it a variable, you can just print it:
print( tbl[math.random(#tbl)] )
In response to the issue you had with the error, the length operator (#) does not account for values that are non-numerically indexed (values that do not have an index that is a number), so to solve this, the table would need to be an array, like @Sarchyx mentioned, which inherently have all of the values numerically indexed
when people refer to arrays or matrices they mean the key/index is a number
so if you know that information, you can tell if it is a dictionary or not
local table1={1,2,3}
print(math.random[0,#table1])
Output will be random 1,2, or 3
13:31:03.998 Players.NubblyFry.PlayerGui.ScreenGui.TextButton.LocalScript:6: invalid argument #2 to 'random' (interval is empty) - Client - LocalScript:6
13:31:03.999 Stack Begin - Studio
13:31:04.000 Script 'Players.NubblyFry.PlayerGui.ScreenGui.TextButton.LocalScript', Line 6 - Studio - LocalScript:6
13:31:04.001 Stack End - Studio
local part = game.Workspace:WaitForChild("Part")
local colorTable = {Red = Color3.new(1,0,0), Orange = Color3.new(1, 0.666667, 0), Yellow = Color3.new(1,2,0), Green = Color3.new(0.682353, 1, 0.435294), Blue = Color3.new(0.458824, 0.556863, 1), Purple = Color3.new(0.635294, 0, 1), Pink = Color3.new(1, 0.333333, 0.498039)}
script.Parent.MouseButton1Click:Connect(function()
part.Color = colorTable[math.random(1,#colorTable)]
end)
why did you put a 0 there
this is lua lmao
Dictionaries are tables that have “names” with a value, to use index numbers you will need to remove those “names” and leave the values so it can be an array as I told at post #8.
Yeah I was typing on touch screen. It is supposed to be a 1