Random sound script doesn't work? why?

try replacing the random index thing with:

local sound = sounds[math.random(1,#sounds)]

This is because your Random r and randomIndex doesn’t have a defined range.
So if you have 5 sounds in your list, but it tries to find the index of 6 - sound will be NIL (empty, void, null).

Thus, the error you recieve.

@SeargentAUS’s code will resolve that.

To clarify what it does.

math.random(1,5) picks a number between 1 and 5.
#sounds counts how many items are in the sounds table
math.random(1, #sounds) picks a random number between 1 and the number of items in the table
sounds[1] picks index 1 from the sounds table.

their user of r should have been fine tbh, the only input it has, which is optional, is a seed:

not sure why it didn’t work regardless but yea, math.random is better

I’m pretty sure the error was because the sound:play was sounds:play. it seems to work after I changed the spelling but I’m not 100% sure, at least I heard the sound when I tested but I’m unsure if it will play it again.
edit: seems to only do it when starting the game and not play it again. chooses random sound but not a random wait time.

That would also do it xD
Should switch it to math.random anyway, tidies up the script a bit

local folder = game:GetService("SoundService")
local sounds = folder:GetChildren()

function pickRandomSound()
	while true do
		local sound = sounds[math.random(1, #sounds)]
		sound:Play()
		sound.Ended:Wait()
	end
end

pickRandomSound()

This is essentially the same as what I provided, other than the fact that you didn’t declare the function locally, which makes accessing it slower.

i just noticed that i forgot to mention that it also has to wait for a random amount of time Which might make the wait(math.ranfom(1,10)) look like a silly mistake when not mentioned. sorry! im starving rn which makes me crazy!

Where did you get sounds:play() from? Unless you’re not using the script I originally provided.

local folder = game:GetService("SoundService")
local sounds = workspace:GetChildren()

function pickRandomSound()
	while true do
		local sound = sounds[math.random(1, #sounds)]
		if sound:IsA("Sound") then
			sound:Play()
		else
			sound = sounds[math.random(1, #sounds)]
			return
		end
		sound.Ended:Wait()
	end
end

pickRandomSound()

This should work!

Put the sounds in workspace btw!

I’m using the original script but with changes I think would fix it which includes few stuff that were in your script. It does work now but the wait random thing doesn’t, but I will look into it.

I was originally going to write some code to maybe get some another perspective into this problem, but then I realized: what are you actually trying to do? Do you want the code to play a random song for 1-10 seconds, stop that song, then play another one and do that entire loop over and over? Or do you want it to just play 1 random song and then wait 1-10 seconds so you can do something else that random length of time passes.

wait for a random time length, plays a random sound, wait a random time and play another random sound. (they change every time, both time and sound) I’ve fixed the script that it does work when it comes to the choosing a random sound but it doesn’t wait a random time

That part should be as simple as this:

task.wait(math.random(10))

But my way of doing this whole task would probably be along the lines of something like this:

local soundService = game:GetService("SoundService")
local sounds = soundService:GetChildren()

local function loop()
    while true do
        local randSound = sounds[math.random(#sounds)]
        randSound:Play()
        
        task.wait(math.random(10))
        -- If you wanted to stop the sound before the next one plays:
        
        randSound:Stop()
        randSound.TimePosition = 0
    end
end

loop()

Something else to note which I saw being discussed earlier: If you want every player in the game to hear the sound at the same time, it should be done on a regular Script (not a local one).

it being local script is for a reason! :smiley:

local soundService	= game:GetService('SoundService')
local sounds		= soundService:FindFirstChild('Sounds'):GetChildren()

local min, max		= 5, 10
local rng			= Random.new()

while true do
	local waitTime	= rng:NextInteger(min, max)	-- Picking random number from the range
	task.wait(waitTime)	-- Wait for the random number
	
	local sound		= sounds[rng:NextInteger(1, #sounds)]	-- Choosing random sound from the sounds folder
	sound:Play()	-- Play the chosen sound
	
	sound.Ended:Wait()	-- Wait until the sound finished playing
end

Create a folder named Sounds inside the SoundService and put all the sounds you want to play inside it.
Put this local script inside StarterPlayerScript.

1 Like

ohho! this one works, im not used for a random time to be in this way so is there a way to multiply the times by *60. i tried to do it in a way it can be done for math.random but i think it broke it?

Do you mean like you generate a random wait time, after that you want to multiply it with 60?

yea, turning the second into a minute

How long do you want the delay be?