Range Differing For Lights

Good Morning, Day, Evening or Night DevForum.
I’m wanting to make a light for my game that differs range kind of like this:
robloxapp-20211120-1501086.wmv (1.9 MB)
(Streamable link incase it doesn’t work https://streamable.com/adzp78 )

If it would be possible, I’d love someone reaching out to me showing me a simple script for this, because i’m a beginner scripter and I’d want to understand the script. Thanks Already!

Hey,
To achieve this kind of differing, you can make a loop that changes the brightness and colour of the light point every few seconds or so.

I wouldn’t like to spoonfeed you with already made code so here is some pseudocode to guide you to the solution of you problem:

local Light = Part.PointOfLight

while true do wait(math.random(0.1,2)
Light.Brightness = math.random(20,100)
Light.Color = Color3.fromRGB(math.random(colorrange1-colorrange2),...)
wait(0.5)
Light.Brightness = -- original brightness
Light.Color = -- original colour
end

So this script will do the job for example?

local Light = script.parent.PointLight

While true do
Light.Range = math.random(10,15)
wait()
end

It’s basically a loop and it randomizes the range every time(atleast, i think) will it work?

This should work but it will change the light range very fast so it might look like it’s flickering.

Oh yeah but to fix that I just insert a number in seconds between the brackets right?

The script works, but how can I make it so the script will also change the range to an uneven number.
Example: Range: 1.5 Instead of 1 or 2

Because now the script is only choosing the numbers 8,9 and 10, Because the math.random is set to (8,10)

You can try doing math.random(1000,5000) / 100 for an uneven number.
5000 is 50 max range and 1000 is 10 min range in this example.

2 Likes

I think i missed something…


The script:
image

You forgot to divide the math.random result by 100

so it’s: Light.range = math.random(800,1000) / 100 ?

Yep. The max range is 60 and dividing the math.random result by 100 you get a decimal.

2 Likes

Thanks man! Really helped! I can’t thank you enough!

1 Like

wait should be replaced with task.wait due to it having a ton of performance issues. It probably wouldn’t make much of a difference here but it’s generally a good idea to use it anyways.

I’ll look into it, because the game is pretty small and there are not many performance issues yet since the game only has a part or 50.