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

Need to make a Flickering Light, Any Help?

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited by Shawnyg 7 years ago

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!

1
Fixed up your code blocks. I recommend you see what I changed. Shawnyg 4330 — 7y
0
Thanks. Couldn't figure out why they weren't working. Astradant 45 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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

Answer this question