ImageButton is not randomizing within loop

My goal here is to have an ImageButton of a Pass that changes every 10 seconds. When the player clicks on the button, they’re prompted to buy the Pass that’s currently displayed on their screen. I have a folder filled with ImageButtons of all my game’s Passes, and each button has an IntValue containing the Pass ID which the script uses to prompt the Pass to the player.

The script I made randomizes the buttons in the folder and the loop that I have repeats every 10 seconds which is supposed to make the currently visible ImageButton not visible and then make another ImageButton visible. The loop works as expected, but the ImageButton randomly changes once instead of changing every 10 seconds. No matter what I do, I can’t get the ImageButton to keep changing every 10 seconds. It only changes once.

This is what I currently have:

local player = game.Players.LocalPlayer
local marketplaceservice = game:GetService("MarketplaceService")
local buttons = script.Parent.Main.Passes:GetChildren()
local randompass = buttons[math.random(1, #buttons)]

randompass.MouseButton1Click:connect(function()
	marketplaceservice:PromptGamePassPurchase(player, randompass.id.Value)
end)

while true do
	wait(10)
	
	randompass.Visible = false
	randompass.Visible = true
end

What do I need to do to fix this?

randompass.Visible = false
randompass.Visible = true

There is no delay in-between these statements. It changes to false, then back to true again instantly, thus it always appears like the image is visible. You need to move the top statement to be before the delay so there’s actually time to see the ImageButton being invisible.

ah i see, try placing the randoompass variable and its click function inside the loop, since its before the loop it only chooses 1 random, and stays the same while loop happens, so place them in loop

So what your doing wrong over here is setting the random gamepass button once when the script starts. You’ll hace to change it every time you loop

local player = game.Players.LocalPlayer
local marketplaceservice = game:GetService("MarketplaceService")
local buttons = script.Parent.Main.Passes:GetChildren()
local randompass = buttons[math.random(1, #buttons)]

randompass.MouseButton1Click:connect(function()
	marketplaceservice:PromptGamePassPurchase(player, randompass.id.Value)
end)

while true do
      randompass=buttons[math.random(1,#buttons)]
	randompass.Visible = false
        wait(10)
	randompass.Visible = true
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.