ProximityPrompt Interaction Scripts

Hello fellow Roblox Developers,

I’m currently working on a project that involves implementing interactive elements using ProximityPrompts. I’ve created two scripts, one for managing interactions and the other for handling those interactions, but I’m encountering some issues and could use some assistance from the community.

Intentions:
1. “Interactions” Module Script: This script defines various types of interactions, such as using ATMs, giving items, and changing teams. It handles ProximityPrompt configurations, action texts, and object texts. The goal is to enable players to interact with objects in the game based on their permissions, team, and other attributes.

Module Script
local lawEnforcementTeams = {
	["Police"] = true,
	["SWAT"] = true,
}

local InteractionModule = {}

local interactionsConfig = {
	{ 
		Type = "ATM",
		ObjectText = "ATM",
		ActionText = "USE ATM",
		Callback = function(player)
		end
	},
	{
		Type = "Give",
		ActionText = "TAKE ITEM",
		Callback = function(player, itemName)
			local item = game.Lighting.Items:FindFirstChild(itemName)
			if item then
				local backpack = player.Backpack
				if backpack then
					local clone = item:Clone()
					clone.Parent = backpack
				end
			end
		end
	},
	{
		Type = "Team",
		ActionText = "CHANGE TEAM",
		Callback = function(player, teamName)
			local currentTeamName = player.Team.Name
			if currentTeamName == teamName then
				player.Team = game.Teams["Civilian"]
			elseif game.Teams:FindFirstChild(teamName) then
				player.Team = game.Teams[teamName]
			end
		end
	},
}

function InteractionModule.SetPromptActionText(prompt, actionText)
	prompt.ActionText = actionText
end

function InteractionModule.SetPromptObjectText(prompt, objectText)
	prompt.ObjectText = objectText
end

function InteractionModule.HandleInteraction(interactionPart)
	local proximityPrompt = interactionPart:FindFirstChildOfClass("ProximityPrompt")
	if proximityPrompt then
		local typeAttribute = proximityPrompt:GetAttribute("Type")
		local idAttribute = proximityPrompt:GetAttribute("Id")
		local rAttribute = proximityPrompt:GetAttribute("R")
		local itemAttribute = proximityPrompt:GetAttribute("Item")
		local leAttribute = proximityPrompt:GetAttribute("LE")
		local teamAttribute = proximityPrompt:GetAttribute("Team")

		print("Handling interaction for type:", typeAttribute)

		for _, config in ipairs(interactionsConfig) do
			if typeAttribute == config.Type then
				print("Found matching config for type:", config.Type)
				InteractionModule.SetPromptActionText(proximityPrompt, config.ActionText)

				if config.Type == "Team" then
					InteractionModule.SetPromptObjectText(proximityPrompt, teamAttribute)
				elseif config.Type == "Give" and itemAttribute then
					InteractionModule.SetPromptObjectText(proximityPrompt, itemAttribute)
				else
					InteractionModule.SetPromptObjectText(proximityPrompt, config.ObjectText)
				end

				proximityPrompt.Triggered:Connect(function(player)
					print(player.Name .. " triggered interaction with type:", typeAttribute)

					local hasPermission = true
					if idAttribute and idAttribute ~= 0 then
						hasPermission = player:GetRankInGroup(idAttribute) > 0
					end

					if hasPermission then
						if rAttribute and player:GetRankInGroup(idAttribute) < rAttribute then
							print(player.Name .. " does not have sufficient rank for this interaction.")
							return
						end

						if leAttribute and not lawEnforcementTeams[player.Team.Name] then
							print(player.Name .. " does not have permission for this law enforcement interaction.")
							return
						end

						if config.Type == "Give" and itemAttribute then
							print(player.Name .. " is receiving item:", itemAttribute)
							config.Callback(player, itemAttribute)
						elseif config.Type == "Team" and teamAttribute then
							print(player.Name .. " is changing team to:", teamAttribute)
							config.Callback(player, teamAttribute)
						else
							config.Callback(player)
						end
					else
						print(player.Name .. " does not have permission for this interaction.")
					end
				end)
				break
			end
		end
	end
end

return InteractionModule

2. “InteractionHandler” Local Script: This script is responsible for initializing and managing interactions using the “Interactions” module. It identifies interaction parts in the workspace and connects them to the appropriate interaction functions. However, there seems to be an issue, as the interactions are not working as expected.

Local Script
local moduleName = "Interactions"
local interactionsModule = game:GetService("ReplicatedStorage"):WaitForChild("Shared"):FindFirstChild(moduleName)

