Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[FIXED BY ME] I made a script for a randomly generated sign, not switching text. Why?

Asked by 4 years ago
Edited 4 years ago

This question has been solved by the original poster.

So, I'm making a sign that every 1 minutes (I know, my script says 10 seconds, that's just for making testing easier) It will change text. The first time the script runs, it randomly picks a line of text, Which works. But the second time it runs, it prints the randomly generated number correctly but the text on the sign does not change. Heres my code.

local text = script.Parent.SurfaceGui.TextLabel

local number = math.random(3)
i = 10

while i == 10 do
    if number == 3 then
        text.Text = "Now turn back :)"
        wait(10)
        text.Text = ""
        local number = math.random(3)
        print(number)
    end

    if number == 2 then
        text.Text = "What are you gonna do now?"
        wait(10)
        text.Text = ""
        local number = math.random(3)
        print(number)
    end

    if number == 1 then
        text.Text = "There's nothing here. Lol."
        wait(10)
        text.Text = ""
        local number = math.random(3)
        print(number)
    end
end

Thank you for helping :)

Not gonna lie, I did a dumb. If anyone is curious, on the lines where I 'Redifined" number I typed 'local' before, like this

local number = math.random(3)

this was creating a new variable, I had to remove the local to fix the script, so now the script looks like this

local text = script.Parent.SurfaceGui.TextLabel

local number = math.random(3)
i = 10

while i == 10 do
    if number == 3 then
        text.Text = "Now turn back :)"
        wait(10)
        text.Text = ""
        number = math.random(3)
        print(number)
    end

    if number == 2 then
        text.Text = "What are you gonna do now?"
        wait(10)
        text.Text = ""
        number = math.random(3)
        print(number)
    end

    if number == 1 then
        text.Text = "There's nothing here. Lol."
        wait(10)
        text.Text = ""
        number = math.random(3)
        print(number)
    end
end

1 answer

Log in to vote
0
Answered by 4 years ago

Howdy!

Your number value isn't changing as it's predefined outside of the loop. If you move the variable inside the loop, it'll continuously reroll the number at random. You also won't need the number reroll inside the three functions.

If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.

Be sure to check out the Roblox API Documentation as well for additional reference.

0
As you answered this, I fixed the script. Sorry lol! pokemine1o9 44 — 4y
Ad

Answer this question