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

Why Is My Sound Not A Valid Member Of PlayerGui?

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

✓ Accepted answer

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)

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()

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