if interactionsModule then
	local InteractionsModule = require(interactionsModule)

	local function initializeInteractions()
		local interactionsFolder = game.Workspace:WaitForChild("Interactions")
		for _, interactionPart in ipairs(interactionsFolder:GetChildren()) do
			if interactionPart:IsA("Part") then
				print("Initializing interaction for part:", interactionPart.Name)
				InteractionsModule.HandleInteraction(interactionPart)
			end
		end
	end

	initializeInteractions()
else
	warn("Interactions module not found:", moduleName)
end

Issues Faced
A few days ago, everything was working as intended. However, now I’m encountering unexpected behavior where interactions have stopped functioning. The scripts were operational initially, and I haven’t made any changes to them. Currently, I’m experiencing the following problems:

1. No Output or Errors: When testing the interactions, I noticed that no debug prints are being displayed in the Output panel. There are no error messages or warnings that could indicate what might be going wrong.

2. Interaction Functions Not Executing: The interaction functions specified in the “Interactions” module script are not being executed. For example, the code inside the Callback functions, such as team changes or item giving, is not being triggered when players interact with the objects.

I’ve thoroughly reviewed the scripts, but I’m unable to identify the cause of these issues. It’s perplexing because the scripts were functional before, and I haven’t made any recent changes that could have caused this behavior. I’m at a loss as to why these problems have surfaced suddenly.

If anyone has encountered similar problems or has insights into what might be causing these changes in behavior, I would greatly appreciate your guidance.

2 Likes

Can you send a portion of the LocalScript and ModuleScript?

1 Like

The scripts are already available in my post, accessable by using the arrows.

2 Likes

You should try to test the code in a new project. Comment out the parts that actually do things, and just use prints instead.

If it works, you know the issue is with some other part of code, not your prompt code.

1 Like

I did not know you could do that lol
Your issue is that your calling the module script from a local script. Module scripts when called from a local script can only do the same things as a local script, so they can’t set teams or clone tools. You will have to call it from a server side script, using remote events/functions, which then you could have the script do the function or the script could call the module script.

(I didn’t see you doing this already, but I am prone to missing things, so you might’ve already done that and I didn’t see)

1 Like

Thank you for your suggestion.

However, I must admit that I’m not very familiar with remote events and functions, and I’m not entirely sure how to implement them to achieve the desired functionality. I’m at a point where I could really use some guidance on how to properly set up remote events/functions and utilize them to interact with the Interactions Module Script on the server side.

If you could provide me with a more detailed explanation or an example of how I should structure the remote event/function communication between my local script and the server, it would be immensely helpful. I’m eager to learn and willing to put in the effort to get this working correctly. Your assistance in bridging this knowledge gap would be greatly appreciated.

Again, thank you for taking the time to offer your insight, and I look forward to any further guidance you can provide.

Here’s what I’ve tried:

InteractionHandler
local moduleName = "Interactions"
local interactionsModule = game:GetService("ReplicatedStorage"):WaitForChild("Shared"):FindFirstChild(moduleName)

if interactionsModule then
	local InteractionsModule = require(interactionsModule)

	local function initializeInteractions()
		local interactionsFolder = game.Workspace:WaitForChild("Interactions")
		for _, interactionPart in ipairs(interactionsFolder:GetChildren()) do
			if interactionPart:IsA("Part") then
				local function handleInteraction()
					local player = game.Players.LocalPlayer
					if player then
						local remoteEvent = game.ReplicatedStorage:WaitForChild("InteractEvent")
						remoteEvent:FireServer(interactionPart)
					end
				end

				print("Initializing interaction for part:", interactionPart.Name)
				InteractionsModule.SetPromptActionText(interactionPart.ProximityPrompt, "Interact")
				InteractionsModule.SetPromptObjectText(interactionPart.ProximityPrompt, "Object")
				interactionPart.ProximityPrompt.Triggered:Connect(handleInteraction)
			end
		end
	end

	initializeInteractions()
else
	warn("Interactions module not found:", moduleName)
end

ServerSide
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local interactEvent = Instance.new("RemoteEvent")
interactEvent.Name = "InteractEvent"
interactEvent.Parent = ReplicatedStorage

local interactionsModule = require(ReplicatedStorage.Shared.Interactions)

interactEvent.OnServerEvent:Connect(function(player, interactionPart)
	interactionsModule.HandleInteraction(interactionPart, player)
end)


I tested your suggestion in a new project, using print statements as replacements. However, the issue persisted.

So, its not even displaying any prints… I’ll look it over and see if I see anything.

What happens if you put a print statement here, does it display?
image

It displays.

15:35:03.715 Interactions - Client - InteractionHandler:4

I am getting to this point and this is printing for me

Are you getting this print statement working?