How to make a table random Object and put it as a Text?

Hello everyone,

This is my first time I have been working with Objects in table. So please notice that!
and this is my first time I have been using table.concat and as you see it didnt work well ;D

So what am I trying?

So actually I want get the Value of the picutre, in that case it is Z, X, but I dont know how to get the Value and put it in the Texbutton Text.

Should I work with table.concat? I dont know

local Frame = script.Parent.Frame

local picture1 = Instance.new("ImageLabel")
picture1.ImageTransparency = 0
picture1.Visible = false
picture1.Image = "http://www.roblox.com/asset/?id=66896959"
picture1.BackgroundTransparency = 0
picture1.Position = UDim2.new(0,0,0,0)
picture1.Name = "Image1"
picture1.Size = UDim2.new(0, 100,0, 100)
picture1.Parent = Frame

local picture2 = Instance.new("ImageLabel")
picture2.ImageTransparency = 0
picture2.Visible = false
picture2.Image = "http://www.roblox.com/asset/?id=16441928"
picture2.BackgroundTransparency = 0
picture2.Position = UDim2.new(0.6,0,0.6,0)
picture2.Name = "Image2"
picture2.Size = UDim2.new(0, 100,0, 100)
picture2.Parent = Frame

local KEYS = {
	[picture1] = "Z",
	[picture2] = "X",
}

local random = KEYS[math.random(#KEYS)]	
local TextButton = script.Parent.Frame.TextButton
TextButton.Text = random
3 Likes

If I fallow my brain it would tell me

local random = KEYS[math.random(#KEYS.Value)]
local TextButton = script.Parent.Frame.TextButton
TextButton.Text = random

but thats absolutely wrong

You cannot use # in this case because your KEYS table is not an array. Instead, you can use this

local count = 0
for _ in pairs(KEYS) do count += 1 end

local random = KEYS[math.random(count)]	

As I said I am new in table, but thanks I will test it out now

You cannot use # in this case because your KEYS table is not an array. Instead, you can use this

local count = 0
for _ in pairs(KEYS) do count += 1 end

local random = KEYS[math.random(count)]	

I got nil

your code should look like this

local Frame = script.Parent.Frame

local picture1 = Instance.new("ImageLabel")
picture1.ImageTransparency = 0
picture1.Visible = false
picture1.Image = "http://www.roblox.com/asset/?id=66896959"
picture1.BackgroundTransparency = 0
picture1.Position = UDim2.new(0,0,0,0)
picture1.Name = "Image1"
picture1.Size = UDim2.new(0, 100,0, 100)
picture1.Parent = Frame

local picture2 = Instance.new("ImageLabel")
picture2.ImageTransparency = 0
picture2.Visible = false
picture2.Image = "http://www.roblox.com/asset/?id=16441928"
picture2.BackgroundTransparency = 0
picture2.Position = UDim2.new(0.6,0,0.6,0)
picture2.Name = "Image2"
picture2.Size = UDim2.new(0, 100,0, 100)
picture2.Parent = Frame

local KEYS = {
	[picture1] = "Z",
	[picture2] = "X",
}

local count = 0
for _ in pairs(KEYS) do count += 1 end

local random = KEYS[math.random(count)]
local TextButton = script.Parent.Frame.TextButton
TextButton.Text = random
1 Like

Players.Iwantbebetter.PlayerGui.ScreenGui.LocalScript:33: invalid argument #3 (string expected, got nil)

1 Like

What you actually try to do is with numbers but I need strings.

1 Like

I need the string to the value

local count = 0
for _ in pairs(KEYS) do count += 1 end

local random = KEYS[math.random(count)]

So as I said how can I get the value??

sorry, I was just thinking about the random part. This code will work

local objects = {}
for k in pairs(KEYS) do
	table.insert(objects, k)
end

local random = KEYS[objects[math.random(#objects)]]
local TextButton = script.Parent.Frame.TextButton
TextButton.Text = random

Do you maybe know how I can visible the Image of the randomized then true?

after I randomized? then the randomized will get true

yes this question is too hard I need to post a new one.

If I understand correctly, what you want is this:

local objects = {}
for k in pairs(KEYS) do
	table.insert(objects, k)
end

local randomPicture = objects[math.random(#objects)]
local random = KEYS[randomPicture]
local TextButton = script.Parent.Frame.TextButton
TextButton.Text = random

randomPicture.Visible = true
1 Like

nono I will delete my post your solution is true