OnClientEvent unreliable when using ProximityPrompt

Hi Fellow scripters.

I’m working on a Quest system which has a few server and client events in order to dynamically build the GUI. This means I have two parts of the system.

  1. localscript
local ReplicatedStorage = game:GetService("ReplicatedStorage");
-- Dialog initializer
ReplicatedStorage.Events.sendDialog.OnClientEvent:Connect(function(who, title, par1, par2, par3, par4, par5, image, isYesNo)
	local dialog = script.Parent.Main.Dialog;
	print("Retrieved sendDialog")
	if dialog.isOpen.Value == false then
		print("Done setting up Dialog");
	end
end)

And my server script (A part of it)

					if contentFolder ~= nil then
						local who = contentFolder.Who.Value;
						local title = contentFolder.Title.Value;
						local par1 = contentFolder.Par1.Value;
						local par2 = contentFolder.Par2.Value;
						local par3 = contentFolder.Par3.Value;
						local par4 = contentFolder.Par4.Value;
						local par5 = contentFolder.Par5.Value;
						local image = contentFolder.Image.Value;
						print(who, title, par1, par2, par3, par4, par5, image, yesNo)
						ReplicatedStorage.Events.sendDialog:FireClient(player, who, title, par1, par2, par3, par4, par5, image, yesNo);
						print("Sending quest ("..who..") to player "..player.Name);
					end

In my situation sometimes print("Retrieved sendDialog") is visible in the console, but sometimes its not. Its very unreliable. Is this normal? If not any idea what i overlook?
The server script is in this case located in a character model which is available in game.Workspace. No that I write this that might be the issue :thinking: … Going to rewrite my code so it works from ServerScripServer. Does this matter?

The localscript is located in the StarterGui / player.PlayerGui

I assume the server script is printing lines as expected.

A possibility is that the local script has not yet been duplicated into PlayerGui. This would happen if the game has just started or the player has just reset. You could fix this by adding a “WaitForChild” in the server script to ensure the PlayerGui contains the script.

Its also possible that you have the wrong player.

Putting the script in ServerScriptService is probably a better idea for organization but it should still work.

Thank you for your reply!

I’ve doubble checked my code and I noticed I already tried adding a wait(5) into my localscript.
It also seemed that the player was correct since one moment the UI did open and one moment the UI did not.

One thing I did not mention was that this events is fired from a ProximityPrompt.
I decided to look up the documentation when I realized I had not done that yet. Since everything i write is out of my head.

Once i was done reading the documentation I noticed this:

server script

script.Parent.Head.ProximityPrompt.PromptTriggered:Connect(function(player)
    -- some code
    ReplicatedStorage.Events.sendDialog:FireClient(player, who, title, par1, par2, par3, par4, par5, image, yesNo);
    -- some more code
image, yesNo);
end)

A beter way to tackle this is to make use of the ProximityPromptService;
This keeps track of all the proximityprompt triggers.

local ProximityPromptService = game:GetService("ProximityPromptService")

-- Detect when prompt is triggered
local function onPromptTriggered(promptObject, player)
     local npc = promptObject.Parent.Parent; -- this would be my NPC in my case
     ReplicatedStorage.Events.sendDialog:FireClient(player, who, title, par1, par2, par3, par4, par5, image, yesNo);  
-- all i need to do now is modify this remote event so it takes the NPC as parameter
end

-- Connect prompt events to handling functions
ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)

I hope this information will help other people that might walk against the same issue.