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