Randomly pick something in a table without reusing it until everything has been used

Hello developers, I’m trying to figure out how to use everything in the table below without repeating it’s self (randomly picks one to use) and then after everything has been picked it restarts. Here’s what I have…

Currently, the script goes throught everything in the PossibleNotifications one by one in order but I want it to randomly pick through it and not repeat untill everything in the tabel has been used.

How would I achieve this, thanks? :slight_smile:

ThreeSix = math.random(3, 6)
TenFifteen = math.random(10, 15)
WaitingTime = math.random(150, 300)

local PossibleNotifications = {
	"Notification: We hope you're enjoying the first version of ---! ☕",
	"Notification: Make sure to like & favorite the game! 👍⭐",
	"Notification: Many more foods & items are on their way!",
	"We thank you for your dedication to ---. 💕"
}

while true do
		for Value = 1, #PossibleNotifications do
			local Notification = PossibleNotifications[Value]
			
			NotificationText.Text = Notification
			MainFrame.Visible = true
			TweenService:Create(MainFrame, TransitionInfo, {Position = UDim2.new(0.5, 0, 0.1, 0)}):Play()
			NotificationText.Visible = true
			TweenService:Create(NotificationText, TransitionInfo, {BackgroundTransparency = 0}):Play()
			TweenService:Create(NotificationText, TransitionInfo, {TextTransparency = 0}):Play()
			
			wait(TenFifteen)
			
			TweenService:Create(MainFrame, TransitionInfo, {Position = UDim2.new(0.5, 0, 0.16, 0)}):Play()
			local TweenWait = TweenService:Create(NotificationText, TransitionInfo, {BackgroundTransparency = 1})
			TweenWait:Play()
			TweenService:Create(NotificationText, TransitionInfo, {TextTransparency = 1}):Play()

			TweenWait.Completed:Connect(function()
				TweenService:Create(MainFrame, TransitionInfo, {Position = UDim2.new(0.5, 0, 0.04, 0)}):Play()
				MainFrame.Visible = false
				NotificationText.Visible = false
			end)
			
			wait(WaitingTime)
		end
	end
1 Like

Right after that while true do, shuffle the table, i.e. randomize the order. Then simply use them in the order they are in as normal.

Shuffling a table:

-- https://gist.github.com/Uradamus/10323382#gistcomment-2754684
for i = #PossibleNotifications, 2, -1 do
	local j = math.random(i)
	PossibleNotifications[i], PossibleNotifications[j] = PossibleNotifications[j], PossibleNotifications[i]
end

One annoying thing is that you might shuffle, loop through all of them, shuffle again and end up with the previous last item in the first spot, which means that one notification is used twice in a row.

Yesterday I believe you had something that worked, do you still have that old code before you edited your message?

Why not just do

while true do
	for index,Value in pairs(PossibleNotifications) do

?

Seems to error out,
image
image

Remove local Notification = PossibleNotifications[Value] and instead just use Value.

Thank you but this code didn’t do what I wanted. It preforms the exact same way I started the form.

What it does: Goes through the table one by one in a loop.
What it’s supposed to do: Randomly go through the table without repeating it’s past until everything has been picked and then the cycle resets.

For example,

local PossibleNotifications = {
	"Hi",
	"Hello",
	"Yes",
	"No"
}

It would go Hello, No, Hi, Yes Cycle resets, No, Yes, Hi, Hello Cycle resets, and it keeps going.

Hopefully that helps. :slight_smile:

