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)
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