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
5 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:

screech = script.Parent
debounce = false
screech.Touched:connect(function(hit)
    if not debounce then
    debounce = true
    local sound = Instance.new("Sound", game.StarterGui)
    sound.SoundId = "rbxassetid://1841154098"
    sound.Volume = 1
    sound.Looped = true
    sound.Name = "Screech"
    game.Players[hit.Parent.Name].PlayerGui.Screech:Play()
    end
    end)

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 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:

screech = script.Parent
debounce = false
screech.Touched:connect(function(hit)
    if not debounce then
        debounce = true
        local sound = Instance.new("Sound", game.Players[hit.Parent.Name].PlayerGui)
        sound.SoundId = "rbxassetid://1841154098"
        sound.Volume = 1
        sound.Looped = true
        sound.Name = "Screech"
        game.Players[hit.Parent.Name].PlayerGui.Screech:Play()
    end
end)
Ad
Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 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.

local Players = game:GetService("Players")

local screechPart = script.Parent
local soundId = 1841154098 --Your original sound id to play

local debounce = false

screechPart.Touched:Connect(function(hit)
    local human = hit.Parent:FindFirstChild("HumanoidRootPart")
    if human then
        if debounce then return end
        debounce = true

        local player = Players:GetPlayerFromCharacter(hit.Parent)

        local sound = Instance.new("Sound")
        sound.SoundId = "rbxassetid://"..soundId
        sound.Volume = .5
        sound.Looped = true
        sound.Parent = player.PlayerGui

        sound:Play()
    end
end)

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