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

Repeat until isn't working properly?

Asked by 5 years ago

I've created a repeat until loop that does technically work, but the repeat never stops. Here's my script:

posi = -31.7
posi2 = 4.81
posi3 = -57.215

repeat
    wait(0.1)
    posi = posi + 1
    game.Workspace.Part.Position = Vector3.new(posi, posi2, posi3)
until posi == 40.7

It's supposed to be a sliding door.. my only problem is that it keeps on sliding, as I wrote, it never stopped. It was meant to stop at the value of 40.7 (posi)- when I looked at the properties of the part, it went waaayyyy above 40, up to the 70s.

There was no error messages.

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

If you want the door to slide on its own without a trigger and then stop when it reaches the end, this could possibly work.

1   posi = -31.7
2   posi2 = 4.81
3   posi3 = -57.215
4    
5   while true do
7       if posi < 40.7 then
8       wait(0.1)
9       posi = posi + 1
10      game.Workspace.Part.Position = Vector3.new(posi, posi2, posi3)
12      end
11  end
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Instead of using while, repeat loops, use for i and lerp.

It's simple. Duplicate the door part two times and name the first one StartPart and the second FinishPart. You should have three door parts but all different names. Position the StartPart and the FinishPart. (don't forget to make the duplicate parts invisible and cancollide false)

local DoorPart = workspace.Door --(change your door part's name to 'Door')
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart

Now that you set the variables, get both start and finish parts CFrame.

local DoorPart = workspace.Door --(change your door part's name to 'Door')
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart

local StartPos, FinishPos = StartPart.CFrame, FinishPos.CFrame

Next, create a function and use the for i loop.

local DoorPart = workspace.Door --(change your door part's name to 'Door')
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart

local StartPos, FinishPos = StartPart.CFrame, FinishPos.CFrame

local Tick = .1 --higher value will make the door transition look rough, really low values take too long. Suggest keeping this  greater than .1 and less than .3

function openDoor()
    for i = 0, 1, Tick do 
        DoorPart.CFrame = DoorPart.CFrame:lerp(FinishPos, i) --transitions the door part to the finish part's cframe
        wait() --don't add a number in wait() it makes things look choppy
    end
    DoorPart.CFrame = FinishPos
end

function closeDoor()
    for i = 0, 1, Tick do 
        DoorPart.CFrame = DoorPart.CFrame:lerp(StartPos, i) 
        wait() 
    end
    DoorPart.CFrame = StartPos
end

Add a click detector in the door to fire the functions

local DoorPart = workspace.Door --(change your door part's name to 'Door')
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart

local StartPos, FinishPos = StartPart.CFrame, FinishPos.CFrame

local Tick = .1 --higher value will make the door transition look rough, really low values take too long. Suggest keeping this  greater than .1 and less than .3

function openDoor()
    for i = 0, 1, Tick do 
        DoorPart.CFrame = DoorPart.CFrame:lerp(FinishPos, i) --transitions the door part to the finish part's cframe
        wait() --don't add a number in wait() it makes things look choppy
    end
    DoorPart.CFrame = FinishPos
end

function closeDoor()
    for i = 0, 1, Tick do 
        DoorPart.CFrame = DoorPart.CFrame:lerp(StartPos, i) 
        wait() 
    end
    DoorPart.CFrame = StartPos
end

local IsOpen = false --determines if the door is open or not
local ClickDetector = Instance.new("ClickDetector", DoorPart)

ClickDetector.MouseClick:connect(function()
    if IsOpen == false then 
        print("opening door")
        openDoor()
        IsOpen = true 
    else
        print("closing door")
        closeDoor()
        IsOpen = false 
    end
end)

Answer this question