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()