Sound script not working

Hello, I am trying to make a script that plays a random audio from a specific folder in SoundService when the player joins. The problem is that is that it is not working at all.

local Songs = game:GetService("SoundService").MainSounds:GetChildren()
local Sound = script.Parent

while true do
    local RandomId = Songs[math.random(1, #Songs)]
    Sound.SoundId = RandomId.SoundId
    Sound:Play()
    Sound.Ended:Wait()
end

I have tried looking on the DevForum to find solutions but it seems that this is relatively unique as far as I can see. This is a LocalScript inside WorkSpace for those wondering.

Local scripts won’t execute in the workspace, it can only be ran in a few select places. One of those is StarterPlayerScripts. You can put the script there and re-reference the sound object in the workspace:

local Songs = game:GetService("SoundService").MainSounds:GetChildren()
local Sound = workspace.Sound -- whatever you've called the sound

while true do
    local RandomId = Songs[math.random(1, #Songs)]
    Sound.SoundId = RandomId.SoundId
    Sound:Play()
    Sound.Ended:Wait()
end

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

  • A Player’s Backpack , such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts .
  • The ReplicatedFirst service

(Sourced from this page on the Roblox Developer Wiki)