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

How do I add 1 to a Number Value every 5 seconds, until it reaches a certain amount?

Asked by
Nidoxs 190
10 years ago

So I have a "Number Value" inside a brick and every 5 seconds I want it to add 1 to the current value. Here is what I have so far.

local Pos = script.Parent.Position
local C = script.Parent.C
if C.Value <= 5 then
game.Soundscape.Bomb:Play()
E = Instance.new("Explosion", script.Parent) 
E.BlastRadius = 190 
E.BlastPressure = 50000 
E.DestroyJointRadiusPercent = 100 
E.Position = Vector3.new(Pos)   
else return end  

for i=1,5 do 
wait(5)
C.Value +1 
wait() 
end
1
What is this script's purpose? Is it when the value of C is equal to '5', an explosion occurs? Because if you set up the conditional to 'C.Value <= 5', then anything that's below 5 will run the 'if' statement, making your 'for' loop quite useless. Redbullusa 1580 — 10y

3 answers

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

To fix your problem, make sure you're making the value equal to itself, then add one.

for i = 1, 5 do
    wait(5)
    C.Value = C.Value + 1
end

If you want to add restrictions, you may do this:

for i = 1, 5 do
    wait(5)
    if C.Value < 5 then
        C.Value = C.Value + 1
    end
end

That way it prevents the 'for' loop from adding any more.


I would like to point out that your 'for' loop won't return back to the 'if' statement on line 3, therefore making it impossible to execute the 'if' statement unless if you disabled and re-enabled your script.

Instead of doing that, use the 'Changed' event since you're using a physical NumberValue object.

function onChanged()
    if C.Value <=5 then
        game.Soundscape.Bomb:Play()
        E = Instance.new("Explosion") -- I wouldn't define it's parent yet.
        E.BlastRadius = 190
        E.BlastPressure = 50000
        E.DestroyJointRadiusPercent = 100
        E.Position = Vector3.new(Pos)
        E.Parent = script.Parent
    end  
end

C.Changed:connect(onChanged)
0
There is no error in the output but It wont add anything on to the value. Nidoxs 190 — 10y
0
Look at the post below. Nidoxs 190 — 10y
0
I've commented on the post below. Redbullusa 1580 — 10y
Ad
Log in to vote
1
Answered by
Mystdar 352 Moderation Voter
10 years ago

Try:

x = game.Workspace.IntValue -- Path here
while x<5 do -- Will only run if x is smaller than 5
    x.Value = x.Value + 1
    wait(5)
end

EDIT:

Your problem was on line 14, you did:

C.Value +1

Instead of:

C.Value = C.Value+1

You can't just add one, like you did.

0
Oh my god! Thanks so much! :) I really appreciate it! Nidoxs 190 — 10y
1
I edited my answer, look at that Mystdar 352 — 10y
Log in to vote
0
Answered by
Nidoxs 190
10 years ago

This won't work?

local C = script.Parent.C 
local Pos = script.Parent.Position

for i = 1, 5 do
    wait(5)
    if C.Value > 5 then
        C.Value = C.Value + 1
    end
end

function onChanged(E)
    if C.Value <=5 then
        game.Soundscape.Bomb:Play()
        E = Instance.new("Explosion")
        E.BlastRadius = 190
        E.BlastPressure = 50000
        E.DestroyJointRadiusPercent = 100
        E.Position = Vector3.new(Pos)
        E.Parent = script.Parent
    end  
end

C.Changed:connect(onChanged)
0
Oh, switch signs from 'greater than' to 'less than' on line 6. Redbullusa 1580 — 10y
0
If you want the onChanged function to fire when the Value hits '5', make sure that the sign is either '==', or '>='. Redbullusa 1580 — 10y

Answer this question