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

Looking for something to replace wait()?

Asked by
Aozwel 71
4 years ago
Edited 4 years ago

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,

0
debounce. local deb = while true do return end iiDkOffical 109 — 4y
0
ohyeah otherwise idk iiDkOffical 109 — 4y
0
How would i apply this to say the likes of, When a player touches a part the script would carry on? Aozwel 71 — 4y
0
delay(1, function()) end) namespace25 594 — 4y
View all comments (5 more)
0
suspectshot108, Were would i put that inside of to trigger and how do i call on it to carry on my Scripts? Aozwel 71 — 4y
1
you could also fire a bindable/remote event when a certain criteria is met theking48989987 2147 — 4y
0
theking48989987, What would that do exactly? Sorry im super new Aozwel 71 — 4y
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) whenallthepigsfly 541 — 4y
0
Done. Sorry if its alot to look at Aozwel 71 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
@whenallthepigsfly Dang that looks pretty advanced id have to look into that, But thanks again for your time! Aozwel 71 — 4y
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. Aozwel 71 — 4y
Ad

Answer this question