How to break loop in this script

Hello everyone,

So I’m doing a little experiment for fun with an SCP elevator model I found in the toolbox and was tweaking the code a little little bit and almost got it to work like how I had it in mind but even though I added a script:Destroy() at the end of the script it won’t stop repeating everything.

The code:


I just want to know how can I break the loop so it doesn’t repeat everything again. I know it has to do with the while true do and the repeat in the beginning but when I was trying to remove them, I would get some errors and just break the script overall.

Footage of what things look like:


In my mind the doors are supposed to open and things are left at that but it just keeps on repeating seconds later. I’d appreciate any help and explanation too as it’s fairly simple but a bit confusing to me right now as I don’t really specialize in scripting!

1 Like

Use task.wait() instead of wait(), it is newer and is just more preferable.

You can use break to exit a loop. Also, instead of using a repeat loop, use TargetFloor:GetPropertyChangedSignal("Value"):Connect() and add in the rest of the code below it into that function.

3 Likes

Alright so I made the changes you suggested but now “until” gets underlined from these changes? I’m unsure if it should still be kept or if it’s still necessary or if I’m just placing things incorrectly now. Also the script is now making the elevator activate itself automatically rather than with the button being clicked though I had placed the remaining code into/under the “TargetFloor:GetPropertyChangedSignal(“Value”):Connect()” function as instructed. Like the script ends off as planned just that it activated by itself automatically.

New footage:

Adjusted code:


But yeah as you see, the script now ends as planned just that the elevator is activating itself automatically as soon as you spawn which is not what I was going for as said before, I’d appreciate a bit more explanation :slight_smile:

You need to connect with a function in the :Connect() line.

TargetFloor:GetPropertyChangedSignal("Value"):Connect(function()
    -- add the rest of the code here
end)
2 Likes

Yeah I’m aware now of adding the rest of the code below but does this include until in there too? It seems to be underlined when included it but if I were to remove it then it would underline TargetFloor.Value == 2 and so on causing more errors.

Should the last task.wait() also be in there or somewhere else? Sorry still having doubts on where these underlined things should go.

sigh
You don’t need an until, it’s not a for loop.

When I said “the rest of the code” I meant the entire block of code below that function. I can’t send you the function because I’m not writing all of it out.