Proximity Prompt in Module

Hello!

I’m trying to use proximity prompts in a module script to keep my game clean and organized. However, I believe my unorganized knowledge of prompts is to blame for this faulty code.

  1. What is the issue?
    image
    it just says interact instead of ‘Use Door’ and all the settings i applied to the prompt aren’t working either. It also doesn’t run any fuction i pass into my parameters

  2. What solutions have you tried so far?
    I’ve tried using loops, task.spawn(), task.wait(), nothing worked.

==============================

Here is my code for the module script(i don’t even know what I did)

-- the code in the --[[]] block is what is the syntax is supposed to look like in a real script
function module.CreatePrompt(item, promptFunction, properties: {})
	--[[
	local part = script.Parent

	function use()
		print('hi')
	end

	local properties = {
		ActionText = 'Print',
		MaxActivationDistance = 7,
		RequiresLineOfSight = false
	}

	objectModule.CreatePrompt(part, use, properties)	
	]]
	
	task.spawn(function()
		local proximityPrompt = Instance.new('ProximityPrompt', item)

		proximityPrompt.ActionText = properties.ActionText
		proximityPrompt.MaxActivationDistance = properties.MaxActivationDistance
		proximityPrompt.RequiresLineOfSight = properties.RequiresLineOfSight

		proximityPrompt.Triggered:Connect(function()
			promptFunction()
		end)
	end)
end

and here’s my part of my code for the my door script:


function module.UseDoor()
	local open = false
	local debounce = false
	
	if not debounce then
		print('works! :D')
	end
end

function module.Setup()
	-- all of this is in a loop checking for all descendants in workspace that is a door
			
			local properties = {
				ActionText = 'Use Door',
				MaxActivationDistance = 7,
				RequiresLineOfSight = false
			}
			
			objectModule.CreatePrompt(v, module.UseDoor, properties)
		end
	end
end
1 Like

I can’t find what seems to be wrong about the code. I suppose the code is in fact running since you can see the proximity prompt from:
local proximityPrompt = Instance.new('ProximityPrompt', item)
Or is the screenshot of the proximity prompt not that one?

2 Likes

the screenshot is the one that was created
my issue is that it isn’t rendering the prompt with the correct settings like in the example i’ve shown in my code

local properties = {
	ActionText = 'Use Door',
	MaxActivationDistance = 7,
	RequiresLineOfSight = false
}

the prompt doesn’t use these settings and instead uses the default properties of a proximityprompt and doesn’t run it’s .Triggered() event called in the CreatePrompt function

2 Likes

Having copied your code almost 1:1, i found no errors:


Perhaps something outside of your code is causing the issue?

Below you can find exactly what i copied/changed to make it work:

Setup:
image

Script
local module = require(script.DoorScript)
module.Setup(script.Parent)
ObjectModule
local module = {}

function module.CreatePrompt(item, promptFunction, properties: {})
	task.spawn(function()
		local proximityPrompt = Instance.new('ProximityPrompt', item)

		proximityPrompt.ActionText = properties.ActionText
		proximityPrompt.MaxActivationDistance = properties.MaxActivationDistance
		proximityPrompt.RequiresLineOfSight = properties.RequiresLineOfSight

		proximityPrompt.Triggered:Connect(function()
			promptFunction()
		end)
	end)
end

return module
DoorScript
local objectModule = require(script.Parent.ObjectModule)

local module = {}

function module.UseDoor()
	local open = false
	local debounce = false

	if not debounce then
		print('works! :D')
	end
end

function module.Setup(item)
	local properties = {
		ActionText = 'Use Door',
		MaxActivationDistance = 7,
		RequiresLineOfSight = false
	}

	objectModule.CreatePrompt(item, module.UseDoor, properties)
end

return module
1 Like

hmm.
image
this is what i’m using.
ObjectModule → Replicated Storage
Door Module → ServerScriptService

I’m initializing the module on my initialize script.
i dunno if it has something to do with my setup or something :confused:

1 Like

I think the looping has something to do with it

1 Like

