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

Is there any way to shorten this?

Asked by 8 years ago

I want to use math.random, but i'm not sure how or if it will work. Here is the script:

wait(2)
if script.Parent.Visible == true then 
script.Parent.Text = script.Parent.Fact1.Value
wait(6)
script.Parent.Text = script.Parent.Fact2.Value
wait(6)
script.Parent.Text = script.Parent.Tip1.Value
end

1 answer

Log in to vote
1
Answered by
davness 376 Moderation Voter
8 years ago

Maybe, if you think like the most beginners, you will think on a script like this

local rand = math.random(1,3) -- draw a number between 1 and 3
wait(2)
if script.Parent.Visible == true then 
    if rand == 1 then
        script.Parent.Text = script.Parent.Fact1.Value
        wait(6)
    elseif rand == 2 then
        script.Parent.Text = script.Parent.Fact2.Value
        wait(6)
    elseif rand == 3 then
        script.Parent.Text = script.Parent.Tip1.Value
        wait(6)
    end
end

Obvoiusly, this is too long. Maybe you should use a table. You can consider it as a value container:

local myFirstTable = {nil, false, "dav1000999", 563754, math.huge, nil, "efl"}

So, you can use it to shorten your script. Once the three results use the same structure, we can replace them.

local values = {"Fact1", "Fact2", "Tip1"} -- gets the names
local num = math.random (1,3) -- draws a number
if script.Parent.Visible == true then
    script.Parent.Text = script.Parent:WaitForChild(values[num]).Value -- takes the name in basis of the number
    wait(6)
end

Hope it helps!

0
It did thanks. :D supermanswaqq 65 — 8y
0
please be sure to accept answer davness 376 — 8y
Ad

Answer this question