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

How could I make this countdown work?

Asked by
Avectus 120
9 years ago

I'm trying to make a countdown start when the RadiusBrick is touched and when it's untouched the countdown starts to go back down. Just like a conquest style gamemode, if a player has captured the node for let's say 5 seconds and the player leaves the capture radius, the countdown decreases from 5 instead of returning to 0 right away. The touching functions work fine from previous tests but I'm not sure what to put in the countdown parts of this script.

01Beams = script.Parent.Beams
02SkyBeam = script.Parent.SkyBeam
03NumValue = script.Parent.Value.Value
04Countdown = script.Parent.Countdown.Value
05 
06RadiusBrick = script.Parent.RadiusBrick
07 
08RadiusBrick.Touched:connect(function(otherPart)
09    if NumValue == 1 and otherPart.Parent:FindFirstChild("Humanoid") ~= nil then
10        NumValue = 2
11        Countdown =
12    else
13        end
14end)
15 
16RadiusBrick.TouchEnded:connect(function()
17    NumValue = 1
18    Countdown =
19end)

Could someone please tell me what I need to put in the countdown parts or give me some tips?

Thanks.

2 answers

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

The way I'd accomplish your goal is by creating 2 additional scripts, as children of your current script. One would be named "CountUp", while the other would be "CountDown". To trigger each of those scripts, you should do the following: (Please excuse my spacing, as I'm doing all of this on the site)

01Beams = script.Parent.Beams
02SkyBeam = script.Parent.SkyBeam
03NumValue = script.Parent.Value.Value -- You may want to fix this.. Never have ".Value" when defining a variable
04RadiusBrick = script.Parent.RadiusBrick
05 
06RadiusBrick.Touched:connect(function(otherPart)
07    if NumValue.Value == 1 and otherPart.Parent:FindFirstChild("Humanoid") ~= nil then
08        NumValue.Value = 2
09        script.CountDown.Disabled = true
10        script.CountUp.Disabled = false
11    else
12        end
13end)
14 
15RadiusBrick.TouchEnded:connect(function()
16    NumValue.Value = 1
17    script.CountDown.Disabled = false
18    script.CountUp.Disabled = true
19end)

In the CountUp script, it would look like:

1local Countdown = script.Parent.Parent.Countdown
2 
3while true do
4    wait(1)
5    Countdown.Value = Countdown.Value + 1
6end

CountDown:

1local Countdown = script.Parent.Parent.Countdown
2 
3while true do
4    if Countdown.Value > 0 then
5        Countdown.Value = Countdown.Value - 1
6    end
7    wait(1)
8end

Hope I helped!

0
Works great thanks! Also, what's wrong with having .Value? Avectus 120 — 9y
Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago
01local NumValue, lastIndex, countdown, debounce do -- creating new scope
02    countdown = function(start, stop, step) -- numeric loop function
03        local last -- later set to the last `index`
04        if not step then -- the `step` argument is blank or nil
05            for index = start, stop do -- do not take it into consideration
06                print(index);wait(1)
07                last = index
08            end
09        else --elseif if the step is provided
10            for index = start, stop, step do -- do take it into consideration
11                print(index);wait(1) -- prints each index with interval of `1` (wait)
12                last = index -- sets the `last variable` equal to the `last index`
13            end
14        end
15        lastIndex = last -- the `lastIndex` variable outside of this `block`
View all 25 lines...

You could make use of the lastIndex variable when setting the reverse countdown starting from that last value.

Answer this question