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
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
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
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.
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
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).
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.
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.
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()
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!
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()