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

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

-- 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

✓ Accepted answer

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

3 comments

0

Nice answer. I accepted it!

0

Thanks :)

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.

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