Removing a wait(), functions are called in the wrong order. Is the ModuleScript implemented correctly?

This modulescript adds a new functionality to proxmity prompts, it detects when a player fails to finish holding down the button during the HoldDuration, firing \prematureCallback\ and \completionCallback\ on completion. The second script demonstrates the module in use, passing the ProximityPrompt and the two callbacks.

The main issues with this code is:
• How to get rid of this wait, commented in the ModuleScript
• Are we missing some best practices that we should be applying?
• How can I code this for readability and maintainability?

In: ServerScriptService > ProximityModule \ModuleScript\

local module = {}

module.__index = module

function module.new(proximityPrompt, completionCallback, prematureCallback)
	
	local self = {}
	
	setmetatable(self, module)
	
	self.proximityPrompt = proximityPrompt
	
	self.completedCallback = completionCallback
	self.prematureCallback = prematureCallback
	self.isFinished = false
	
	self.promptTriggeredConnection = self.proximityPrompt.Triggered:Connect(function(ply)
		self.isFinished = true
	end)
	
	self.promptHoldBeganConnection = self.proximityPrompt.PromptButtonHoldBegan:Connect(function(ply)
		self.isFinished = false
	end)
	
	self.promptButtonHoldEndedConnection = self.proximityPrompt.PromptButtonHoldEnded:Connect(function(ply)
		
		-- important wait() ##### How do I remove this. Added because the functions are called in the wrong order.
        -- It should be: PromptHoldBegan > Triggered > PromptHoldEnded
        -- This wait() is so that triggered runs before the check happens
		wait()
		
		if self.isFinished then -- This is the check
			self.completedCallback()
		else
			self.prematureCallback()
		end
		
		self.isFinished = false
	end)
	
	return self
end

function module:Disconnect()
	self.promptTriggeredConnection:Disconnect()
	self.promptHoldBeganConnection:Disconnect()
	self.promptButtonHoldEndedConnection:Disconnect()
end

return module

In: Part > ProximityPrompt > Script

local proxModule = require(game:GetService("ServerScriptService").ProximityPromptv2)

local ProximityObject = proxModule.new(script.Parent,
    function() print("ran to completion") end,
    function() print("did not ran to completion") end)

ProximityObject:Disconnect()

Are we missing some best practices that we should be applying?

A rule of thumb is to use a maid object to manage your connections. They make the process way easier to do so and in general are so useful.

How to get rid of this wait, commented in the ModuleScript

The new task library solves this issue, use task.defer. It just resumes code in the next resumption cycle at which point it is resumed with the engine’s scheduler. Visit here to learn more about the new methods.

self.promptButtonHoldEndedConnection = self.proximityPrompt.PromptButtonHoldEnded:Connect(function(ply)
   	
   	task.defer(function()
	   if self.isFinished then -- This is the check
   	      self.completedCallback()
   	   else
   	       self.prematureCallback()
   	    end
    end)

   	self.isFinished = false
end)
1 Like