Hey guys can somebody tell me how to make my code more efficient

Trying to make a block move forward every second

wait(1)
script.Parent.CFrame = CFrame.new(0, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(1, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(2, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(3, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(4, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(5, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(6, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(7, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(8, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(9, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(10, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(11, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(12, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(13, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(14, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(15, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(16, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(17, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(18, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(19, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(20, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(21, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(22, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(23, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(24, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(25, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(26, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(27, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(28, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(29, 5, 0)
wait(1)
script.Parent.CFrame = CFrame.new(30, 5, 0)
1 Like
1 Like

So like

while wait(1) do
script.Parent.CFrame = CFrame.new(0, 5, 0)
script.Parent.CFrame = CFrame.new(1, 5, 0)
script.Parent.CFrame = CFrame.new(2, 5, 0)
script.Parent.CFrame = CFrame.new(3, 5, 0)
script.Parent.CFrame = CFrame.new(4, 5, 0)
script.Parent.CFrame = CFrame.new(5, 5, 0)
script.Parent.CFrame = CFrame.new(6, 5, 0)
script.Parent.CFrame = CFrame.new(7, 5, 0)
script.Parent.CFrame = CFrame.new(8, 5, 0)
script.Parent.CFrame = CFrame.new(9, 5, 0)
script.Parent.CFrame = CFrame.new(10, 5, 0)
script.Parent.CFrame =CFrame.new(11, 5, 0)
script.Parent.CFrame = CFrame.new(12, 5, 0)
script.Parent.CFrame = CFrame.new(13, 5, 0)
script.Parent.CFrame = CFrame.new(14, 5, 0)
script.Parent.CFrame = CFrame.new(15, 5, 0)
script.Parent.CFrame = CFrame.new(16, 5, 0)
script.Parent.CFrame = CFrame.new(17, 5, 0)
script.Parent.CFrame = CFrame.new(18, 5, 0)
script.Parent.CFrame = CFrame.new(19, 5, 0)
script.Parent.CFrame = CFrame.new(20, 5, 0)
script.Parent.CFrame = CFrame.new(21, 5, 0)
script.Parent.CFrame = CFrame.new(22, 5, 0)
script.Parent.CFrame = CFrame.new(23, 5, 0)
script.Parent.CFrame = CFrame.new(24, 5, 0)
script.Parent.CFrame = CFrame.new(25, 5, 0)
script.Parent.CFrame = CFrame.new(26, 5, 0)
script.Parent.CFrame = CFrame.new(27, 5, 0)
script.Parent.CFrame = CFrame.new(28, 5, 0)
script.Parent.CFrame = CFrame.new(29, 5, 0)
script.Parent.CFrame = CFrame.new(30, 5, 0)

?

1 Like

No, use a for loop.

Code:

for i = 0, 30 do
	task.wait(1)
	script.Parent.CFrame = CFrame.new(i, 5, 0)
end

If you want it to move forward forever, use a while loop:

local counter = 0

while task.wait(1) do
	script.Parent.CFrame = CFrame.new(counter, 5, 0)
	counter += 1
end
1 Like

What is “i”?

I thought imaginary numbers don’t exist in programming.
(I have taken Algebra before. Here’s an article on Imaginary Numbers: Imaginary number - Wikipedia. )

i stands for index in this case, which refers to what iteration the loop is on. This has nothing to do with math.

1 Like

So would

local number = 0

for number ~= 31 do
number = number + 1
script.Parent.CFrame = CFrame.new(0, 5, 0)
script.Parent.CFrame = CFrame.new(1, 5, 0)
script.Parent.CFrame = CFrame.new(2, 5, 0)
script.Parent.CFrame = CFrame.new(3, 5, 0)
script.Parent.CFrame = CFrame.new(4, 5, 0)
script.Parent.CFrame = CFrame.new(5, 5, 0)
script.Parent.CFrame = CFrame.new(6, 5, 0)
script.Parent.CFrame = CFrame.new(7, 5, 0)
script.Parent.CFrame = CFrame.new(8, 5, 0)
script.Parent.CFrame = CFrame.new(9, 5, 0)
script.Parent.CFrame = CFrame.new(10, 5, 0)
script.Parent.CFrame =CFrame.new(11, 5, 0)
script.Parent.CFrame = CFrame.new(12, 5, 0)
script.Parent.CFrame = CFrame.new(13, 5, 0)
script.Parent.CFrame = CFrame.new(14, 5, 0)
script.Parent.CFrame = CFrame.new(15, 5, 0)
script.Parent.CFrame = CFrame.new(16, 5, 0)
script.Parent.CFrame = CFrame.new(17, 5, 0)
script.Parent.CFrame = CFrame.new(18, 5, 0)
script.Parent.CFrame = CFrame.new(19, 5, 0)
script.Parent.CFrame = CFrame.new(20, 5, 0)
script.Parent.CFrame = CFrame.new(21, 5, 0)
script.Parent.CFrame = CFrame.new(22, 5, 0)
script.Parent.CFrame = CFrame.new(23, 5, 0)
script.Parent.CFrame = CFrame.new(24, 5, 0)
script.Parent.CFrame = CFrame.new(25, 5, 0)
script.Parent.CFrame = CFrame.new(26, 5, 0)
script.Parent.CFrame = CFrame.new(27, 5, 0)
script.Parent.CFrame = CFrame.new(28, 5, 0)
script.Parent.CFrame = CFrame.new(29, 5, 0)
script.Parent.CFrame = CFrame.new(30, 5, 0)

work?

Use the loop I sent. Also, take a look at the link I sent.

But I haven’t defined “i” yet.

Would my computer read it as an imaginary number or am I okay and won’t accidentally break it?

Looking at this accounts other posts, this is just a troll thread. He knows what he’s doing. Just ignore this lol

1 Like

Yeah I can tell lol, was just trying to be nice.

I hit my head and forgot how to program.

This is the best approach to resolving your problem.

I’ve already explained why it’s wrong.

Please find another way around.

I found a solution online.

-- Function to calculate the length of the hypotenuse
function pythagoreanTheorem(a, b)
    local c = math.sqrt(a^2 + b^2)
    return c
end

-- Example usage
local side1 = 3
local side2 = 4

local hypotenuse = pythagoreanTheorem(side1, side2)

print("Length of the hypotenuse: " .. hypotenuse)

I’ll be testing it tomorrow. Thank you for the help.

1 Like

Regardless of this post being a troll or not it’s still possible that other people who need help solving a problem of
this complexity will need assistance in the future there’s no reason why this should be completely disregarded.

1 Like

Oh hell nah you with him too???

1 Like

What I’m sayin’.

I’m being for real though. I found the answer so please stop bumping the thread.

Language. There are children, please.

1 Like