Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I make a script like this work?

Asked by 9 years ago

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)

4 answers

Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
9 years ago

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

0
so the numbers you have at the top (9999999, etc.) are the ID's? And this is the whole script? User#4422 0 — 9y
0
Yup. The actual Id's can be found at the top of the page where the image is. They should be just a bunch of numbers at the end of the url. Replace the ones I put with those. dyler3 1510 — 9y
0
In the same script, I want it to also change the image of Card2 and Card3, would I just copy and paste the same code and tweek it a little, while all still in the same script? User#4422 0 — 9y
0
No, that is the decal not the image. EzraNehemiah_TF2 3552 — 9y
View all comments (9 more)
0
You could if you wanted to, yea. It'd work if you did it correctly. dyler3 1510 — 9y
0
Thanks! I'm going to test it :P User#4422 0 — 9y
0
No prob. Hope I helped. If you run into any problems, let me know dyler3 1510 — 9y
0
There is a problem, Card2 and Card3 are working fine, but Card1 isn't showing up. In an answer, I'll post the script I have. User#4422 0 — 9y
0
Exactly, That is the problem with your script EzraNehemiah_TF2 3552 — 9y
0
Dude... dyler3 1510 — 9y
0
Not working :/ User#4422 0 — 9y
0
The frame with the images won't even open now, I can click the button, but nothing happens. User#4422 0 — 9y
0
Message me on Roblox dyler3 1510 — 9y
Ad
Log in to vote
2
Answered by
Unclear 1776 Moderation Voter
9 years ago

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)]
0
Is that the whole script? User#4422 0 — 9y
0
This is an example of how you would get a random image link. I will not provide the whole script because we have a "no requests" policy. :P Unclear 1776 — 9y
Log in to vote
2
Answered by 9 years ago

We need a table of image ids and use math.random()


Tables

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.

math.random()

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".



Final Product

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!

Log in to vote
0
Answered by 9 years ago
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)
0
Hold on. I'll post an updated version of my answer with this in it. dyler3 1510 — 9y
0
Thank you so much. My game looks great because of you! User#4422 0 — 9y
0
No prob dude. Glad I could help :P dyler3 1510 — 9y

Answer this question