PlaybackSpeed math.random only changes to 0 or 1

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
2 Likes

try this idk if it will change much

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
1 Like

That fixes the problem with the while loop :thumbsup: but the playback speed still doesnt change

This item is not replicated across Roblox’s server/client boundary. so im pretty sure u cant do this

1 Like

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

OH I JUST SAW!! in the wiki it says this

  1. sound.PlaybackSpeed = 3 – 3x faster
  2. wait(5)
  3. 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

Huh that is weird why would 0.5 be 2 times slower? does that mean that it only counts in 0.25? so 0.25 is 4 times slower?

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.

I dont get why roblox does this

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

Okay I see why now! 1 is the default. Going under 1 would be less then normal meaning its decreasing

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

I just googled can math.random do decimals it says math . random () will return a random decimal between 0-1 meaning sadly u cant use a decimal

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

You can use Random:NextNumber for random double between two doubles.

local randomObject = Random.new()
randomObject:NextNumber(0.6, 1.1)
1 Like

Do this might take a minute

local Var = math.random(1,6)
if Var == 1 then
PlayBackspeed
elseif Var == 2 then
PlayBackspeed
elseif Var == 3 then
PlayerBackSpeed
1 Like

Do this instead (math.random only takes integers unless blank):

local pbs = math.random(6, 11)
sound.PlaybackSpeed = (pbs / 10)
1 Like

Hey sorry for replying to everyone late but:

@MihawkRBLX This doesn’t seem to work but thanks for helping you did actually teach me stuff i didnt know yet :smiley: :+1:

but @Blockzez It did work I created this:

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

Thanks again everyone

7 Likes

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)

and you will get what you originally intended.

4 Likes