script.Parent.Touched:Connect(function(hit)
if script.Parent.Playing.Value == true then return
else
if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local sound = player.PlayerGui.OutdoorSound
if script.Parent.Parent.Stairwell.Playing.Value == true then
player.PlayerGui.StairwellSound:Stop()
script.Parent.Parent.Stairwell.Playing.Value = false
sound:Play()
else
script.Parent.Playing.Value = true
sound:Play()
end
end
end
end)
My script is failing to play a sound, though it plays in studio, but not in-game?
3 answers
This is because you tried to access the PlayerGui from the server. Place the sound in the part, and play it there.
2 comments
Will it still play locally if I do that?
You could do SoundService:PlayLocalSound(sound) if you want it to be local.
You are accessing the player from the server. I'm guessing that this is on FE. In the line
local sound = player.PlayerGui.OutdoorSound
you are trying to access the PlayerGui, but it has nothing from the server's point of view. In Studio, it does not have that, so it works fine in Studio. A fix for this would to have a remote event or something to tell the client that it should play the sound.
If you're using a Script with experimental mode off, the server can't view the descendants of each clients' PlayerGui. This does work in studio because everything in studio is ran on the client.
You could make a sound play locally by re-writting the script in a client-sided(local) script, or using a RemoteEvent, the listener which will be the LocalScript, will play the sound, while your server-sided(normal) script will fire the event to play the sound for the client.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
I debugged it by using "print", and it printed perfectly. So it's as though the script is skipping "sound:Play()". Nothing is outputted to the DevConsole.
In line 6. remove the brackets, and remove the ~= nil as they are redundant and only unnecessarily extend your if statement.