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

Looking for something to replace wait()?

Hey there, new scripter here.

I'm making a short story based game similar to the likes of Camping ect,

And was wondering if there is a way to "Pause" a gui/code from carrying on until i manually call for it?

Right now im just using wait()'s and putting in random seconds but i need something more reliable?

Once these 3 strings are finished i want it to be "Paused" until i Touch a part which will carry on the script when i add more Strings to it later

  -- Roblox services
local dialogGui = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local object = script.Parent
 
-- Require module
local TypeWriter = require(ReplicatedStorage:WaitForChild("TypeWriter"))
 	wait(0.1)
TypeWriter.typeWrite(script.Parent, "Welcome students, I hope you enjoy your stay!")
	wait(1)
	TypeWriter.typeWrite(script.Parent, "Why don’t you get started by heading upstairs for some classes")
	wait(1)
	TypeWriter.typeWrite(script.Parent, "It’s the first door on the left")
	wait(1)
	
	
object.AnchorPoint = Vector2.new(0, 0)
object.Position = UDim2.new{0, 0},{0, 0}
 
 
object:TweenPosition(UDim2.new(0, 0, -10.04, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine,0.5)

wait(2)

script.parent.Visible = false
	if script.parent.visible == false then print("Worked!")
		
	end

Thanks,

9 comments

0

debounce. local deb = while true do return end

0

ohyeah otherwise idk

0

How would i apply this to say the likes of, When a player touches a part the script would carry on?

0

delay(1, function()) end)

0

suspectshot108, Were would i put that inside of to trigger and how do i call on it to carry on my Scripts?

1

you could also fire a bindable/remote event when a certain criteria is met

0

theking48989987, What would that do exactly? Sorry im super new

0

There are simple answers and complex answers on how to do this - it really depends on the context. Can you post the code you want to improve? (You can edit your post)

0

Done. Sorry if its alot to look at

1 answer

✓ Accepted answer

Doing what you want is a bit more complicated than it would seem.

To accomplish it, you need to use coroutines. All these are are ways to run different tasks/code at the same time, even if one stops to wait for something. Here's the gist of what you want to do:

local myCoroutine = coroutine.running()
local brick = script.Parent

function continueColorChange()
	coroutine.resume(myCoroutine)
end
brick.Touched:Connect(continueColorChange)

--Things below here will be paused--
coroutine.yield()
brick.Color = Color3.new(1,0,0)
coroutine.yield()
brick.Color = Color3.new(0,1,0)
coroutine.yield()
brick.Color = Color3.new(0,0,1)

This script, when parented to a brick, will incrementally change its colors to the ones we set every time it is touched, pausing in between each color change. It's a different outcome but the same concept you want to achieve. Let's go through it. The first thing we do is get the entire script's coroutine. This coroutine will allow us to control when the script is running and when it isn't.

local myThread = coroutine.running()

Then, we link an event to resume the script when something happens that we want to restart the script once it is paused (In this case, touching the brick). coroutine.resume() restarts the main script's coroutine "myCoroutine" after it is paused.

function continueColorChange()
	coroutine.resume(myThread)
end
brick.Touched:Connect(continueColorChange)

Finally, we get to the part that will be executed sequentially. Everytime "coroutine.yeild()" is called, it pauses execution of the script until coroutine.resume() is called - in this case by the event linked to the brick touch event.

coroutine.yield()
brick.Color = Color3.new(1,0,0)
coroutine.yield()
brick.Color = Color3.new(0,1,0)
coroutine.yield()
brick.Color = Color3.new(0,0,1)

If you want some more examples and explinations, you can look at the coroutine tutorial here.

2 comments

0

@whenallthepigsfly Dang that looks pretty advanced id have to look into that, But thanks again for your time!

0

Seemed to have got it figured out, Seems as if it acts like a Remote controlling other bricks when pressed which is exactly what im looking for, However now to make it work with what i need is gonna be the hard part.

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