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

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

Asked by 5 years ago

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?

0
It's crashing because you need a wait() in your loop. DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
-1
Answered by 5 years ago

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.

Ad

Answer this question