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.
01 | Beams = script.Parent.Beams |
02 | SkyBeam = script.Parent.SkyBeam |
03 | NumValue = script.Parent.Value.Value |
04 | Countdown = script.Parent.Countdown.Value |
05 |
06 | RadiusBrick = script.Parent.RadiusBrick |
07 |
08 | RadiusBrick.Touched:connect( function (otherPart) |
09 | if NumValue = = 1 and otherPart.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
10 | NumValue = 2 |
11 | Countdown = |
12 | else |
13 | end |
14 | end ) |
15 |
16 | RadiusBrick.TouchEnded:connect( function () |
17 | NumValue = 1 |
18 | Countdown = |
19 | end ) |
Could someone please tell me what I need to put in the countdown parts or give me some tips?
Thanks.
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)
01 | Beams = script.Parent.Beams |
02 | SkyBeam = script.Parent.SkyBeam |
03 | NumValue = script.Parent.Value.Value -- You may want to fix this.. Never have ".Value" when defining a variable |
04 | RadiusBrick = script.Parent.RadiusBrick |
05 |
06 | RadiusBrick.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 |
13 | end ) |
14 |
15 | RadiusBrick.TouchEnded:connect( function () |
16 | NumValue.Value = 1 |
17 | script.CountDown.Disabled = false |
18 | script.CountUp.Disabled = true |
19 | end ) |
In the CountUp script, it would look like:
1 | local Countdown = script.Parent.Parent.Countdown |
2 |
3 | while true do |
4 | wait( 1 ) |
5 | Countdown.Value = Countdown.Value + 1 |
6 | end |
CountDown:
1 | local Countdown = script.Parent.Parent.Countdown |
2 |
3 | while true do |
4 | if Countdown.Value > 0 then |
5 | Countdown.Value = Countdown.Value - 1 |
6 | end |
7 | wait( 1 ) |
8 | end |
Hope I helped!
01 | local 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` |
You could make use of the lastIndex
variable when setting the reverse countdown
starting from that last value.