[SOLVED] "while true do" not working as expected

Hello, im having a issue with lasers lerp loop here is my code, but when the laser tween the first time, it automatically stops and don’t continue the loop!

local part1 = game:GetService(“Workspace”).Bank.Lasers.Laser1.Part1

local part2 = game:GetService(“Workspace”).Bank.Lasers.Laser1.Part2

local endPart = game:GetService(“Workspace”).Bank.Lasers.Laser1.End

local otherPart = game:GetService(“Workspace”).Bank.Lasers.Laser1.OtherEnd

while true do

wait()

for i = 0,2,.02 do

wait()

part1.CFrame = part1.CFrame:Lerp(endPart.CFrame, i) – part one to end

wait()

part2.CFrame = part2.CFrame:Lerp(otherPart.CFrame, i) – part two to end

end

end

Can I see the output error? if there is one of course *

use the coding format when posting please

There is no error on the output it simply don’t want to make the loop see: robloxapp-20210427-1858087.wmv (255.4 KB)

try this:

local part1 = game:GetService(“Workspace”).Bank.Lasers.Laser1.Part1
local part2 = game:GetService(“Workspace”).Bank.Lasers.Laser1.Part2
local endPart = game:GetService(“Workspace”).Bank.Lasers.Laser1.End
local otherPart = game:GetService(“Workspace”).Bank.Lasers.Laser1.OtherEnd
local tweenService=game:GetService("TweenService")
info=TweenInfo.new(1, "Linear", "In", -1, true, 2)
goalendpart={
  goal.CFrame=endPart.CFrame
}
goalstartpart={
  goal.CFrame=otherPart.CFrame
}
tween1=tweenService:Create(part1,info,goalendpart)

tween2=tweenService:Create(part2,info,goalstartpart)
while wait() do
tween1:Play()
tween2:Play()
end

i will try this! thanks can you try send me with “CFrame:Lerp”? it looks more smooth i will try this in a second :wink:

this is an script error how to fix it?

local part1 = game.Workspace.Bank.Lasers.Laser1.Part1

local part2 = game.Workspace.Bank.Lasers.Laser1.Part2

local endPart = game.Workspace.Bank.Lasers.Laser1.End

local otherPart = game.Workspace.Bank.Lasers.Laser1.OtherEnd

local tweenService=game:GetService("TweenService")
info=TweenInfo.new(1, "Linear", "In", math.huge, true, 2)
goalendpart={
	CFrame=endPart.CFrame
}
goalstartpart={
	CFrame=otherPart.CFrame
}
tween1=tweenService:Create(part1,info,goalendpart)

tween2=tweenService:Create(part2,info,goalstartpart)
while wait() do
	tween1:Play()
	tween2:Play()
end

line 10 error out put error

1 Like

wait a sec______________________

Hey,

try changing “for i = 0,2,.02 do” to “for i = 0,1,.02 do”

You have the end target of i set to 2, but the Lerp() function you are using is capped from 0 to 1. Which means, once the tween finishes, it waits a long time before it restarts.

Looping for that script still not working ;(

local part1 = game.Workspace.Bank.Lasers.Laser1.Part1

local part2 = game.Workspace.Bank.Lasers.Laser1.Part2

local endPart = game.Workspace.Bank.Lasers.Laser1.End

local otherPart = game.Workspace.Bank.Lasers.Laser1.OtherEnd
while wait() do
for i = 0,2,.02 do
	wait()
	part1.CFrame = part1.CFrame:Lerp(endPart.CFrame, i) 
	part2.CFrame = part2.CFrame:Lerp(otherPart.CFrame, i) 
	end
	for i = 0,2,.02 do
		wait()
		part1.CFrame = part1.CFrame:Lerp(otherPart.CFrame, i) 
		part2.CFrame = part2.CFrame:Lerp(endPart.CFrame, i) 
	end


end

this sohuld work

2 Likes

Thanks mate! Its working , have a nice day!

1 Like

I just looked at your code again, and realized why it wasn’t looping. The code is actually looping just fine, in fact if you stuck a print() in the loop you will see that after it plays the first time, it does actually repeat. However, I want you to look at something.

part1.CFrame = part1.CFrame:Lerp(endPart.CFrame, i) – part one to end

Look at this line. What you are doing is taking the current CFrame of part1, lerping that to your endPart CFrame, and setting that value as part1’s new CFrame. This means that when your animation finishes, part1’s CFrame is equal to endPart.CFrame. Then, when you restart this loop, you have nothing in your code that resets part1’s CFrame back to whatever the start is.

So to fix this, you need to create a second part that indicates where the start of the animation should be, which would mean that when the loop plays a second time, instead of taking the lerp of part1’s cframe (which is already at the endpart cframe), you are taking the lerp of the start CFrame and end CFrame.

Here’s a cleaned up version of your script: (note that you need to create two additional parts inside your model, one called Start and another called OtherStart)

local laser1 = workspace.Bank.Lasers.Laser1
–[[You do not need to use GetService() to grab workspace, instead you can simply type workspace and it will be there for you. This only works for workspace though, not other services.
Also, creating a variable for your laser is probably a good idea for keeping your code easier to read and edit.]]

local part1 = laser1.Part1
local part2 = laser1.Part2

local startPart = laser1.Start
local otherStartPart = laser1.OtherStart

local endPart = laser1.End
local otherEndPart = laser1.OtherEnd

while true do

for i = 0, 1, 0.02 do --Remember that the ending value of i should be 1, not 2.
part1.CFrame = startPart.CFrame:Lerp(endPart.CFrame, i) – part one to end
part2.CFrame = otherStartPart.CFrame:Lerp(otherEndPart.CFrame, i) – part two to end
wait() --You really only need one wait() here, no need to double up or anything.
end

end

[code ended]
Now the other script someone replied here is perfectly good; in fact, TweenService is probably the better thing to use here, since it lets you tween other values like color and transparency. But I just felt like I owe you a corrected version of your original script, since I somehow missed the issue initially. This should be helpful for learning what went wrong!

2 Likes

Here it goes a heart! it in fact works thank you guys so much for that community <3 i used the one before because its a bit simple! im learning alot about lerping

1 Like