I'm making an NPC dialog system for my game that display's the NPC's name, dialog, and confirmation arrow when you click on their model. I have it just about about working, but a strange problem I'm running into is that if you talk to the NPC more than once, it starts running again even though it should be finished. Here's a screenshot of the output (currently I have it printing the Dialog followed by the value of currentPage.
Notice how at some points, it doesn't even print the dialog, it just prints straight to the next iteration of currentPage. I'm having a brain fart and can't quite track down the logic responsible for this.
As a side note just in case, you click on the "next" button to get to the next page dialog, not the NPC itself.
Code below:
local db = 0
local dee = script.Parent.Parent
local npcName = "Dee"
local currentPage = 0
local dialog = {"Did you hear? Someone took the owner of Rocky Bay's statue.",
"Everyone has been saying it was a squid. But what would a squid want with a statue?"
}
function clicked(player)
if db == 1 then return end
db = 1
local gui = player.PlayerGui.NPCD
local speech = gui.sBox
local id = gui.name.n
local close = gui.Close
local n = gui.next
id.Text = npcName
local function enableNext()
n.Visible = true
end
local function disableNext()
n.Visible = false
end
local function makeVisible() --MAKE THE DIALOG BOX VISIBLE
for i, v in pairs(gui:GetChildren()) do
v.Visible = true
end
end
local function makeInvisible() --MAKE THE DIALOG BOX INVISIBLE
for i, v in pairs(gui:GetChildren()) do
v.Visible = false
end
end
local function talk()
disableNext()
speech.s.Text = ""
print(dialog[currentPage])
for x = 1, string.len(dialog[currentPage]) do
speech.s.Text = speech.s.Text .. dialog[currentPage].sub(dialog[currentPage],x,x)
wait(0.01)
end
enableNext()
end
local function nextPage()
print(currentPage)
if currentPage ~= #dialog then
currentPage = currentPage + 1
talk()
else
makeInvisible()
currentPage = 1
disableNext()
db = 0
end
end
n.MouseButton1Down:connect(nextPage)
makeVisible()
currentPage = 1
talk()
end
dee.click.MouseClick:connect(clicked)
Any help is appreciated, thank you!!
