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

Wait as a While loop condition??

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.

1 comment

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

1 answer

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.

5 comments

0

That does not work as I intend

0

I have edited it and i think i have answered your question.

0

You could just use BindableEvent:Wait()

0

-1. Too many comments, makes it harder to read. I can reconsider my downvote once the readability of the code is improved.

0

I have edited the code, I hope it is easier to read now sjr04Alt.

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