How to make a script do something random?

Hello. I want to make it so that when this thing is triggered, one out of 4 frames i have is made visible instead of the same one every time. how can i do this?
image

3 Likes

can i see your explorer?

charzzzzzzzzzzzzzzzzzz

1 Like
local frames = script.Parent:GetChildren()

local randomFrame = frames[math.random(1, #frames)]

randomFrame.Visible = true
2 Likes

thats great, but how does it know the randomFrame is not a script?

1 Like

i would use this probably, a bit more unnecessarily complicated but whatever

gui.Triggered:Connect(function()
	ok  = false
	selected = script.Parent:GetChildren()[math.random(1,#script.Parent:GetChildren())]
	while not ok do
		if selected.ClassName == "Frame" then
			ok = true
		else
			selected = script.Parent:GetChildren()[math.random(1,#script.Parent:GetChildren())]
		end
		wait()
	end
	selected.Visible = true
end)
1 Like

image

basically, instead of making one of these visible when the thing is triggered, i would want to make “Pick1” and “see1” visible at the same time or “pick2” and “see2” visible at the same time. but like randomly if that makes sense.

1 Like

where is the script in the explorer?

1 Like

image
inside of starter gui, i just have it so when a proxy promt is triggered this happens
image

2 Likes

thinking that the “frame” in this explorer is the frame thats seen from image above, this should work.

gui.Triggered:Connect(function()
	local tabel = {"See1", "See2", "Pick1", "Pick2"}
	local number1 = tabel[math.random(1, #tabel)]
	table.remove(tabel, table.find(tabel, number1))
	local number2 = tabel[math.random(1, #tabel)]
	script.Parent.Frame[number1].Visible = true
	script.Parent.Frame[number2].Visible = true
end)
2 Likes

If you’re looking for a fully automatic solution, you can instead have something like this:

local Children = gui:GetChildren() -- replace gui with whatever UI you want
local ChildrenCount = #Children

gui.Triggered:Connect(function()
    local RandomFrame = Children[math.random(1, ChildrenCount)]

    if RandomFrame then
        print(`Selected {RandomFrame.Name} as the random frame`)
    end
end)

This way, it’ll get the gui’s children, randomly pick one of its children and then you can do whatever you want with it. If you’re looking for a specific component type (e.g. only frames), you can simply iterate through the gui’s children & create a new table of the frames and then use that as your Children table

1 Like

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