Well, I mean you could do local Value = PossibleNotifications[math.random(1,#PossibleNotifications)]

Would I put that into my code like this?

Remove the for loop, and yes. However, this will cause your thing to have a possibility of repeating messages, so erm, just use this…

local used = {}
while true do
	if #PossibleNotifications >= 0 then
		PossibleNotifications = table.clone(used)
		used = {}
	end
	local index = math.random(1,#PossibleNotifications)
	local Value = PossibleNotifications[index]
	table.insert(used,Value)
	table.remove(value,index)
end

(something like this)

1 Like

Seems to be erroring out again even though there is an interval.
image
image

If anybody has any solutions that would be welcomed.

this would work i think

local Table = {"A", "B", "C"}

local RandomThing = Table[math.random(1, #Table)]
table.remove(Table, RandomThing)
1 Like

ok nvm here is the full script

local Table = {"A", "B", "C"}

local UsedTable = {}

while wait() do
	if #Table >= 1 then
		local RandomNumber = math.random(1, #Table)
		local RandomThing = Table[RandomNumber]
		
		if not table.find(UsedTable, RandomThing) then
			print(RandomThing)
			
			table.insert(UsedTable, RandomThing)
			table.remove(Table, RandomNumber)
		end
	end
end

and heres the output

2022-05-10

1 Like

I have not edited my message. If I had, you would’ve been able to click the pencil icon at the top of an edited message and see what it was.

Thanks for the base code or this, really helps. Is there a way I can make it so that way it keeps going, so when the table is empty it just resets and starts all over again and print A, B, C, randomly.

1 Like

to reset it just do this:

UsedTable = {}

PossibleNotifications = {
	"Notification: We hope you're enjoying the first version of ---! ☕",
	"Notification: Make sure to like & favorite the game! 👍⭐",
	"Notification: Many more foods & items are on their way!",
	"We thank you for your dedication to ---. 💕"
}

or if you want it to be in a function that you can reuse

local function ClearUsedTable()
	PossibleNotifications = {
		"Notification: We hope you're enjoying the first version of ---! ☕",
		"Notification: Make sure to like & favorite the game! 👍⭐",
		"Notification: Many more foods & items are on their way!",
		"We thank you for your dedication to ---. 💕"
	}
	
	table.clear(UsedTable)
end
ClearUsedTable()
1 Like

Would this work for your example?

local Table = {"A", "B", "C"}
local UsedTable = {}

local function ClearUsedTable()
	UsedTable = {}
end

while wait() do
	if #Table == 0 then
		ClearUsedTable()
		print("Reset")
	elseif #Table >1 then
		local RandomNumber = math.random(1, #Table)
		local RandomThing = Table[RandomNumber]
		
		if not table.find(UsedTable, RandomThing) then
			print(RandomThing)
			table.insert(UsedTable, RandomThing)
			table.remove(Table, RandomNumber)
		end
	end
end

just change

to

so the script will work now

then the script will look like this

local PossibleNotifications = {
	"Notification: We hope you're enjoying the first version of ---! ☕",
	"Notification: Make sure to like & favorite the game! 👍⭐",
	"Notification: Many more foods & items are on their way!",
	"We thank you for your dedication to ---. 💕"
}

local UsedTable = {}

while wait() do
	if #PossibleNotifications >= 1 then
		local RandomNumber = math.random(1, #PossibleNotifications)
		local RandomThing = PossibleNotifications[RandomNumber]

		if not table.find(UsedTable, PossibleNotifications) then
			print(PossibleNotifications)

			table.insert(UsedTable, RandomThing)
			table.remove(PossibleNotifications, RandomNumber)
		end
	end
end

image
image

local UsedTable = {}

local function ClearUsedTable()
	PossibleNotifications = {
		"Notification: We hope you're enjoying the first version of ---! ☕",
		"Notification: Make sure to like & favorite the game! 👍⭐",
		"Notification: Many more foods & items are on their way!",
		"We thank you for your dedication to ---. 💕"
	}

	table.clear(UsedTable)
end

while wait() do
	if #UsedTable == 4 then
		ClearUsedTable()
		print("Reset Table")
	elseif #PossibleNotifications >= 1 then
		local RandomNumber = math.random(1, #PossibleNotifications)
		local RandomThing = PossibleNotifications[RandomNumber]

		if not table.find(UsedTable, RandomThing) then
			print(RandomThing)
			table.insert(UsedTable, RandomThing)
			table.remove(PossibleNotifications, RandomNumber)
		end
	end
end