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

Displaying Random Images

So, when the user clicks, it makes another frame visible making itself invisible while at the same time display 4 random images. I have 1 image so far (I will be making the others later): images/http://www.roblox.com/asset/?id=146289136 I have got as far as this:

local PackPlayers = {"Nytroz", "Legendowski", "HypervenomPhantomz"}
numOfPlayers = 4

numOfPlayers meaning the maximum amount of images to display. Am I going about this correctly? And how would I continue? My previous code was this:

local PackPlayers = {"Nytroz", "Legendowski", "HypervenomPhantomz"}
numOfPlayers = 4


for i=1,numOfPlayers do
   PackPlayers[i] = display.newImageRect("images/http://www.roblox.com/asset/?id=146289136", 200, 280) 
   PackPlayers[i].x = math.random(0, display.contentWidth) 
   PackPlayers[i].y = math.random(0, display.contentHeight) 
end

I found it on the internet and tried to edit the syntax to work with Roblox LUA. But of course it didn't work. So I am asking you guys here to assist me.

2 answers

✓ Accepted answer

For starters, you are trying to use members of display. If this is all your code, display is nil, meaning it will error.

I also don't understand your code. Why are you overwriting PackPlayers?

If you just want to show random images, why would you need ImageRects? Why not show the Images themselves?

I would do this:

local PossibleImages = {146289136}
local MaxImages = 4

function GetImage()
return "http://www.roblox.com/asset/?id="..PossibleImages[math.random(1, #PossibleImages)]
end

function CreateImage(img, x)
local new = Instance.new("ImageLabel")
new.Image = img
new.Position = UDim2.new(0.2 * x, 0, 0.5, 0)
new.Size = UDim2.new(0.1, 0, 0.1, 0)
new.Parent = game.Players.LocalPlayer.PlayerGui.Gui -- somewhere in PlayerGui
end

button.MouseButton1Down:connect(function()
button.Visible = false
for i = 1, MaxImages do
local img = GetImage()
CreateImage(img, i)
end
end)

25 comments

0

ImagesRect was part of Syntax from a different type Lua and thanks for your help. I appreciate it alot. And I can also add Id's to the first table?

0

Yes, you can, but the current code will not work. You will have to define in which GUI it should go (line 13) and what button is (line 16). I know what ImageRects are, but you don't need them for this. My point was that you were trying to use functions from a "display" table, which is nil in your current code!

0

Everything has been defined, and the size of each image is 200, 280. Where do I put that data in the code?

0

new.Size = UDim2.new(0,200,0,280)

0

And where would I put this? In the frame I want to make visible or in the button?

0

I would put the script into the ScreenGui itself, like this you don't get any strange issues on destroying images for example. Make sure that you update the references to the Gui and button.

0

http://ideone.com/0Mdtim, http://prntscr.com/2tfwfo PremPackOpen is where the 4 images should be displayed and where it says Script is the code you posted earlier

0

PremiumGP is also the button that activates the code

0

First of all: use a local script. Scripts won't work (yes, on local test mode, they will). new.Parent = script.Parent.Holder.PremPackOpen; put somewhere on top of the code button = script.Parent.Holder.GPackUI.PremiumGP

0

http://ideone.com/efhAcf So that will work now?

0

Yes, I think it should work. Go ahead and test it! :)

0

I didn't work, the imagebutton is unresponsive.

0

Oh wait, I know why. The frame where the images should be displayed in isn't in the code. Any way I could fix that?

0

It is in the code, that's where the "new" things go. Is PremiumGP an ImageButton?

0

PremiumGP is the ImageButton but in http://prntscr.com/2tfwfo PremPackOpen is where the images should be displayed

0

Does the output throw in any error?

0

I can't run the game in the output, it will just crash studio.

0

You can't check the output..!? (View --> output ?)

0

I can view it but the only things that show are: 22:10:34.386 - Http initialization M1 = 0x1061b90 M2=0x1061b94 22:10:49.586 - DataModel Loading http://www.roblox.com/asset/?id=146262208 logging probability 0.90560625019074 not logging 22:15:36.108 - Auto-Saving...

0

Wait, I fixed it! I missed out the 'S' in GPack(S)UI which caused the Output to throw errors.

0

But it is still staying I am trying to index global (nil) value

0

Wait, I was looking at the wrong scripts.

0

:O It works now! Thanks a million mate!

0

No problem at all! I hope you learned something from this! :)

0

Yep! I learned how to call data from a table and how to call certain functions.

Well, uh, display.newImageRect isn't even part of the ROBLOX API.

What I think you want to do is make a image on the screen. In this case, you'll want to use a ImageLabel.

Also, you're going to need to use a TextButton or ImageButton to detect those clicks.

To create an ImageLabel, just follow the standard procedure when creating objects in the ROBLOX API:

local image = Instance.new("ImageLabel")
image.Parent = script.Parent
image.Size = UDim2.new(0, 300, 0, 300)
image.Image = "rbxassetid://1234"
image.Position = UDim2.new(0, 300, 0, 400)
-- and so on

2 comments

0

Great! I'll combine the two but then. how would I randomize the images? As there will more images I'd like to add.

0

Use the math.random function to return a number between the first parameter and the second. Then use that index to grab the image ID from a table.

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