Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My script is failing to play a sound, though it plays in studio, but not in-game?

Asked by 6 years ago
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)
0
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. ConnorThomp 87 — 6y
0
In line 6. remove the brackets, and remove the ~= nil as they are redundant and only unnecessarily extend your if statement. User#19524 175 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago

This is because you tried to access the PlayerGui from the server. Place the sound in the part, and play it there.

0
Will it still play locally if I do that? ConnorThomp 87 — 6y
0
You could do SoundService:PlayLocalSound(sound) if you want it to be local. User#19524 175 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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.

Log in to vote
0
Answered by 6 years ago

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.

Answer this question