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

I cant figure out how to start this script?

Asked by 8 years ago

I am very new to scripting and If anyone could provide a template to build off of I would very grateful.

So basically I am trying to make a script that dims PointLights in radius when a specific NPC in near it, I also have no clue how to create a cap for it to stay in so it doesn''t get too dark when the NPC is very close and too bright when far?, I have no code to provide because I'm so lost.

0
This wiki page will tell you how to get the distance between two parts; http://wiki.roblox.com/index.php?title=Magnitude legosweat 334 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

First of all, if you haven't touched scripting yet, I'd recommend checking out a beginner's guide: http://wiki.roblox.com/index.php?title=Intro_to_Scripting

Now, on to you're question:

So basically I am trying to make a script that dims PointLights in radius when a specific NPC in near it

Let's start with the "radius" part - Fortunately, Vector3 has a handy "magnitude" property built-in for you!

http://wiki.roblox.com/index.php?title=Magnitude

The distance between two parts can be calculated with the following piece of code: (part1.Position - part2.Position).magnitude Given this, let's say that both your light and the NPC are in Workspace. The NPC is named "Noob", and the light is located at game:GetService("Workspace").Part.Light

Let's start making the script by setting some variables, shall we?

-- || 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.Part, ws.Noob: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 = 15 -- This is the radius in which the light will start to dim when the NPC comes near it.

Now, let's check the distance between the NPC and the Light every 1/30 of a second. When the distance is checked, we'll adjust the light's brightness according to the distance.

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

Of course, you may want to adjust some of the variables.

0
Thank you so much!, I have been messing with it for about 30 minutes, but I can seem to make it work for more than one light?, iiPrince 0 — 8y
Ad

Answer this question