Help on getting started with a "skip" button for my NPC dialogue system

I’m trying to create a NPC talking system, and I’m kind of stuck on the “skip” button. Everything I tried didn’t work out well.

I don’t really know how to get it to work.
I’m trying to make it so:
If text is typing, and button is clicked, skips to end (What I’m working on right now, and want help on)
If text is done, move to next page (What I’ll do later)

I’m trying to get the “skip to end” part to work right now, and I have no idea what to do.

I tried to make an if statement in my typewrite function, but that had a lot of issues. Could someone please help me get started? (especially if you have had experience making an NPC system)

Thank you!

Current Code (without failed skip button edits)
script.Parent.Enabled = false
local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
npcs = game.Workspace.NPCs
Dummy = npcs.Dummy
page = 1


local function typewrite(object,text)
	for i = 1,#text,1 do
		object.Text = string.sub(text,1,i)
		wait(0.0005)
	end
end

local function dummyText()
	script.Parent.Enabled = true
	typewrite(textlabel1,"This is a long long long long long line of random text.")
	typewrite(textlabel2,"This is a test for the 2nd line test test test.")
end


Dummy.Click.ClickDetector.MouseClick:Connect(dummyText)
-- I followed AlvinBlox's tutorial, to get the typewrite function

I’ve decided to make mine simpler, and make it just go to the next page.

1 Like

You can probably add a variable and if that variable is true then skip the entire text:

local skip = false
local function typewrite(object,text)
	for i = 1,#text,1 do
       if skip then
          object.Text = text
          break
       end
	   object.Text = string.sub(text,1,i)
	   wait(0.0005)
	end
end

What I would do (Keep in mind, this is just one approach. Not necessarily the best approach), is have a type of debounce in your function, or a switch. What I mean by that:

script.Parent.Enabled = false
local Skip = --your skip button here
local SkipVariable = false -- A variable to toggle skipping

local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
npcs = game.Workspace.NPCs
Dummy = npcs.Dummy
page = 1


local function typewrite(object,text)
	for i = 1,#text,1 do
         if SkipVariable ~= true then
		    object.Text = string.sub(text,1,i)
		     wait(0.0005)
          else
             SkipVariable = false -- set it back
            -- Set the text, if wanted
              object.Text = text
              break
         end
	end
end

local function dummyText()
	script.Parent.Enabled = true
	typewrite(textlabel1,"This is a long long long long long line of random text.")
	typewrite(textlabel2,"This is a test for the 2nd line test test test.")
end


Dummy.Click.ClickDetector.MouseClick:Connect(dummyText)
-- I followed AlvinBlox's tutorial, to get the typewrite function

--Handle skip click
Skip.MouseButton1Click:Connect(function()
      SkipVariable = true
end)

This is what @DEVLocalPlayer was saying to do, but I was already typing, and didn’t feel like scrapping it. I thought it might be helpful for you to see it incorporated into your code

1 Like

It actually works, but it’s a bit off. I’m not exactly sure why.

When I click on NPC (didn’t click on the UI),
it prints “worked” when I just clicked on the NPC, so it shows like this as soon as I click:

I don’t really understand how it runs when the NPC is just clicked.

Code, if anyone needs it

script.Parent.Enabled = false
local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
npcs = game.Workspace.NPCs
Dummy = npcs.Dummy
page = 1
skip = false

proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
local function skip()
	skip = true

end

proceed.MouseButton1Click:Connect(skip)
local function typewrite(object,text)
	for i = 1,#text,1 do
		if skip then
			object.Text = text
			print("worked!")
			skip = false
			break
		end
		object.Text = string.sub(text,1,i)
		wait(0.0005)
	end
end

local function dummyText()
	script.Parent.Enabled = true
	typewrite(textlabel1,"This is a long long long long long line of random text.")
	typewrite(textlabel2,"This is a test for the 2nd line test test test.")
end


Dummy.Click.ClickDetector.MouseClick:Connect(dummyText)

Oh you want it to be faster or immediate? In that script I made it immediately finish.

I wanted mine to immediately finish, however my issue was on the 1st click, it made the skip function run somehow, without clicking the the UI button.

Correct me if I’m wrong but I think you gotta run it inside a separate thread.

What do you mean? Separate thread as in Developer Forum post, or what?

I meant that as in if you wanted an immediate response you would have to utilize threads, as in coroutines or spawn.

I have a couple of questions:

  • I don’t understand what you mean by “skip to the end” in your initial post.
  • Why are you using 2 text labels?
  • Why not make an array that stores strings which you want the npc to read out and just loop through the array?

I have no idea how to use coroutines or spawn, is there another easier way to do it?

I meant skip to the end as in when clicked, skip to the end of the text, instead of doing the typewrite.

Is there a better way? I’m using it so that I can easily decide what letters go where.

Not advanced enough to do that… that would take a long time to get it working. :sweat_smile: