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

I am trying to make a floor that gets generated part by part using instances. ?

Asked by 2 years ago
Edited by JesseSong 2 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

-- Create Floor

local h = 0
while h < 5 do
    wait()
    local x = 0
    local y = 5
    local z = 0
    z = z * 2
    local g = 1
    local PartZ = Instance.new("Part", game.Workspace)

    PartZ.Size = Vector3.new(4,1,4)

    PartZ.Position = Vector3.new(x,y,z)

    z = z + 4
    PartZ.Name = g
    g = g + 1
    PartZ.Anchored = true
    h = h + 5
end

Not in Code:

Only one part tends to generate from instance.new(). I would like help understanding why my code does this. I am only generating parts on the z axis, so please help me.

:)

  • IsaacFriend808

Re-edited by JC/JesseSong

1 answer

Log in to vote
1
Answered by 2 years ago

You shouldn't create the variables inside of the while loop because the variable can now only be used inside the loop. This means when the loop runs again, it cannot access the new values of x,y and z. The value of x, y, and z will be the starting value you assigned them to be causing them to all be in the same position. Putting the variable on the outside will allow the entire loop and other code to access the same variable. So instead of putting it in the inside

while true do
    local x = 5
    -- other code
end

Put it outside of the loop

local x = 5
while true do
    -- other code
end
0
Nice answer. I accepted it! JesseSong 3916 — 2y
0
Thanks :) JustinWe12 723 — 2y
0
@JustinWe12, thanks for the input! The code at first had not worked, but I managed to get it working by changing how much 'h' was being added. Telling me I should not add variables like those inside the loop also helped me with my code, thank you Justin. IsaacFriend808 6 — 2y
Ad

Answer this question