So basically how this script is when "Demon" is in radius of "Light" the light (pointlight) is suppose to dim, so far the script works, but I cant seem to make it work on two lights at the same time?, I have been trying to fix this for about an hour and cant find the solution.

01
-- || object references ||
02
local ws = game:GetService("Workspace") -- GetService is used in this context to create a shorthand for workspace, "ws". This isn't necessarily relevant to the script, but it's something I like to do when working with workspace objects
03
local part, npcTorso = ws.Light, ws.Demon:FindFirstChild("Torso") -- This assigns "part" to the part in Workspace, and "npcTorso" to the "Torso" part in the NPC (assuming there is one)
04
local light = part:FindFirstChild("Light") -- The light will be referenced by the variable "Light", assuming the location of the light is "game.Workspace.Part.Light".
05
 
06
-- || configurations ||
07
local maxBrightness = 2 -- This is the maximum brightness, where the NPC is farthest from the light
08
local minBrightness = 0.1 -- This is the brightness that will be shown when the NPC is directly at the same position as the light.
09
 
10
-- || numbers that change ||
11
local radius = 30 -- This is the radius in which the light will start to dim when the NPC comes near it.
12
while wait(1/30) do
13
    local distance = (npcTorso.Position - part.Position).magnitude -- See above, this is getting the distance between the NPC and the part
14
    if distance <= radius then -- This checks if the NPC is within the radius in which the light will be affected
15
        local brightness = (distance/radius) * maxBrightness -- This calculates the brightness based on distance
16
        if (brightness < minBrightness) then -- If the brightness drops below the minimum brightness
17
            brightness = minBrightness -- Set it to the minimum brightness
18
        end
19
        light.Brightness = brightness -- Finally, set the brightness of the light
20
    end
21
end
22
 
23
-- Huge credit to nikon1j1 for huge help!

1 comment

0

You have to loop through every light and do the calculation for each one.