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

Getting a random number each time a function is called?

Asked by
Wayyllon 170
3 years ago

I know this is very amateur but i don't know how I would make it do what the title entails.

This is what i have so far:

L = script.Parent.PointLight.Brightness

function RNG()
    local N = math.random(.1,1.2)
    L = N
end

while true do
    wait(math.random(.3,.7))
    RNG()
end

I want to make it so each time the function is called it generates a new number. The function call works; however, the brightness of the point light doesn't change.

0
I think I know why, make a new variable called "Wait" and put this to the variable: math.random(0.3, 0.7). Then for line 9, just make it say: wait(Wait). VexelGG 0 — 3y
0
Well the problem isnt that the wait doesnt work, the problem is that the math.random in the RNG function doesnt change the brightness Wayyllon 170 — 3y

3 answers

Log in to vote
1
Answered by 3 years ago

Hello There,

You Can Use return to get a number.(Also you can't include decimals in math.random use Random.new():NextNumber(num1, num2) )


L = script.Parent.PointLight function RNG() local N = Random.new():NextNumber(.1,1.2) return N end while true do wait(Random.new():NextNumber(.3,.7)) L.Brightness = RNG() end
Ad
Log in to vote
0
Answered by 3 years ago

The variable L does not reference the Brightness property of the point light; rather, it simply returns the current value of brightness. So whenever you change L to N, you are only changing the variable L, and not the brightness property of the point light.

I would do it like this instead:

L = script.Parent.PointLight

function RNG()
    local N = math.random(.1,1.2)
    L.Brightness = N
end

while true do
    wait(math.random(.3,.7))
    RNG()
end

0
I THOUGHT I FIXED THAT ARE YOU KIDDING ME Wayyllon 170 — 3y
0
Wait actually, even with it referencing the brightness property it doesn't flicker. Wayyllon 170 — 3y
0
Fixed it! Thanks! Wayyllon 170 — 3y
Log in to vote
0
Answered by
Wayyllon 170
3 years ago

turns out this is the fixed version:

L = script.Parent.PointLight

function RNG()
    local N = math.random(.1,1.2)
    L.Brightness = N
end

while true do
    wait(math.random(.3,.7))
    RNG()
end

For whatever reason making it so the brightness is referenced inside the function causes it to work.

2
Right - when you say "L = ...PointLight.Brightness", you're getting a copy of the Brightness at that moment in "L", whereas you want a reference to the PointLight so that you can update the Brightness property every time you call the function chess123mate 5873 — 3y

Answer this question