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

How do I make a value keep increasing in a while loop?

Asked by
Jephi 42
9 years ago
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

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" tkddude2 75 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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
Ad

Answer this question