Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

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.

3 answers

✓ Accepted answer

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)

3 comments

0

There is no error in the output but It wont add anything on to the value.

0

Look at the post below.

0

I've commented on the post below.

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.

2 comments

0

Oh my god! Thanks so much! :) I really appreciate it!

1

I edited my answer, look at that

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)

2 comments

0

Oh, switch signs from 'greater than' to 'less than' on line 6.

0

If you want the onChanged function to fire when the Value hits '5', make sure that the sign is either '==', or '>='.

This discussion is preserved as it was. New answers, comments, and votes are closed.