Is there a better way to code this?

FireSound.Pitch = math.random(0.8, 2)

Thats the line of code I am having trouble with.

when using math.random with pitch, for whatever reason when ever the pitch goes below 1, the sound just doesnt play (usually it should)

Anyone know another random way to randomize the pitch of the audio?

math.random() only does whole numbers, not decimals.

i usually do this:

local Ex = math.random(8, 20)

FireSound.Pitch = Ex/10

Hope this helps

1 Like

Random is also an option, it supports both decimal and integer numbers.

local R = Random.new():NextNumber(0.8, 2)
FireSound.Pitch = R
print(R)
1 Like

Thank you so much for letting me know about this. Is there any way to round the numbers up to the tenth place? if not thats fine. just wondering for sake of the exact pitch

Using math.round and multiplying the number by 10 (to make it an integer since math.round only works with integers) and dividing it by 10 (to make it a decimal again)

local R = Random.new():NextNumber(0.8, 2)
FireSound.Pitch = math.round(R*10)/10

The amount of 0 will be the number of decimal places: 10 = 0.1, 100 = 0.01, etc.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.