so basically i am using math.random to choose a random line of text and then set that text to a textlabel
is there any way that the random line won’t duplicate meaning it wont show twice in a row or more?
Yes there is, you can either store the result from math.random or the string of text that math.random result leads to. Then when its time to pick a new string do something like this:
-- Example using the result from math.random
local temp
local randomNumber
repeat
randomNumber= math.random(1,100) -- Example math.random
until randomNumber ~= temp
temp = randomNumber
If this is being stored in a table, copy the table for use ( only if you plan on using it again, otherwise dont, its code bloat), when you pick a random number, tell the code to remove the index using table.remove()
or t[i] = nil
, this will remove the item from the table, and from there you can avoid using the exact same strings.
If you are looking to get the total number of items in a table, use the operator #
, then your table, for example: #example
you can then tell the script to look for a number based on how many items are in the table with math.random
, because #example -- 'example' being the table variable
will return a number, we can use it as our y
argument to look for any number between the amount.
Quote
This will only work for the number it last chose, if another number is chosen, the code will be able to pick the number again.
Yep. So basicailly I made this little function where it pickes a text, removes it from a list, and Adds it to another list. This new list moves up one position everytime the function is called (1st becomes the 2nd postion, what was 2nd becomes 3rd etc.) And when the position meet with variable "DontShowUpMoreThenBlankInARow
" + 1, then it gets adding back in the real list. so this allows you to have certain text that you don’t want repeating twice in a row, but it allows you to use it again if 3 times. you could change that value to a bigger number, or math.huge if you didn’t want to use this feature (which in that case, this function would be pointless and you would be better off making your own script with that instead of math.huge, but I mean it is an option I guess)
here is my script and explains it the best I can
local ListOfEachText = {"This is one message", "This one is another", "And wow I can add as much as I want",} --All messages here
local DontShowUpMoreThenBlankInARow = 2 --Change this if you don't want the text to show up _ in a row
local TextLabel = nil --Change to pathway of textlabel
local InARowTable = { } --We store them in this table once used, and wait until the amount of different changes in DontShowUpMoreThenBlankInARow happen
local function GenerateText()
local PickedText = ListOfEachText[math.random(1, #ListOfEachText)] -- get text
table.remove(ListOfEachText, table.find(ListOfEachText, PickedText)) -- Remove it for now
local Num = 0
local Value = PickedText
repeat --Go through table
Num += 1
local Switch = InARowTable[Num]
InARowTable[Num] = Value
Value = Switch
--What was in the first position, becomes the 2nd, what was in the second position becomes third etc, until there is no more old text
until Value == nil
if InARowTable[DontShowUpMoreThenBlankInARow + 1] ~= nil then --Checks if there is anything in that slot (plus one so the value is truely rep.)
table.insert(ListOfEachText, InARowTable[DontShowUpMoreThenBlankInARow + 1]) --If so, we add it back so it can be used again!
InARowTable[DontShowUpMoreThenBlankInARow + 1] = nil
end
return PickedText --Returns the picked text so it can be used!
end
--I am using a repeat loop and changing it every 15 seconds, although Everytime you make a text, just call the function
repeat
local Text = GenerateText()
print(Text)
TextLabel.Text = Text
task.wait(15)
until false
thanks a lot for the help
xxxxxxxxxxxxxxxx
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.