Looping through a list and choosing a random option?

Hello, fellow developers. I am making a game in which you open a crate. I need to make a script that loops through the options and picks a random one.

It is kind of like a spinner like in Murder Mystery 2, except it only shows the text looping through every option, not anything else. For example: if it looped through a knife, a gun, and a crowbar, the text would say, “Knife”, then “Gun”, then “Crowbar”, and would eventually stop at a random option.

Also, how would I make it work faster at the beginning and slow down until it reaches the end? I have tried taking apart an actual spinner but failed.

I am not looking for a full script, just how I could do something like this, because I have no idea. Any help?

Thanks,
SpeakerDev

What method are you currently using to move your spinner? Manually moving each UI element (Position = UDim2.new(0, 0, 0, 0)), TweenService or another alternative?

As mentioned, I am not looking to create an actual spinner, just a TextLabel that acts like a spinner without moving. Sorry for any misunderstanding!

To accomplish this you can do something like this to show it visually. This is assuming you handle the actual chosen item on the server.

local possible = {
   'Knife',
   'Gun',
   'Crowbar'
}

local textLabel = script.Parent
local chosen = 'Gun' -- this is where youd put what the server chose

for i = 1, 10 do
   wait(1.1-i/10) -- makes it go faster each loop
   textLabel.Text = possible[math.random(#possible)]
end
textLabel.Text = chosen
 -- do whatever else you want to do after showing the player what they got

Have a table with a list of all the possible things that can be picked
Ex:

local possibleChoices = {"Gun", "Knife", ...}

Pick one before they even spin it

local chosenObject = possibleChoices[math.random(1, #possibleChoices)]

Make loop for changing the text.

local timeToTake = 5 --in seconds
local speed = 3
local startTime = os.clock()
local current = ""
while true do
    for i = 1, #possibleChoices do
        task.wait(speed / 60)
        current = possibleChoices[i]
        textLabel.Text = current
        speed /= .1
    end
    if os.clock() - startTime > timeToTake then
        break
    end
end
if not (current == chosenObject then
    task.wait(speed / 60)
    textLabel.Text = chosenObject
end

You can change the speed /= .1 to adjust the change in speed. (What this does is it makes it start off faster and end slower)

1 Like

This works perfectly except sometimes it stops way early. If I have the time set to 10 seconds, sometimes it stops after 1 second.

--:// Constants
local Items = {'Knife', 'Gun', 'Axe', 'Lightsaber', 'Parachute', 'Apple'}
local Spins = 3 -- Set this to how many times you want it to go through the table fully before it finally reaches the object.
local InterfaceButton = path.to.object -- Set this to be the path to your object, e.g. script.Parent.TextButton
local InterfaceDisplay = path.to.object -- Set this to be the path to your object, e.g. script.Parent.TextLabel

--:// Variables
local SelectedItem, Speed, Spinning = nil, 1, false  -- Defines a few variables to avoid doing so later.

--:// Functions
function SelectItem()
	SelectedItem = Items[math.random(#Items)]
end

--:// Main 'Spinner'
InterfaceButton.MouseButton1Click:Connect(function()
	if Spinning == true then return end
	SelectItem()
	Spinning = true
	Speed = 1
	for i = 1, Spins do
		for _, v in pairs (Items) do
			InterfaceDisplay.Text = v
			task.wait(Speed / 60)
			if Speed <= 3 then
				Speed /= 0.1 -- How fast you want it to slow down.
			end
		end
	end
	for _, v in pairs (Items) do
		InterfaceDisplay.Text = v
		if v == SelectedItem then
			InterfaceDisplay.BackgroundColor3 = Color3.fromRGB(113, 255, 52)
			task.wait(0.1)
			InterfaceDisplay.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			task.wait(0.1)
			InterfaceDisplay.BackgroundColor3 = Color3.fromRGB(113, 255, 52)
			task.wait(0.1)
			InterfaceDisplay.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			task.wait(0.1)
			InterfaceDisplay.BackgroundColor3 = Color3.fromRGB(113, 255, 52)
			task.wait(0.1)
			InterfaceDisplay.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			task.wait(0.1)
			break
		else
			task.wait(Speed / 60)
		end
	end
	Spinning = false
end)

Not the world’s cleanest script, but it works.

Feel free to use the place below.

ThomasTest.rbxl (38.5 KB)

2 Likes

Thank you so much, this works absolutely perfectly. I can’t imagine how much time you probably spent on this, especially since you took the time to make your own place to show it. Thank you again!

3 Likes