Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script is only effecting one light but not both? [closed]

Asked by 8 years ago

This question already has an answer here:

How can I make more than one light dim when "NPC" is in radius?

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!

0
You have to loop through every light and do the calculation for each one. GoldenPhysics 474 — 8y

Marked as Duplicate by TheHospitalDev, Kryddan, Pyrondon, GoldenPhysics, and koolkid8099

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?