CCommand Things
CCommand Things

Reputation: 17

I need two variables for a for loop in lua

I'm trying Lua code to make a game in roblox and I want the cylinder to grow bigger every 0.0625 seconds to make a shockwave kind of thing and the scripts parent is the cylinder part.

for i = 0,5,1 a = 1,0,-0.1 do
    script.Parent.Transparency = a
    script.Parent.Size = Vector3.new(script.Parent.Size.X, script.Parent.Size.Y = i, script.Parent.Size.Z = i)
end

Upvotes: 0

Views: 847

Answers (1)

Piglet
Piglet

Reputation: 28974

You can only have one control variable in a for loop. If you need more you need more loops or manage another variable in the loop's body.

It's not clear from your post what is required solution.

From Lua Reference Manual 3.3.5: for statement

for v = e1, e2, e3 do block end

is equivalent to the code:

 do
   local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
   if not (var and limit and step) then error() end
   var = var - step
   while true do
     var = var + step
     if (step >= 0 and var > limit) or (step < 0 and var < limit) then
       break
     end
     local v = var
     block
   end
 end

Make sure you understand this and derive a solution for your problem from it.

Assuming you want to degrade transparency value between each size increase you should use a nested loop.

for size = 1, 5 do
  script.Parent.Size = Vector3.new(script.Parent.Size.X, size, size)
  for transparency = 1, 0, -0.1 do
    script.Parent.Transparency = transparency
  end

end

I skipped size 0 because I assume that size 0 objects are not visible so why alter their transparency?

Note that I fixed a syntax error in your code.

script.Parent.Size = Vector3.new(script.Parent.Size.X,
                       script.Parent.Size.Y = i, script.Parent.Size.Z = i)

would cause a ')' expected near '=' error as you may not assign values in a function call.

Upvotes: 2

Related Questions