This checks to see if a light button is in the vicinity and turns on the lights incorporated to that button. For some reason it only sometimes fires and sends the signal to the server even though all of the requirements are met. Help!
local script in backpack:
wait(1)
local interactables = game.Workspace.MapParts:WaitForChild("Interactables")
local player = game.Players.LocalPlayer
local contextAction = game:GetService("ContextActionService")
local SelectedLight = nil
local active = false
local range = 4.6
function InteractionS(name, inputState, inputObject)
if SelectedLight ~= nil then
game.ReplicatedStorage.LightEvent:InvokeServer(SelectedLight)
script.LightS:Play()
end
end
function FindClosestLights()
if not player.Character or not player.Character:FindFirstChild("Humanoid")then
return
end
local Region = Region3.new(
player.Character.Torso.Position-Vector3.new(range,range,range),
player.Character.Torso.Position+Vector3.new(range,range,range)
)
local CloseObjects = game.Workspace:FindPartsInRegion3(Region)
local Lights = nil
for _,x in next,CloseObjects do
if x.Parent == interactables.Switches then
Lights = x
end
end
return Lights
end
function AntiCrash(name, inputState, inputObject)
if inputState ~= Enum.UserInputState.Begin or not SelectedLight then return end
spawn(InteractionS)
end
while wait() do
interactables = game.Workspace.MapParts:WaitForChild("Interactables")
SelectedLight = FindClosestLights()
if SelectedLight ~= nil and not active then
active = true
contextAction:BindActionToInputTypes("Interaction",AntiCrash,true,"e")
contextAction:SetTitle("Interaction","Light ON/OFF")
elseif active then
active = false
contextAction:UnbindAction("Interaction")
end
end
Script in workspace works fine and without the AntiCrash the script crashes the game.

2 comments
You shouldn't be doing this client-sided. A script this long in every player vs one script this long to iterate through all the players and check their distances.. I think it's a given that the latter is more efficient.
AND you won't have to worry about supplements for FilteringEnabled, as any player's character is in the server!