So this music script is supposed to only play music in specific parts of my game. In Test or studio mode it works perfectly nothing goes wrong. However when I join a server for my Game it doesn't do anything. I checked my dev console's server log and I get the error "Workspace is not a valid member of players" Does this mean I wrote Workspace is in Players in the script? I honestly have no clue why it's not working. What did i do wrong?
Theres 2 scripts and I will show both of them but Im pretty sure the problem lies in the first one.
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Song2.Volume = 1 game.Players[hit.Parent.Name].PlayerGui.Song1:stop() game.Players[hit.Parent.Name].PlayerGui.Song2:play() end end)
script.Parent.TouchEnded:connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') then game.Players[hit.Parent.Name].PlayerGui.Song2.Volume = 0 end end)
If you don't know, you can get the Player from his Character, using "game:GetService('Players'):GetPlayerFromCharacter(hit.Parent)"
So the script will be like this:
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') then local player = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent) local playergui = player:WaitForChild('PlayerGui') local song1 = playergui:WaitForChild('Song1') local song2 = playergui:WaitForChild('Song2') song2.Volume = 1 song1:Stop() song2:Play() end end)
I hope I helped you!