You would need to use CollectionService to tag all the Lights of your game. It’s really useful to use it as it’ll reduce alot the Cluster to organize things. You can check-out a Plugin about it here.
Right after this, Create a tag named “Light” and tag all of your Light Instances (Light Objects, Such as SpotLight, PointLight, SurfaceLight, etc) with it. Then you can loop through each Tagged Instance with the “Light” tag every Frame and update it. We can check the Distance between the Player’s Character and the Light’s ancestor.
The code would need to be Client-sided as we only want to update it for the Client. This is what it would look like inside a script in “StarterCharacterScripts”
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local LocalCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local LocalRootPart = LocalCharacter:WaitForChild("HumanoidRootPart")
local TaggedLightInstances = CollectionService:GetTagged("Light")
local UpdateTaggedAdded = CollectionService:GetInstanceAddedSignal("Light"):Connect(function() TaggedLightInstances = CollectionService:GetTagged("Light") end)
local UpdateTaggedRemoved = CollectionService:GetInstanceRemovedSignal("Light"):Connect(function() TaggedLightInstances = CollectionService:GetTagged("Light") end)
local LIGHT_DISTANCE = 100
RunService.RenderStepped:Connect(function()
if LocalRootPart and #TaggedLightInstances > 0 then
for _, Light in ipairs(TaggedLightInstances) do
-- Ensure the Object is actually a Light Source
if Light:IsA("Light") then
-- Find the Ancestor and get the Distance
-- If there's not an Ancestor which's a BasePart or Attachment, It's gonna get ignored
local Ancestor = Light:FindFirstAncestorWhichIsA("BasePart") or Light:FindFirstAncestorWhichIsA("Attachment")
local Distance = if Ancestor then (LocalRootPart.Position - Ancestor.Position).Magnitude else nil
-- Are we close to the Distance?
-- Yes; Enable
-- No; Disable
if Distance then
if Distance > LIGHT_DISTANCE then
Light.Enabled = false
elseif Distance <= LIGHT_DISTANCE then
Light.Enabled = true
end
end
else
continue
end
end
end
end)