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?
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
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.
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, YesCycle resets,No, Yes, Hi, HelloCycle resets, and it keeps going.
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
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
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.
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()
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
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
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