So I am trying to implement math.random() into a script that changes PlaybackSpeed, but all my attempts just result in PlaybackSpeed changing to only 0 or 1?? Thanks in advance
script
while true do
script.Parent.PlaybackSpeed = math.random(0.6, 1.1)
script.Parent:Play()
wait(math.random(3,7))
end
while true do
local randomSpeed = math.random(0.6,1.1)
local randomWait - math.random(3,7)
script.Parent.PlaybackSpeed = randomSpeed
script.Parent:Play()
wait(randomWait)
end
actually you might be half right, because upon closer inspection ive noticed its not the loop breaking, its just that the playback speed can only change to 1+ or 0. But, it can actually be changed if i put PlaybackSpeed = 0.5 it will change to that. it seems to be a math.random problem
sound.PlaybackSpeed = 0.5 – 2x slower
when using decimals i think it makes it slower because im reading the wiki and it says this. so the decimal might be making it go slow
For example, a value of 2 will cause the Sound to play 2x faster, whereas a value of 0.5 will cause it to play 2x slower. When PlaybackSpeed is equal to 1, the sound will take Sound.TimeLength (in seconds) to complete.
Well my main question is does that mean 0.25 is 4 times slower? or 3 times slower?? This is very confusing, if it is slower it should be -0.5 or something
But this won’t work because I have realised its a problem with math.random. If I do what you said it works fine, but if I do for example math.random(0.5,0.7) it still sets the value to 0. Anything above 1 will turn into 1 and anything below 1 will turn into 0. In a nutshell: The value can only change to 1 or 0 using math.random unless I’m doing something wrong
Ok, I looked up what you did and saw it too. But that means it should return at least a decimal? It doesn’t it just does either 0 or 1. Also, I’m going try using functions like this since setting it without math.random does work
function 1()
script.Parent.PlaybackSpeed = 0.6
end
function 2()
script.Parent.PlaybackSpeed = 0.7
end
while true do
function .. math.random(1,3):Play()
end
I know this won’t work but I will research it and it’s something like that
local randomObject = Random.new()
while true do
script.Parent.PlaybackSpeed = randomObject:NextNumber(0.6, 1.1)
wait(1)
end
although it works its not that precise but it is a small gap!
.
.
.
Also @Ty_IsHomeSchooled this works perfectly! precise decimals so I will use this tysm
local pbs = math.random(6, 11)
local sound = script.Parent
while true do
local pbs = math.random(6, 11)
sound.PlaybackSpeed = (pbs / 10)
wait(1)
end
math.random() : Random real number on the interval [0,1[
math.random(M) : Random integer from 1 to M inclusive, when M is an integer
math.random(M,N) : Random integer in the interval [M,N] where M and N are integers.
When you call math.random(0.6,1.1), the arguments are being implicitly converted to integers by truncation. So math.random(0.6,1.1) is the same as math.random(0,1), and it can only ever return 0 or 1.
The way you do a real number range from 0.6 to 1.1 with the old math.random is like this:
local r = 0.6 + (1.1-0.6) * math.random()
and I show you this for academic interest only, you have already been given the correct advice to use the new Random class. The new Random class does directly support min and max for real numbers, so you can do this:
local RNG = Random.new()
local r = RNG:NextNumber(0.6, 1.1)