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

How do you stop a wait() while it's running?

I am trying to make an NPC dialogue, but I do not want to use the Studio's dialogue, so I am trying to make my own, but when you click "bye" before it's done talking, and then click on the NPC to talk again, what it was saying before still goes on. I want it so that it stops what the NPC is saying when you click "Bye", only problem is I am not sure how to stop a wait(), and I've tried using a function() return line but that doesn't work. Note: this is a localscript. Please excuse the messy and long script

local plr = game.Players.LocalPlayer
local gui = plr.PlayerGui.NPCDialogue

workspace.NPC.ClickDetector.MouseClick:Connect(function()
	local X = false
	gui.Frame.Visible = true

---- the talk "animation" -----
local function chat()
	gui.Frame.TextLabel.Text= "H" wait(0.01) gui.Frame.TextLabel.Text= "He" wait(0.01)  gui.Frame.TextLabel.Text= "Hell" wait(0.01) gui.Frame.TextLabel.Text= "Hello" wait(0.01) gui.Frame.TextLabel.Text= "Hello," wait(.5) gui.Frame.TextLabel.Text= "Hello, t" wait(0.01) gui.Frame.TextLabel.Text= "Hello, tr" wait(0.01) gui.Frame.TextLabel.Text= "Hello, tra" wait(0.01) gui.Frame.TextLabel.Text= "Hello, trav" wait(0.01) gui.Frame.TextLabel.Text= "Hello, trave" wait(0.01) gui.Frame.TextLabel.Text= "Hello, travel" wait(0.01) gui.Frame.TextLabel.Text= "Hello, travell" wait(0.01)gui.Frame.TextLabel.Text= "Hello, travelle" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller," wait(0.5) gui.Frame.TextLabel.Text= "Hello, traveller, w" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, wo" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, wou" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, woul" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would y" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would yo" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you l" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you li" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you lik" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like t" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to t" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to tr" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try o" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try ou" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our" wait(0.01)gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our s" wait(0.01)gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our sp" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our spe" wait(0.01)gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our spec" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our speci" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specia" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our special" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specialt" wait(0.01)gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specialty" wait(0.01)gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specialty?" wait(0.01)gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specialty? (" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specialty? (5" wait(0.01) gui.Frame.TextLabel.Text= "Hello, traveller, would you like to try our specialty? (5)"
end

if X == false then
chat()
else 
chat()
return end

	gui.Frame.Option3.MouseButton1Click:Connect(function() -- the "bye" button
		X = true
		gui.Frame.Visible = false
		wait(0.01)
		X = false
	end)

5 comments

0

Holy tomato bread you should use a for loop to cut this down.

0

yes Holy tomato...use string.sub (in a loop) to cut down on manual typing https://developer.roblox.com/en-us/api-reference/lua-docs/string

0

oh.. my.. god... what's this ugly mess >.<

0

HOLY CANNED ROBLOXIAN this is a crazy bit of code

1 answer

You can make your chat animation less messy by printing the message with a loop.

The following loop will run while the number i is less than the number of characters in the messageString and continueMessage is false. If either one of these conditions are not met, the loop will not run.

What's happening inside of the loop is we are getting the string from 0 to what i currently is, making that our string text, adding one to i and then waiting 1.

continueMessage = true
messageString = "Hello, traveller, would you like to try our specialty? "
i = 0
while(i < #messageString and continueMessage)do
	    local partialString = string.sub(messageString, 0, i) 
	    gui.Frame.TextLabel.Text= partialString
	    i = i + 1
	    wait(.1)
end

1 comment

0

Hi, you've been online since I last posted, but you seem to not have the question answered. Do you still need this answer to be answered or does the code above work?

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