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

How do I keep a loop running without using "while true do"?

Here is a weld script:

while true do
	local Model = script.Parent.Parent
local Other = Model:WaitForChild("SuitcasePlacePosition")
local Main = Model:WaitForChild("Suitcase")
	
local Weld = Instance.new("Weld", Main)
Weld.C0 = Main.CFrame:inverse() * Other.CFrame
Weld.Part0 = Main
Weld.Part1 = Other
Weld.Name = "SuitcaseWeld"
end

It works, but when I destroy the weld and do it again in the same game it will not work as it has already been through the script. When I start with while true do, however, it simply crashes studio when I try to weld it at the start. How do I fix it?

1 comment

0

It's crashing because you need a wait() in your loop.

1 answer

While true do loops require a wait(), I usually place the wait() after everything, basically before the end.

while true do
    local Model = script.Parent.Parent
local Other = Model:WaitForChild("SuitcasePlacePosition")
local Main = Model:WaitForChild("Suitcase")

local Weld = Instance.new("Weld", Main)
Weld.C0 = Main.CFrame:inverse() * Other.CFrame
Weld.Part0 = Main
Weld.Part1 = Other
Weld.Name = "SuitcaseWeld"
wait() --See?
end

You can change the wait to any number you want, but the number in the brackets is how long it will wait until looping again.

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