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

Wait as a While loop condition??

Asked by
Kap_K 0
6 years ago
Edited 6 years ago

Im so scared to try this:

while wait(3) do
end

Update: I want a loop to run for x amount of time, not delay running. I need it to run exactly x seconds before it becomes nil, and out of that timeframe it is nil.

0
Don't use wait() as a loop's condition. Read this wonderful document written by one of the scriptinghelpers discord server moderators: https://docs.google.com/document/d/1ieZ8OzXPiXe3R7rs_xWmYZOnhQevlagN0ZWwoj_zKP4/edit User#24403 69 — 6y

1 answer

Log in to vote
0
Answered by
Aznarog 54
6 years ago
Edited 6 years ago

To do this you IN ONE SERVER SCRIPT you will need a Bindable Event. If you are doing Server Script to Local Script (vice versa) you need to use Remote Event.

This is the code.

local BindableEvent = game.ReplicatedStorage.Event 
local length = 100 -- timer length
local done = false -- will be true once the timer is done.

local function timer()
    for time = length, 0, -1 do
        wait(1)
    end
    done = true
end

BindableEvent.Event:Connect(timer)

BindableEvent:Fire() -- tells the BindableEvent.Event to connect/start the timer.

repeat
    wait()
    -- your code here
until done

Don't forget to create a bindable event in Replicated Storage.

So whats going on here? well the bindable event allows you to run two loops at once essentially, i.e. it creates different script threads. It also allows you to talk between server scripts.

0
That does not work as I intend Kap_K 0 — 6y
0
I have edited it and i think i have answered your question. Aznarog 54 — 6y
0
You could just use BindableEvent:Wait() hiimgoodpack 2009 — 6y
0
-1. Too many comments, makes it harder to read. I can reconsider my downvote once the readability of the code is improved. User#24403 69 — 6y
0
I have edited the code, I hope it is easier to read now sjr04Alt. Aznarog 54 — 6y
Ad

Answer this question