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

Displaying Random Images

Asked by
Nytroz 15
10 years ago

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

Log in to vote
1
Answered by
jobro13 980 Moderation Voter
10 years ago

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)

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? Nytroz 15 — 10y
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! jobro13 980 — 10y
0
Everything has been defined, and the size of each image is 200, 280. Where do I put that data in the code? Nytroz 15 — 10y
0
new.Size = UDim2.new(0,200,0,280) jobro13 980 — 10y
View all comments (21 more)
0
And where would I put this? In the frame I want to make visible or in the button? Nytroz 15 — 10y
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. jobro13 980 — 10y
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 Nytroz 15 — 10y
0
PremiumGP is also the button that activates the code Nytroz 15 — 10y
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 jobro13 980 — 10y
0
http://ideone.com/efhAcf So that will work now? Nytroz 15 — 10y
0
Yes, I think it should work. Go ahead and test it! :) jobro13 980 — 10y
0
I didn't work, the imagebutton is unresponsive. Nytroz 15 — 10y
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? Nytroz 15 — 10y
0
It is in the code, that's where the "new" things go. Is PremiumGP an ImageButton? jobro13 980 — 10y
0
PremiumGP is the ImageButton but in http://prntscr.com/2tfwfo PremPackOpen is where the images should be displayed Nytroz 15 — 10y
0
Does the output throw in any error? jobro13 980 — 10y
0
I can't run the game in the output, it will just crash studio. Nytroz 15 — 10y
0
You can't check the output..!? (View --> output ?) jobro13 980 — 10y
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... Nytroz 15 — 10y
0
Wait, I fixed it! I missed out the 'S' in GPack(S)UI which caused the Output to throw errors. Nytroz 15 — 10y
0
But it is still staying I am trying to index global (nil) value Nytroz 15 — 10y
0
Wait, I was looking at the wrong scripts. Nytroz 15 — 10y
0
:O It works now! Thanks a million mate! Nytroz 15 — 10y
0
No problem at all! I hope you learned something from this! :) jobro13 980 — 10y
0
Yep! I learned how to call data from a table and how to call certain functions. Nytroz 15 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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
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. Nytroz 15 — 10y
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. User#11893 186 — 10y

Answer this question