Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I add choices to my dialogue system?

I'm currently working on a dialogue system, and it works and is pretty smooth. But I wanna add choices like, Yes, No, Hi. Etc. It's all managed by a local script, and an NPC model with a folder called "Lines" That contains string values with the text I want it to have inside. I'll show the local script.

--//Services\\--
local StarterGui = game:GetService("StarterGui")
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 Skip = 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.Q then
		if Chatting == true then
			Skip = 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	
		if Detected == true then
			local Lines = DetectedNPC:FindFirstChild("Lines")
			if Lines then
				Sounds.Click:Play()
				
				Chatting = true
				Detected = false
				
				LineLabel.Main.Text = " "
				
				LineLabel.NPC_THINGS.NPC_NAME.Text = (DetectedNPC.Name)
				PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
				PromptLabel:TweenPosition(UDim2.new(0, 0, 0.8, 0), "In", "Linear", 0.2)
				LineLabel:TweenPosition(UDim2.new(-0.01, 0, 0, 0), "In", "Linear", 0.2)
				
				wait(0.5)
				
				for i, Line in pairs(Lines:GetChildren()) do
					local Text = Line.Value
					
					for i = 1, #Text do
						LineLabel.Main.Text = string.sub(Text, 1, i)
						Sounds.Talk:Play()
						if Skip == true then
							Skip = false
							LineLabel.Main.Text = Text
							break
						end
						if Exit ==  true then
							break
						end
						wait(0.07)
					end
					if Exit == true then
						Exit = false
						break
					end
					repeat wait() until Skip == true or Exit == true
					Skip = false
				end
				
				Exit = false
				Skip = false
				
				PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.3)
				LineLabel:TweenPosition(UDim2.new(-0.01, 0, 1.2, 0), "In", "Linear", 0.2)
				PromptLabel:TweenPosition(UDim2.new(0, 0, 1.2, 0), "In", "Linear", 0.2)
				
				wait(0.5)
				
				Chatting = false
				Detected = false
			end
		end
	end
end)

--//Main Loop\\--
RunService.RenderStepped:Connect(function()
	if Detected == false and Chatting == false then
		for i, NPC in pairs(NPCS:GetChildren()) do
			local Humanoid = NPC:FindFirstChild("Humanoid")
			local HMR = NPC:FindFirstChild("HumanoidRootPart")
			
			if Humanoid and HMR then
				if (HMR.Position - CharHMR.Position).magnitude < 15 then
					Detected = true
					DetectedNPC = NPC
					PromptLabel:TweenSize(UDim2.new(0, 60, 0, 60), "In", "Linear", 0.2)
					print(DetectedNPC.Name)
				end
			end
		end
	end
	
	if Detected == true and Chatting == false then
		local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
		local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
		
		if Humanoid and HMR then
			if (HMR.Position - CharHMR.Position).magnitude > 15 then
				Detected = false
				DetectedNPC = nil
				PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
				print("No longer detected NPC")
			else
				local WTSP = Camera:WorldToScreenPoint(HMR.Position)
				PromptLabel.Position = UDim2.new(0, WTSP.X, 0, WTSP.Y)
			end
		end
	end
	
	if Chatting == true then
		local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
		local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
		
		if Humanoid and HMR then
			Camera.CameraType = Enum.CameraType.Scriptable
			Camera.CFrame = Camera.CFrame:Lerp(HMR.CFrame * CFrame.new(-4, 4, -7) * CFrame.fromOrientation(math.rad(-12), math.rad(230), 0), 0.07)
		end
	else
		Camera.CameraType = Enum.CameraType.Custom
	end
end)

How could I add choices to this system? Could someone help me out real quick. I'm stuck.

1 answer

You don't have to write a single letter in a script to make a dialogue system with an npc. Just find the NPC in workspace and find it's head. Insert a dialog. Then click the dialog and go to your properties. Then there will be something that says Goodbye Prompt, in that you can write something like "Goodbye! "Im fine." "Sorry bye!". Then an Initial Prompt right under, in here you are going to write what the NPC is going to say when the player first wants to talk to it. Now insert a dialog choice inside of the Dialog and then in the properties of that, there is a user dialog and response dialog. The user dialog is one of the choices for the player to say and the response is what the bot says in response. If you want you can put another choice dialog in that choice dialog to add to the first response, and/or you can add another dialog choice inside of the normal dialog. That is what gives more choices. The Goodbye prompt will always appear as a choice though. Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.