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.

Beams = script.Parent.Beams
SkyBeam = script.Parent.SkyBeam
NumValue = script.Parent.Value.Value
Countdown = script.Parent.Countdown.Value

RadiusBrick = script.Parent.RadiusBrick

RadiusBrick.Touched:connect(function(otherPart)
    if NumValue == 1 and otherPart.Parent:FindFirstChild("Humanoid") ~= nil then
        NumValue = 2
        Countdown = 
    else
        end
end)

RadiusBrick.TouchEnded:connect(function()
    NumValue = 1
    Countdown = 
end)

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)

Beams = script.Parent.Beams
SkyBeam = script.Parent.SkyBeam
NumValue = script.Parent.Value.Value -- You may want to fix this.. Never have ".Value" when defining a variable
RadiusBrick = script.Parent.RadiusBrick

RadiusBrick.Touched:connect(function(otherPart)
    if NumValue.Value == 1 and otherPart.Parent:FindFirstChild("Humanoid") ~= nil then
        NumValue.Value = 2
        script.CountDown.Disabled = true
        script.CountUp.Disabled = false
    else
        end
end)

RadiusBrick.TouchEnded:connect(function()
    NumValue.Value = 1
    script.CountDown.Disabled = false
    script.CountUp.Disabled = true
end)

In the CountUp script, it would look like:

local Countdown = script.Parent.Parent.Countdown

while true do
    wait(1)
    Countdown.Value = Countdown.Value + 1
end

CountDown:

local Countdown = script.Parent.Parent.Countdown

while true do
    if Countdown.Value > 0 then
        Countdown.Value = Countdown.Value - 1
    end
    wait(1)
end

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
local NumValue, lastIndex, countdown, debounce do -- creating new scope
    countdown = function(start, stop, step) -- numeric loop function
        local last -- later set to the last `index`
        if not step then -- the `step` argument is blank or nil
            for index = start, stop do -- do not take it into consideration
                print(index);wait(1)
                last = index
            end
        else --elseif if the step is provided
            for index = start, stop, step do -- do take it into consideration
                print(index);wait(1) -- prints each index with interval of `1` (wait)
                last = index -- sets the `last variable` equal to the `last index`
            end
        end
        lastIndex = last -- the `lastIndex` variable outside of this `block`
        --because we can't use the `last` variable created here in other blocks
    end;

script.Parent.Touched:connect(function (hit)
    if pcall(function() return hit.Parent.Humanoid end) and NumValue  then
    --whenever the script's parent is touched, we start the countdown
        countdown(1,5);
        print(lastIndex)  --and print the last index
    end
end))

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

Answer this question