How to activate a script when a dialogue is opened?

I’d like to set a camera position when the player clicks on the dialogue button over the npcs head.
Is there a script I can put inside the dialogue that lets me activate a script when its clicked? Thanks.

1 Like

Are you talking about the Regular Dialog or a Custom one? Just asking

Regular roblox dialogue, not a custom one.

Unfortunately, for regular Roblox dialogs there isn’t an event that gets fired when a player triggers the dialog, however there is an event for when a player makes their dialog decision. That would be DialogChoiceSelected(player: Player, dialogChoice).

You can find more on this event here: Dialog | Roblox Creator Documentation

One important thing to mention is that the event is only fired on the client, so you would utilize this in a LocalScript for it to actually work.

Code for utilizing an event goes as follows:

DialogInstance.DialogChoiceSelected:Connect(function(player, dialogChoice)
         -- Code you wish to be executed here
end)

Hopefully this is of help to you.

oh ok, thats, really inconvenient then

Well, now that I look through the documentation there is a rather hacky way to do it and that is by using a method inside of the dialog instance called GetCurrentPlayers(), which returns a list of players currently using the dialog. You can use that to check if the LocalPlayer is inside of that list (Meaning they are using the dialog) and have code get executed if the player is using the dialog.

It’s unfortunate that there isn’t an efficient way to check if a player started a dialog by default. Logically, you can probably just create the whole dialog instance on the client & listen for the dialog:GetPropertyChangedSignal("InUse") event. You could use the GetCurrentPlayers() method instead, but personally, I wouldn’t do this.

5 Likes

I know this is old but for any new comers i found a solution for single player scenarios

dialog:GetPropertyChangedSignal("InUse"):Connect(function()
    if dialog.InUse == true then
        print("hi")
    end
end
2 Likes