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.

2 Likes

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.

3 Likes

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

Found out how to do this locally.

You could just do it with the InUse property, if you are making a singleplayer game. Otherwise, I came up with this method:

You check if the Dialog has been changed, then compare the current players using the Dialog with a table featuring the previous batch of players that used it. Send a RemoteFunction with a boolean value (true if the player opened the dialog, false if they left the dialog) and the Dialog object.

ServerScript in Dialog:

local rep = game:GetService("ReplicatedStorage")
local leftPlrs = {} --table that stores previous players

local dialogFunc = rep:WaitForChild("DialogFunc") --function that fires when player leaves or joins dialog
local dialog = script.Parent

dialog.Changed:Connect(function()
	local plrs = script.Parent:GetCurrentPlayers()
	
	if plrs ~= nil then
		
		--checks the players that left the dialog
		for _, left in pairs(leftPlrs) do
			if left ~= nil and not table.find(plrs, left) then
				rep.DialogFunc:InvokeClient(left, false, dialog)
			end
		end
		
		leftPlrs = plrs
		
		for _, plr in pairs(plrs) do
			rep.DialogFunc:InvokeClient(plr, true, dialog)
		end
		
	end
	
end)

LocalScript in StarterPlayerScripts:

local rep = game:GetService("ReplicatedStorage")
local dialogFunc = rep:WaitForChild("DialogFunc")

dialogFunc.OnClientInvoke = function(bool, dialog)
	if bool == true then
		--code for when player enters dialog
	else
		--code for when player leaves dialog
	end
end
1 Like