I have a dialogue script that allows the player to press E on an NPC and start a conversation. Pressing X makes the dialogue progress to the next sentence, and Z exits the conversation.
I have a for loop that's supposed to loop through each sentence that the NPC says, and output each letter, one by one. It's having issues though. For some reason, after exiting the conversation, it tries to output the very first sentence, even though the player has exited the conversation. Another problem I noticed, is that the dialogue lines will be in the wrong order sometimes when you re-initiate the conversation with the NPC.
I'd appreciate any help with the above two problems.
Here's a video of the script in action, and the bugs I mentioned: https://www.youtube.com/watch?v=30WUfX3blEQ
Below is the code portion that is relevant. The for loops start around line 91 and goes to line 119 in the UserInputService function.
--// Services
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
--// Main Variables
local DetectedNPC = nil
local Detected = false
local Chatting = false
local Next = false
local Exit = false
--// Player
local Player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local Gui = script.Parent
local Sounds = Gui.Sounds
local PromptLabel = Gui.PromptLabel
local LineLabel = Gui.LineLabel
--// Character
local Character = Player.Character or Player.CharacterAdded:Wait()
local CharHMR = Character:WaitForChild("HumanoidRootPart")
--// NPC
local NPCS = game.Workspace:WaitForChild("NPCS")
--// Functions
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.X then
if Chatting == true then
Next = true
Sounds.Click:Play()
end
end
end)
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Z then
if Chatting == true then
Exit = true
Sounds.Click:Play()
end
end
end)
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.E then
print("E pressed")
if Detected == true then
local Lines = DetectedNPC:FindFirstChild("Lines")
local npcName = DetectedNPC.Name
LineLabel.npcname.Text = npcName --Displays NPC's name in dialogue GUI
local npcImage = DetectedNPC.NPCImage.ImageLabel.Image --NPC's image that will be displayed
LineLabel.ImageLabel.Image = npcImage
LineLabel.ImageLabel.Visible = true
if Lines then
Sounds.Click:Play()
Chatting = true
Detected = false
LineLabel.Text = " "
PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
LineLabel:TweenPosition(UDim2.new(0, 0, 0.75, 0), "In", "Linear", 0.2)
wait(0.5)
end
for i, Line in pairs(Lines:GetChildren()) do --DON'T FORGET TO REMOVE TEST PRINTS
local Text1 = Lines.Line1.Value --Line1 StringValue
local Text2 = Lines.Line2.Value --Line2 StringValue
local Text3 = Lines.Line3.Value --Line3 StringValue
local sentenceArray = {Text1,Text2,Text3}
for j = 1, #sentenceArray do
print("lines: " .. #sentenceArray)
for i = 1, #sentenceArray[j] do --
LineLabel.Text = string.sub(sentenceArray[j], 1, i)
print(LineLabel.Text)
Sounds.Talk:Play()
if Next == true then
Next = false
LineLabel.Text = sentenceArray[j]
break
end
if Exit == true then
LineLabel.ImageLabel.Visible = false
break
end
wait(0.07)
end
if Exit == true then --Pause & wait
Exit = false
break
end
repeat wait() until Next == true or Exit == true
Next = false
end
Exit = false
Next = false
LineLabel.ImageLabel.Visible = false
PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
LineLabel:TweenPosition(UDim2.new(0, 0, 1.2, 0), "Out", "Linear", 0.2)
wait(0.5)
Chatting = false
Detected = false
end
end
end
end)

2 comments
Yea, I kinda realized that for this particular code problem. I've posted the same question over on robloxforum.com, but haven't got any responses either. I've been trying my best to keep troubleshooting it myself by rebuilding some of the code. Not much luck so far.
Well good luck.