user-interfaceluarobloxcustom-lists

How do I change a TextLabel's text using a random number and a list?


So recently I've been trying new things in roblox scripting, and I have been working on this problem for days but I can't seem to find an answer. What I want to do is every 3 seconds, the script chooses a random string from the list and then changes the script's parent's Text property to it, but it doesn't seem to be working. Any ideas on this? Thanks.

Script:

local phrases = {"This should be changing!", "Have a nice time!", "Help used!", "Test", "Sample Text",}

while true do
    script.Parent.Text = (phrases[Random:NextNumber])
    wait(3)
end 

Solution

  • calling NextNumber without any parameters returns a float between 0-1, a value like 0.333 or 0.5
    https://developer.roblox.com/en-us/api-reference/datatype/Random

    number Random:NextNumber ( )
    Returns a pseudorandom number uniformly distributed over [0, 1).

    You need a usable index from your list, somewhere between 1 and the length of your phrases list.

    int Random:NextInteger ( int min, int max )
    Returns a pseudorandom integer uniformly distributed over [min, max].

    local phrases = {"This should be changing!", "Have a nice time!", "Help used!", "Test", "Sample Text",}
    
    while true do
        script.Parent.Text = phrases[ Random:NextInteger( 1, #phrases ) ]
        wait(3)
    end