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

How do I make this countdown work correctly?

Asked by 9 years ago

I made this simple countdown script by repeating things, but when I said like, one second, two second, it was like slower than I was counting. Plus I am making a countdown for the new game I am making which has a movie in it. I set the movie to start in 20 seconds, but it started 2 seconds early. This is the script:

local c = game.Workspace.Countdown.SurfaceGui.TextLabel
while true do
c.Text = 20
wait(1)
c.Text = 19
wait(1)
c.Text = 18
wait(1)
c.Text = 17
wait(1)
c.Text = 16
wait(1)
c.Text = 15
wait(1)
c.Text = 14
wait(1)
c.Text = 13
wait(1)
c.Text = 12
wait(1)
c.Text = 11
wait(1)
c.Text = 10
wait(1)
c.Text = 9
wait(1)
c.Text = 8
wait(1)
c.Text = 7
wait(1)
c.Text = 6
wait(1)
c.Text = 5
wait(1)
c.Text = 4
wait(1)
c.Text = 3
wait(1)
c.Text = 2
wait(1)
c.Text = 1
wait(1)
c.Text = 0
wait(1)
c.Text = "Movie has Started!"
wait(15)
end

I am trying to get this start in this precise timing on this script (Thanks Goulstem for helping me this):

local bin = script.Parent
local decal = bin.Decal

local MainSlides = {
    ["1"] = {'http://www.roblox.com/asset/?id=213163470', 15},
    ["2"] = {'http://www.roblox.com/asset/?id=213486920', 3},
    ["3"] = {'http://www.roblox.com/asset/?id=157656296', 3},
    ["4"] = {'http://www.roblox.com/asset/?id=213486967', 3},
    ["5"] = {'http://www.roblox.com/asset/?id=151111211', 3},
    ["6"] = {'http://www.roblox.com/asset/?id=213487020', 3}
}

function getNumTabs(tab)
    local num = 0
    for i,v in pairs(tab) do num = num + 1 end
    return num
end

function runTable(tab)
    for i = 1,getNumTabs(tab) do
        local currentTab = tab[tostring(i)]
        decal.Texture = currentTab[1]
        wait(currentTab[2])
    end
end

while wait() do
    runTable(MainSlides)
end

When it says Movie has started, I put wait wait 15 secs. since 2-6 says 3 seconds, and 3 times 5 equals 15. I hope you understand what I am asking. I am not good at asking clearly :D

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

There's no reason for you use to use the strings "1", "2", ..., "6" as the keys. Just use a list, ie, 1, 2, 3, ..., 6

local MainSlides = {
    {'http://www.roblox.com/asset/?id=213163470', 15},
    {'http://www.roblox.com/asset/?id=213486920', 3},
    {'http://www.roblox.com/asset/?id=157656296', 3},
    {'http://www.roblox.com/asset/?id=213486967', 3},
    {'http://www.roblox.com/asset/?id=151111211', 3},
    {'http://www.roblox.com/asset/?id=213487020', 3}
}

Now getNumTabs isn't useful, since you can just use #MainSlides to find out how many they are.

Even better, you can just use ipairs for runTable:

function runTable(tab)
    for _, slide in ipairs(tab) do
        decal.Texture = slide[1]
        wait(slide[2])
    end
end

First of all, use a for loop for your countdown:

for timeLeft = 20, 0, -1 do
    -- Replaces the 40 lines of repetition!
    c.Text = timeLeft
    wait(1)
end
c.Text = "Movie has started!"

If you want the slide playback to match up with this, just put them in the same script!

-- (Define everything needed for runTable here, of course)

local c = game.Workspace.Countdown.SurfaceGui.TextLabel

function countDown()
    for timeLeft = 20, 0, -1 do
        -- Replaces the 40 lines of repetition!
        c.Text = timeLeft
        wait(1)
    end
    c.Text = "Movie has started!"
end

while true do
    countDown()
    runTable(MainSlides)
end
0
Thanks bro. Especially for explaining them. User#5689 -1 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

not sure if this will help

local bin = script.Parent
local decal = bin.Decal
local SG = bin.SurfaceGui
local Label = SG.Label

local MainSlides = {
--   ['rbxassetid://IMAGEID'] = WaitTime,
    ['rbxassetid://213163470'] = 15,
    ['rbxassetid://213486920'] = 3,
    ['rbxassetid://157656296'] = 3,
    ['rbxassetid://213486967'] = 3,
    ['rbxassetid://151111211'] = 3,
    ['rbxassetid://213487020'] = 3
}

function runTable(tab)
   for image, time in pairs(MainSlides) do
        decal.Texture = image
        wait(time)
    end
end

-- Countdown
for i=20, 0, -1 do
    Label.Text = i
    wait(1)
end

Label.Text = "Movie will now start!"
wait(2)
Label.Visible = false
-- Movie Starts

--Activate sequence
while wait() do
    runTable(MainSlides)
end

--Edited by DragonSK16
0
`pairs` doesn't guarantee the order that the table will be traversed; in this case, it's probably that the order matters, so you shouldn't use `pairs`. BlueTaslem 18071 — 9y

Answer this question