Strange, the loop seems completely fine to me…
Are you getting any errors or warning? If not, have you tried adding print(properties) at the end of the task.spawn() function to see if it receives the properties at all?

1 Like

NOTE: I have 2 doors in the game for testing different tweening types and different times with cooldowns

It does seem to print out the properties.
So… i dunno the issue
image

1 Like

This is what it’s doing for some odd reason:

It just does it when i join but then reverts to a proximity prompts default settings

1 Like

hmmmmmmmmmmmmm. i used the same code but on a server script and it works for it but not on the doors(which use the createPrompt function in a module script, not a server script even tho it’s being called on a server script(in server script service)
image

1 Like

I’m a bit confused by

it works for it but not on the doors

Did you put all the code into one single server script?

Although, i find it really strange how it clearly works, but then resets itself? Perhaps its creating multiple proximity prompts on the door, but that doesn’t explain why they don’t also have the correct properties… Do you have any other script/module in your game that uses proximity prompt to some extent? Again seems unlikely, but just throwing my last ideas out there :face_with_diagonal_mouth:

the only scripts i have with proximity prompts also use the module in a server script in workspace for separate items. I dunno if I should make module scripts for items or what but…

Also to answer: “It works for it but not on the doors”
It works on separate objects or just singular objects, i.e. objects in workspace, but not on doors(which are handled in a module)

When i try to replicate the problem on my end with same types of scripts and code, i just can’t get the problem to occur. I don’t think the code being in a module script is wrong, so it may be the way you are using the module scripts.

Btw, are you saying the doors are created through a script?
but not on doors(which are handled in a module)

No. I have the doors in a folder in workspace called Doors, and then a model called Door with attributes that are got in my module script.

Here is my hierarchy:

Workspace:
    -> Doors: Folder
        -> Door: Model
            -> -- bla bla, parts, weld constraints, etc.
        -> Door: Model

ServerScriptService:
     -> ServerModules: Folder
        -> DoorModule.lua: ModuleScript
     Initialize.lua: ServerScript
  

The hierarchy you have seems fine, i tested it with same locations with no problems. If i was you, i think i would just rewrite the whole door code from scratch in a new game and afterwards import it into your actual game.

1 Like

image
this is strange. I dunno why it’s working with scripts in workspace but not in server script service.

do you believe it has something to do with the code in server script service?

local modules = {}

local serverModules = script.Parent.ServerModules
function Initialize()
	if serverModules then
		for _, v in pairs(serverModules:GetDescendants() and serverModules.Objects:GetDescendants()) do
			if v:IsA('ModuleScript') then
				local module = require(v)
				modules[v.Name] = module
			end
		end

		for i, v in pairs(modules) do
			if type(v.Setup) == 'function' then
				task.spawn(function()
					v.Setup()
				end)
			end
		end
	else
		assert('not found lol; restart or do ur code better')
	end
end

Initialize()

I found one error, this part:
for _, v in pairs(serverModules:GetDescendants() and serverModules.Objects:GetDescendants()) do
If i’m not mistaken, you can’t use the and operator in a for loop to combine two tables. But it’s no problem because simplyfying it and just saying: serverModules:GetDescendants() is the same thing, since serverModules.Objects is a descendant of serverModules. While i was at it, i tried to simplify the code to:

local modules = {}

local serverModules = script.Parent:WaitForChild("ServerModules")
function Initialize()
	for _, v in serverModules:GetDescendants() do
		if v:IsA('ModuleScript') then
			local module = require(v)
			modules[v.Name] = module
			if type(module.Setup) == 'function' then
				task.spawn(module.Setup)
			end
		end
	end
end

Initialize()

Try this and see if it changes anything :smiley:

it does the same thing as before. also this is very confusing:

it’s doing the same thing again but flashing the prompt this time. So it does work but when i walk to it it doesn’t. i’m thinking it has something to do with prompt itself or the module. I will try it with a manual created prompt(using Instance.new()) and also making a prompt in each door within my doors folder.

thanks.

Yeah it makes no sense. I wonder what you see in the properties panel of the prompts before and after you move towards it?

I just tried with all these codes and it works perfect, I have only changed the location of the module scripts to replicated storage.