When I click on a text button, I want it to randomize the image that will appear on an image label and give it a new image when the page with the randomized image on it is closed. Is that possible to script?
function onButtonClick() script.Parent.Parent.Parent.Parent.NewItemsFrame.Card1.Image = random("..., ..., ...") -- "..." represents links end script.Parent.MouseButton1Click:connect(onButtonClick)
Yea, it is definitely possible to do this. First, you would need to create a table. To do this, we simply need to define one, then enter in the id's of our Images. Something like this would work:
Table={"9999999","11111111","0000001"} --Obviously just examples
So now that we have the table assigned, we need to randomly select from the table. To do this, we'll use , math.random to pick a number to choose from the table:
Picked=Table[math.random(1,#Table)] --Picks one of the id's at random
Now that we have these both set up, we can go ahead and enter them into the script. So the finished product should look something like this:
Table={"9999999","11111111","0000001"} --Still just examples function onButtonClick() Picked=Table[math.random(1,#Table)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card1.Image ="rbxassetid://"..Picked end script.Parent.MouseButton1Click:connect(onButtonClick)
Now the code should work! Anyways, hope I helped you out, and as always, if you still have a problem or question, feel free to leave a comment below :P
UPDATE
This should work now:
Table1={"261746216","261746507","261746654","261747019"} function onButtonClick1() local Picked=Table1[math.random(1,#Table1)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card1.Image ="rbxassetid://"..Picked script.Parent.Parent.Parent.Parent.NewItemsFrame.Visible = true script.Parent.Parent.Parent.Visible = false end script.Parent.MouseButton1Click:connect(onButtonClick1) Table2={"261752121","261751827","261751592","261751281","261751020","261750728","261750417","261750092","261749867","261749620","261749411","261749156","261748731","261748439","261748193","261747943","261747788","261747543","261747333","261747185"} function onButtonClick2() local Picked=Table2[math.random(1,#Table2)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card2.Image ="rbxassetid://"..Picked script.Parent.Parent.Parent.Parent.NewItemsFrame.Visible = true script.Parent.Parent.Parent.Visible = false end script.Parent.MouseButton1Click:connect(onButtonClick2) Table3={"261752121","261751827","261751592","261751281","261751020","261750728","261750417","261750092","261749867","261749620","261749411","261749156","261748731","261748439","261748193","261747943","261747788","261747543","261747333","261747185"} function onButtonClick3() local Picked=Table3[math.random(1,#Table3)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card3.Image ="rbxassetid://"..Picked script.Parent.Parent.Parent.Parent.NewItemsFrame.Visible = true script.Parent.Parent.Parent.Visible = false end script.Parent.MouseButton1Click:connect(onButtonClick3)
It should work now. I just made sure that the variables were all assigned different names so that the script didn't mix them all up. If there's any more problems, leave another comment and i'll see what I can do :P
You're on the right track. What we'll do is use a lookup table full of image links, and then pick a random integer between 1 and the number of image links we have! That will be the index for our random image.
Here's an example...
local images = { "imageLink", "imageLink", "imageLink" } randomImageLink = images[math.random(#images)]
We need a table of image ids and use math.random()
can hold multiple material such as, strings, floats, obj, etc.
local table = {1,"roblox",workspace.BasePlate, BrickColor.new("Bright red"), Vector3.new(0,10,0)} --Tables can hold multiple variables.
returns a random number from 0,1. For example:
print(math.random()) --0.74926454267
You can add a numbers into the parameters so that we can set where the number will be between number a and number b:
math.random(a,b) math.random(1,10) --pick a number 1-10
If you add one number it assumes you're saying "Pick a number 1-number b".
pics = {"ID GOES HERE"} script.Parent.MouseButton1Down:connect(function() script.Parent.Parent.Parent.Parent.NewItemsFrame.Card1.Image = "rbxasset://"..pics[math.random(#pics)] --The # operator sees how many things are inside the table. end)
If you copy the ID from the webpage of the decal, get that id and subtract one. If you get it from a decal from a decal object then don't subtract one.
Hope it helps!
Table={"261746216","261746507","261746654","261747019"} function onButtonClick() Picked=Table[math.random(1,#Table)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card1.Image ="rbxassetid://"..Picked script.Parent.Parent.Parent.Parent.NewItemsFrame.Visible = true script.Parent.Parent.Parent.Visible = false end script.Parent.MouseButton1Click:connect(onButtonClick) Table={"261752121","261751827","261751592","261751281","261751020","261750728","261750417","261750092","261749867","261749620","261749411","261749156","261748731","261748439","261748193","261747943","261747788","261747543","261747333","261747185"} function onButtonClick() Picked=Table[math.random(1,#Table)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card2.Image ="rbxassetid://"..Picked script.Parent.Parent.Parent.Parent.NewItemsFrame.Visible = true script.Parent.Parent.Parent.Visible = false end script.Parent.MouseButton1Click:connect(onButtonClick) Table={"261752121","261751827","261751592","261751281","261751020","261750728","261750417","261750092","261749867","261749620","261749411","261749156","261748731","261748439","261748193","261747943","261747788","261747543","261747333","261747185"} function onButtonClick() Picked=Table[math.random(1,#Table)] script.Parent.Parent.Parent.Parent.NewItemsFrame.Card2.Image ="rbxassetid://"..Picked script.Parent.Parent.Parent.Parent.NewItemsFrame.Visible = true script.Parent.Parent.Parent.Visible = false end script.Parent.MouseButton1Click:connect(onButtonClick)