Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

This Music script will only work in studio mode. What did I do Wrong?

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)

1 answer

✓ Accepted answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.