How would I get detect a click with dialog?

So I have an npc, which goes around, waiting for someone to click his overhead dialog box. When this dialog box is clicked, I want the player to disappear. The thing I can’t figure out is how can I detect the click from the dialog box. I have heard of Dialog:DialogChoiceSelected, but it doesn’t seem to fit in since my problem is just the dialog, not the choices.

1 Like

Well, there are no events that I know of that can connect to the Dialog being initiated.

The only way I can think of is using BillboardGui’s and make a Pseudo-Dialog that somehow acts like dialog, but also includes events like PlayerBeganDialog.

Or you could post a feature request to make this event.

(If you don’t want to initiate any dialog though, you could just make a button appear over their head and connect whatever to MouseButton1Click)

1 Like

A quick check going through:
https://developer.roblox.com/en-us/api-reference/class/Dialog
https://developer.roblox.com/en-us/api-reference/class/DialogChoice

There are no events that connect to that, unfortunately. Therefore, you should do a workaround as aforementioned.

You could use Dialog:GetCurrentPlayers() on a loop to check which players are using the dialog.

example loop:

while true do -- serverscript
wait(0.2) -- not neccessary to run the loop every frame, wait(0.2) is fine
local players = dialog:GetCurrentPlayers() -- get all the players using the dialog
if players ~= nil then -- if any players are using the dialog
     for i,v in pairs(players) do -- teleport all players using the dialog
        --teleport player
     end
  end
end
2 Likes

The server doesn’t pickup DialogChoiceSelected because of FilteringEnabled I believe. You have to make a seperate RemoteEvent in your dialog and use the client to detect DialogChoiceSelected and have it’s choice selected fire into the server. Example

--local script
local TheDialog = YourDialogThingy
TheDialog.DialogChoiceSelected:connect(function(Player, Choice)
	TheDialog:WaitForChild("DialogEvent"):FireServer(Choice)
end)
---Server Script
local TheDialog = YourDialogThingy
TheDialog.DialogEvent:connect(function(Player, Choice)
   if Choice == "Chicken" then
       print("Give chicken")
   end
end)

Personally, I would make a custom dialog then use the one on ROBLOX, or you can use a ClickDetector or SurfaceGui.

3 Likes

There is a bug in the previous server side code. It should be:

---Server Script
local TheDialog = YourDialogThingy
TheDialog.DialogEvent.OnServerEvent:connect(function(Player, Choice)
   if Choice == "Chicken" then
       print("Give chicken")
   end
end)

This could have been because of more recent changes by Roblox, but I just wanted to fix in the event that someone tries to use the code in the year 2022 or greater!!

2 Likes