I was trying to make a tips TextLabel for my Loading Screen. I don’t want the tips repeating, so I’m trying to make the math.random result store in a table and when the script tries to generate another number it checks if that number is in the table, and if it is it generates another until the number it generates is not in the table. I even tried to fix it by looking at other threads here, but I couldn’t make it work… Here is the code:
local tipLabel = script.Parent
local tip1 = "Tip1 Test"
local tip2 = "Tip2 Test"
local tip3 = "Tip3 Test"
local tip4 = "Tip4 Test"
local tipsTable = {tip1, tip2, tip3, tip4}
while true do
function selectNewNumber()
local newNumber
local lastNumberTable = {}
while not newNumber or newNumber == lastNumberTable do
newNumber = math.random(1,#tipsTable)
end
table.insert(lastNumberTable, newNumber)
return tipsTable[newNumber]
end
tipLabel.Text = selectNewNumber()
wait(2)
end
Although it has no errors, it still doesn’t work . The generated numbers and tips on the Loading Screen repeat. I think that the problem is that the code is not checking the table too see wich numbers had already been generated.
You lastNumberTable table is just local to the selectNewNumber function. So each time you call that function it will see an empty table. Move your lastNumberTable outside of that function for it to save between calls.
On top of that, you’re checking newNumber == table 0x0a6ef2 (random memory string)
which is trying to compare nil or a number to a table, and not checking if said number is in the table.
Ty for helping!
I changed the code to this, no errors but I think it isn’t working yet… What am I doing wrong??
while true do
local lastNumberTable = {}
function selectNewNumber()
local newNumber
while not newNumber or newNumber == table.find(lastNumberTable, newNumber) do
newNumber = math.random(1,#tipsTable)
end
table.insert(lastNumberTable, newNumber)
return tipsTable[newNumber]
end
tipLabel.Text = selectNewNumber()
wait(2)
end
local tipLabel = script.Parent
local tip1 = "Tip1 Test"
local tip2 = "Tip2 Test"
local tip3 = "Tip3 Test"
local tip4 = "Tip4 Test"
local tipsTable = {
tip1,
tip2,
tip3,
tip4
}
local previousNumbers = {}
local function selectNewNumber()
local newNumber = nil
repeat
newNumber = Random.new():NextInteger(1, #tipsTable)
until not table.find(previousNumbers, newNumber)
table.insert(previousNumbers, newNumber)
return tipsTable[newNumber]
end
while true do
tipLabel.Text = selectNewNumber()
task.wait(2)
end