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

Why Is My Sound Not A Valid Member Of PlayerGui?

Asked by
TtuNkK 37
6 years ago

So, I'm right now making a part instancing a new sound and playing it, however, I keep getting an error called: Screech is not a valid member of PlayerGui. Someone, please find a fix for this, here is my script:

01screech = script.Parent
02debounce = false
03screech.Touched:connect(function(hit)
04    if not debounce then
05    debounce = true
06    local sound = Instance.new("Sound", game.StarterGui)
07    sound.SoundId = "rbxassetid://1841154098"
08    sound.Volume = 1
09    sound.Looped = true
10    sound.Name = "Screech"
11    game.Players[hit.Parent.Name].PlayerGui.Screech:Play()
12    end
13    end)

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You are using StarterGui as a parent when creating your sound, which doesn't work for your purpose. Instead, change the parent to: game.Players[hit.Parent.Name].PlayerGui.

StarterGui only gets copied to your PlayerGui when you respawn or join the game.

As follows:

01screech = script.Parent
02debounce = false
03screech.Touched:connect(function(hit)
04    if not debounce then
05        debounce = true
06        local sound = Instance.new("Sound", game.Players[hit.Parent.Name].PlayerGui)
07        sound.SoundId = "rbxassetid://1841154098"
08        sound.Volume = 1
09        sound.Looped = true
10        sound.Name = "Screech"
11        game.Players[hit.Parent.Name].PlayerGui.Screech:Play()
12    end
13end)
Ad
Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

StarterGui is a folder that copies it's children and inserts them to a Player's PlayerGui. PlayerGui is where the player would see any ScreenGui and it's GuiObjects inside of it.

In your code, you parent the Sound object to the StarterGui when it should be the PlayerGui.


We can use the :GetPlayerFromCharacter() function to get us the player since the hit parameter in the Touched event could have a Parent that is the Character.

01local Players = game:GetService("Players")
02 
03local screechPart = script.Parent
04local soundId = 1841154098 --Your original sound id to play
05 
06local debounce = false
07 
08screechPart.Touched:Connect(function(hit)
09    local human = hit.Parent:FindFirstChild("HumanoidRootPart")
10    if human then
11        if debounce then return end
12        debounce = true
13 
14        local player = Players:GetPlayerFromCharacter(hit.Parent)
15 
View all 24 lines...

Another thing you might notice is that I included rbxassetid:// to retrieve the proper Id for the Sound object. You'll get a load error without this.

Let me know if you need something explained or if I had missed anything.

Edit:

:connect is deprecated. Use :Connect. You should also Parent instances after setting it's properties, so don't use the second parameter to Instance.new()

Answer this question