Issues with proximity prompt

When I activate one proximity prompt, it breaks the other ones…

I’m trying to make a light switch turn the lights on with proximity prompts,
I am using modules to do this.

Weirdly, when I turn one light switch on, It breaks all other light switches causing them to do nothing.

This is a Video of the Issue:
https://youtu.be/Xe-orUOijkU

Module for tween:

local module = {}
--Variables
local Opened = false
--Functions
function module.Open (prox:ProximityPrompt, plr:Player)
	local Door = prox.Parent.Parent.Parent

	if prox.ActionText == "Turn On" and Opened == false then
		Opened = true
		prox.ActionText = "Turn Off"
		--sound
		local Sound = game.ReplicatedStorage.Sounds.Enviromental.LightSwitch
		Sound:Play()
		--tween
		local TS = game.TweenService

		local tween = TS:Create(
			Door.Main,
			TweenInfo.new(.25, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),
			{CFrame = Door.Main.CFrame * CFrame.Angles(0, 0, math.rad(-90))}
		)

		tween:Play()
		--Lights
		local LightsModule = require(script.LightsModule)
		LightsModule.lights(Door:FindFirstChild("LightType"))
		
		tween.Completed:Wait()
	elseif prox.ActionText == "Turn Off" and Opened == true then
		local Sound = game.ReplicatedStorage.Sounds.Enviromental.LightSwitch
		Sound:Play()
		--tween
		local TS = game.TweenService

		local tween = TS:Create(
			Door.Main,
			TweenInfo.new(.25, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),
			{CFrame = Door.Main.CFrame * CFrame.Angles(0, 0, math.rad(90))}
		)

		tween:Play()
		--Lights
		local LightsModule = require(script.LightsModule)
		LightsModule.lights(Door:FindFirstChild("LightType"))

		tween.Completed:Wait()
		--prox
		Opened = false
		prox.ActionText = "Turn On"
	end
end

return module

Module for Lights:

local module = {}
--functions
function module.lights (lightType)
	print(lightType.Value)
	task.spawn(function()
		local Lights = script.Parent.Parent.Parent.Parent.Parent
		
		for _, l in pairs(Lights:GetDescendants()) do
			if l:IsA("SpotLight") then
				if l.Parent.Name == lightType.Value then
					if l.Enabled == false then
						l.Enabled = true
					elseif l.Enabled == true then
						l.Enabled = false
					end
				end
			end
		end
	end)
end

return module

This variable is global for your entire module, meaning that after turning on one light any other light that you try to turn on will find that Opened is true, causing this statement to never let run the code inside it:
if prox.ActionText == "Turn On" and Opened == false then

And same for the next statement will never run, cause the text of the prompt is TurnOn (cause its turned off) and the Opened is true
elseif prox.ActionText == "Turn Off" and Opened == true then

If you delete the Opened variable from the statements and from entire script it will work as expected.


Would be good if you improve the approach too, having this kind of things looks like bad practice:

  • Excessive parent ladders
    local Door = prox.Parent.Parent.Parent
    local Lights = script.Parent.Parent.Parent.Parent.Parent

  • Getting everything just to compare if a parent of the light match the name gotten

for _, l in pairs(Lights:GetDescendants()) do
			if l:IsA("SpotLight") then
				if l.Parent.Name == lightType.Value then

But that just details to improve to make a more readable and friendly system

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.