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.
What is the issue?
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
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
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?
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
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
Perhaps something outside of your code is causing the issue?
Below you can find exactly what i copied/changed to make it work:
Setup:
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
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?
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)
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
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)
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.
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()
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.