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

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

Asked by 8 years ago

So far everything works, when the NPC is near the light it dims gradually, thanks to Nikon1j1. I can't seem to get more than one light to dim when the NPC is near, even when both lights are identical it is only working for that one light.

-- || object references ||
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
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)
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".

-- || configurations ||
local maxBrightness = 2 -- This is the maximum brightness, where the NPC is farthest from the light
local minBrightness = 0.1 -- This is the brightness that will be shown when the NPC is directly at the same position as the light.

-- || numbers that change ||
local radius = 30 -- This is the radius in which the light will start to dim when the NPC comes near it.
while wait(1/30) do
    local distance = (npcTorso.Position - part.Position).magnitude -- See above, this is getting the distance between the NPC and the part
    if distance <= radius then -- This checks if the NPC is within the radius in which the light will be affected
        local brightness = (distance/radius) * maxBrightness -- This calculates the brightness based on distance
        if (brightness < minBrightness) then -- If the brightness drops below the minimum brightness
            brightness = minBrightness -- Set it to the minimum brightness
        end
        light.Brightness = brightness -- Finally, set the brightness of the light
    end
end

-- Huge credit to nikon1j1 for huge help!
0
If you named a light the same name then it should work or add a intvalue inside the light and call it tag or something and then in the script check if that object has the certain child called tag LifeInDevelopment 364 — 8y
0
It looks like your only referencing one light, instead of doing that mabye make a table or do that tag thing or if a part is near its radius then check if its name is light LifeInDevelopment 364 — 8y

Answer this question