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

Getting a random number each time a function is called?

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.

2 comments

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).

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

3 answers

✓ Accepted answer

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

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

3 comments

0

I THOUGHT I FIXED THAT ARE YOU KIDDING ME

0

Wait actually, even with it referencing the brightness property it doesn't flicker.

0

Fixed it! Thanks!

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.

1 comment

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

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