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

How to shuffle sounds and play randomly instead of in order?

Asked by 4 years ago

I have a radio in my game and it just plays the sounds in order, I don't think it's too efficient because every time a new sound is played it just waits the amount of time that sound is playing (that I found out) to play the next one. Another problem I have is it plays in order, would there be any way to shuffle the playlist around?

This is my script right now, any feedback on how to make it more efficient would really help!

local radio1 = workspace:WaitForChild("radio1")
local radio = radio1:WaitForChild("HitBox")
local sound1 = radio:WaitForChild("sound1")
local sound2 = radio:WaitForChild("sound2")
local sound3 = radio:WaitForChild("sound3")
local sound4 = radio:WaitForChild("sound4")
local sound5 = radio:WaitForChild("sound5")
local sound6 = radio:WaitForChild("sound6")
local sound7 = radio:WaitForChild("sound7")
local sound8 = radio:WaitForChild("sound8")


while true do
    wait(.1)
    sound1:Play()
    wait(113)
    sound1:Stop()
    sound2:Play()
    wait(125)
    sound2:Stop()
    sound3:Play()
    wait(120)
    sound3:Stop()
    sound4:Play()
    wait(103)
    sound4:Stop()
    sound5:Play()
    wait(120)
    sound5:Stop()
    sound6:Play()
    wait(44)
    sound6:Stop()
    sound7:Play()
    wait(235)
    sound7:Stop()
    sound8:Play()
    wait(125)
    sound8:Stop()

end

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Firstly, you might want to put your tracks in a table.

Lua doesn't naturally have a shuffle function for tables, but this might work

function shuffle(tbl)
  for i = #tbl, 2, -1 do
    local j = math.random(i)
    tbl[i], tbl[j] = tbl[j], tbl[i]
  end
  return tbl
end

Here's how tables work

0
yeah something like that Donut792 216 — 4y
Ad

Answer this question