How do I perform a function when a dialog choice is selected?

I’m trying to make it so whenever you select a certain dialog choice it starts a quest but for some reason it doesn’t work.

Local script in StarterPlayerScripts:

local dialogBox = workspace:WaitForChild("QuestZombie"):FindFirstChild("Dialog"):FindFirstChild("DialogChoice")
local toggleQuest = game.ReplicatedStorage:WaitForChild("QuestConfigs"):WaitForChild("BrainHunt"):WaitForChild("ToggleQuest")
local toggled = game.ReplicatedStorage:WaitForChild("QuestConfigs"):WaitForChild("BrainHunt"):FindFirstChild("InProgress")

dialogBox.DialogChoiceSelected:Connect(function(choice)
	if choice == "How many should I collect?" and toggled.Value == false then
		toggleQuest:FireServer()
	end
end)```

Script in ServerScriptService:
```lua
local repStor = game:GetService("ReplicatedStorage")

local questConfigs = repStor:WaitForChild("QuestConfigs")

local brainHunt = questConfigs:WaitForChild("BrainHunt")

brainHunt.ToggleQuest.OnServerEvent:Connect(function(player)

brainHunt:WaitForChild("InProgress").Value = true

brainHunt:WaitForChild("EnableUIS"):FireClient()

end)```
3 Likes

Can’t really create an entire script right now, but I did look at a dialog tutorial by roblox: Designing a Dialog Tree

1 Like

Thank you for this but it isn’t quite what I’m looking for.

Oh, realized you were looking at how to perform a function when a dialog choice is selected. My bad. Are there any errors? Also, a better way to do this is by passing a parameter when firing the server, and the server can read the value.

1 Like

You should take a look at the documentation for it, because it doesn’t pass any string arguments.

It instead passes the player and the dialogue choice instance. I suggest you always check the documentation before you ask here, because you’ll get quicker answers and usually with better examples.

4 Likes
This post was heavily updated to ensure a well-guided and updated approach on using the DialogChoiceSelected event.

Well… :clipboard:

The core issue in your script is the inadequate DialogChoiceSelected event utilization. This method is only present within the Dialog instance, meaning that it’s non-existent to the DialogChoice. Below is a step-by-step guidance on how to properly use the event, following the Roblox’s official documentation.


Application guide.

This guide overviews a standard approach on building and programming the event properly. You can also opt for a more complex approach by using a ModuleScript and setting up the events and quests in it, calling it when necessary.

  1. First thing you should do is add separate lines for calling Roblox-related events.
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
  1. Next up, rewrite the way you call your variables.
local zombieQuest = Workspace:WaitForChild("QuestZombie")
local questDialogBox = zombieQuest:FindFirstChild("Dialog")
local triggerQuestChoice = questDialogBox:FindFirstChild("DialogChoice")

local questConfigs = ReplicatedStorage:WaitForChild("QuestConfigs")
local brainHuntQuest = questConfigs:WaitForChild("BrainHunt")
local questToggle = brainHuntQuest:WaitForChild("ToggleQuest")
local questToggled = brainHuntQuest:WaitForChild("InProgress")
  1. Next off, I’ll be remaking the function you’re using to call the zombie quest and fire the server in order to activate it.
questDialogBox.DialogChoiceSelected:Connect(function(player, dialogChoice)
	local targetUserDialog = "How many should I collect?" -- The required user dialog.
	if player then -- Checks if the player is an existent instance.
		if dialogChoice.UserDialog == triggerQuestChoice.UserDialog and triggerQuestChoice.UserDialog == targetUserDialog and not questToggle.Value then
			-- Creates a comparison based on the selected user dialog and the target user dialog, if the selected user dialog matches the targetUserDialog, and if the questToggle.Value is set to false.
			questToggle:FireServer() -- Fires the server.
		else
			print("Prompted DialogChoice does not fits the one or more requirements based on the event connection conditional.") -- Debugging.
		end
	else
		print("Player is a non-existent instance.") -- Debugging,
	end
end)

Code explanation and review.

The code above starts off by rewriting the way you call Roblox-related services, designs the necessary variables, and then begins with the code itself. It starts off by setting the required user dialog to start off with the quest, runs a conditional check to see if the player is an existent instance, and then another conditional check. It makes sure that both user dialogs pair with one another, if the triggerQuestChoice.UserDialog matches with the targetUserDialog, and if the value of the questToggle is false. If all these requirements are met, the questToggle event will fire the server.


Full code.

local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local zombieQuest = Workspace:WaitForChild("QuestZombie")
local questDialogBox = zombieQuest:FindFirstChild("Dialog")
local triggerQuestChoice = questDialogBox:FindFirstChild("DialogChoice")

local questConfigs = ReplicatedStorage:WaitForChild("QuestConfigs")
local brainHuntQuest = questConfigs:WaitForChild("BrainHunt")
local questToggle = brainHuntQuest:WaitForChild("ToggleQuest")
local questToggled = brainHuntQuest:WaitForChild("InProgress")

questDialogBox.DialogChoiceSelected:Connect(function(player, dialogChoice)
	local targetUserDialog = "How many should I collect?" -- The required user dialog.
	if player then -- Checks if the player is an existent instance.
		if dialogChoice.UserDialog == triggerQuestChoice.UserDialog and triggerQuestChoice.UserDialog == targetUserDialog and not questToggle.Value then
			-- Creates a comparison based on the selected user dialog and the target user dialog, if the selected user dialog matches the targetUserDialog, and if the questToggle.Value is set to false.
			questToggle:FireServer() -- Fires the server.
		else
			print("Prompted DialogChoice does not fits the one or more requirements based on the event connection conditional.") -- Debugging.
		end
	else
		print("Player is a non-existent instance.") -- Debugging,
	end
end)

Glad to assist, and hope it helped! :wink:

4 Likes