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 make a value keep increasing in a while loop?

p = script.Parent

while true do
	local pl = game["Workspace"]:FindFirstChild("FlickeringLight").PointLight
	local x  = 1
	wait(0.1)
	pl.Enabled = false
	p.Material = 256
	p.BrickColor = BrickColor.new(Color3.new(0, 0, 0))
	wait(0.02)
	pl.Enabled = true
	p.Material = 288
	p.BrickColor = BrickColor.Yellow()
	wait(0.002)
	pl.Enabled = false
	p.Material = 256
	p.BrickColor = BrickColor.new(Color3.new(0, 0, 0))
	wait(0.2)
	pl.Enabled = true
	p.Material = 288
	p.BrickColor = BrickColor.Yellow()
	
	print("Flicker " + x+1)
end

I want to make x keep increasing by one every time a loop goes through. but it comes out with this error message.

Workspace.FlickeringLight.Script:23: attempt to perform arithmetic on a string value

1 comment

0

You keep redefining x as one in the loop. Set "local x = 1" out of the loop, and then in the loop write "x = x + 1"

1 answer

✓ Accepted answer

Your syntax is wrong. To add something on to a string, do:

print("String " .. x + 1)

Note the ..

Also:

This will not add 1 to x every time it loops. Define x outside of the while loop, then add to it inside.

local x = 1
while true do
	wait(1)
	x = x + 1
	print("String " ..x)
end

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