Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Need to make a Flickering Light, Any Help?

Hi, I don't script that much and wanted to know if anyone could help me with this? Probably something obvious

Script:

while true do
	math.randomseed(tick())
	local Brightness = math.random(0.1 , 1)
	local Light = script.Parent
	local SpotLight = Light.SL
	local B = SpotLight.Brightness
	local RandomPause = (math.random(0.5 , 5))
	wait(RandomPause)
	B = (Brightness)
	wait (RandomPause)
	B = (Brightness)
end

Trying to make a flickering light that randomly changes brightness at random times.

2 answers

Hi. I can clearly see the problem in this code, but sometimes it's harder to identify and can be quite time consuming in longer examples. I recommend you use print() statements and / or breakpoints to find where the particular problem is.

First off, we only really need to set math.randomseed(tick()) at the top of the script. Of course you can set it in the loop if you want to, I just don't know if you've been misled about needing to do that.

The problem arises where we have this statement:

B = (Brightness)

First, let's think about what B is. It's the Spotlight's Brightness property, right? We accessed the property correctly...so why isn't it working? That's because B is actually not a reference to the Brightness property.

In fact, when you declare B, it will hold the contents of whatever is at SpotLight.Brightness. Let's say this is the number 3.

So really, you've done an assignment of B = 3. Now we set B = Brightness. That will change the contents of the variable, 3, to the contents of the Brightness variable, but this won't actually change the property.

How do we fix this? Easy! We just set SpotLight.Brightness to the Brightness variable, like so:

SpotLight.Brightness = Brightness

Hope I could help. :) Good luck scripting!

2 comments

1

Fixed up your code blocks. I recommend you see what I changed.

0

Thanks. Couldn't figure out why they weren't working.

So this?

while true do
	math.randomseed(tick())
	local Brightness = math.random(0.1 , 1)
	local Light = script.Parent
	local SpotLight = Light.SL
	local RandomPause = (math.random(0.5 , 5))
	wait(RandomPause)
	SpotLight.Brightness = Brightness
	wait (RandomPause)
	SpotLight.Brightness = Brightness
end

This discussion is preserved as it was. New answers, comments, and votes are closed.