Random sound script doesn't work? why?

I want to learn so I’d appreciate if you didn’t write the script for me. just say what do I need to do, but you can do otherwise and write if you really want that.

Okay so this, it doesn’t work at all as a local script but the first 2 prints, print when normal script. but it still doesn’t work. how to make it work completely as a local script?

print("hey1") --prints only when not local script
local folder = game.SoundService
local sounds = folder.GetChildren
local r = Random.new()
print("srart1") --prints only whem not local script


function pickRandomSound()
	while sounds do
		local randomIndex = r:NextInteger(1, #sounds)
		print("loopstart") --doesnt


		sounds:Play()
		wait(math.random(1,10))
		print("soundplayed") --doesnt

		return sounds[randomIndex]




	end
end

ty! for help!

1 Like
local folder = game:GetService("SoundService")
local sounds = folder:GetChildren()
local r = Random.new()

local function pickRandomSound()
	while true do
		local randomIndex = r:NextInteger(1, #sounds)
		local sound = sounds[randomIndex]
		sound:Play()
		sound.Ended:Wait()
	end
end
1 Like

doesn’t work, sadly. I even tried inserting parts of this to the original script which “worked” on local script but it only printed the first 2 prints again :frowning:

Hey um, you need to call the function?

at the bottom of the script, do:
pickRandomSound()
You may have already done this

edit: I see that you mention the local script doesn’t run at all. Where is the localscript saved?

Just a few notes

.GetChildren is a function, and requires the brackets at the end to call it
.GetChildren()

Since you’re immediately returning a value from the pickRandomSound function, the while loop is redundant and can be removed.

Lastly, I would presume that you are calling the pickRandomSound function somewhere else, but just in case, you do need to call it.

Put this at the end of your script.
local newSound = pickRandomSound()

oops! forgot that, I accidentally left the local script at the wrong place but after placing it at starsterplayerscripts it works but only prints the same first 2 prints, which was the same for the normal script.

Then you need to call the function:

pickRandomSound()

also, as @WingItMan mentioned, .GetChildren should be :GetChildren()

yea I fixed the get children like 9 mins ago, and putting pickrandomsound at the end does work but it also gave a new error which I have to fix real quick

Alright. What was the error?
Also, is the issue resolved other than the error

the error is just attempt to call nil value stuff for the sound:Play but I’m currently looking at it if I can find the cause of it myself.

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!