One problem is it's in a ServerScript and you're using game.Players.LocalPlayer, which will work in Studio but not in-game. So if you plan on using game.Players.LocalPlayer, you'll have to use a LocalScript.
One reason it wasn't working in-game in a LocalScript(at least for me), it was referring to the Music folder before it loaded for the Player. If you add a wait(1), WaitForChild, or something similar to that, it should get rid of that error, at least it did for me.
Example:
wait(1)
local Player = game.Players.LocalPlayer
Songs = script["Music"]:GetChildren()
Selected = nil
MTL = .25
function Song()
Random = Songs[math.random(1,#Songs)]
Selected = Random
script.Parent.Playing.Playing.Text = Selected.Name
wait()
for i = 1,#Selected:GetChildren(),1 do
Selected:GetChildren()[i]:Play()
wait(Selected:GetChildren()[i].Duration.Value-MTL)
end
end
while true do Song() end
Player.Character.Humanoid.Died:connect(function()
for Index,A in pairs(Selected:GetChildren()) do
if A:IsA("Sound") then
A:stop()
end
end
end)
Also not sure why you have a Value for how long the Sound actually is, ROBLOX has a Property for Sound's called TimeLength, heard before that ROBLOX broke it, not sure how true that is.. But anways, you could just use wait(Selected:GetChildren()[i].TimeLength-MTL). Downside with this is you might have to use repeat wait() until Selected:GetChildren()[i].TimeLength > 5 or something like that as it can take a few for ROBLOX to actually update that property.
Example:
local Player = game.Players.LocalPlayer
local Songs = script["Music"]:GetChildren()
local Selected = nil
local MTL = .25
function Song()
local Random = Songs[math.random(1,#Songs)]
Selected = Random
script.Parent.Playing.Playing.Text = Selected.Name
for i = 1,#Selected:GetChildren(),1 do
repeat
wait(1/30)
until Selected:GetChildren()[i].TimeLength > 5
Selected:GetChildren()[i]:Play()
wait(Selected:GetChildren()[i].TimeLength-MTL)
end
end
while true do Song() end
Player.Character.Humanoid.Died:connect(function()
for Index,A in pairs(Selected:GetChildren()) do
if A:IsA("Sound") then
A:stop()
end
end
end)
Another issue: A problem with your while true do Song() end and the Humanoid.Died function is, you're using a never-ending loop then a function below it, so the function below it will never get read. You need to put the Humanoid.Died function above the while true do Song() end loop in order for that to work.
Example:
local Player = game.Players.LocalPlayer
local Songs = script["Music"]:GetChildren()
local Selected = nil
local MTL = .25
function Song()
local Random = Songs[math.random(1,#Songs)]
Selected = Random
script.Parent.Playing.Playing.Text = Selected.Name
for i = 1,#Selected:GetChildren(),1 do
repeat
wait(1/30)
until Selected:GetChildren()[i].TimeLength > 5
Selected:GetChildren()[i]:Play()
wait(Selected:GetChildren()[i].TimeLength-MTL)
end
end
Player.Character.Humanoid.Died:connect(function()
for Index,A in pairs(Selected:GetChildren()) do
if A:IsA("Sound") then
A:stop()
end
end
end)
while true do Song() end
Sorry I can't really explain it any better.
1 comment
it did not work either way, iv tried everything, Configurations, Folders, it still all works in Studio, but not in Player... And when i use a local script, it dosent work at all