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

How come the error keeps saying "attempt to index a nil value"?[UNSOLVED]

game.Players.ChildAdded:connect(function(child)
    local function PlaySound(id)
        local sound = Instance.new("Sound",child:findFirstChild("PlayerGui"))
        sound.SoundId = "http://www.roblox.com/asset/?id= "..tostring(id)
        sound.Volume = 1
        sound:Play()
        repeat sound:Play() wait(2.5) sound:Stop() wait(2.5) sound:Play() until sound.IsPlaying
        TextBox = child:findFirstChild("PlayerGui"):findFirstChild("ScreenGui").Music.Enter
        TextButton = TextBox:findFirstChild("Play")
    end
        TextButton.MouseButton1Click:connect(function() 
            PlaySound(tonumber(TextBox.Text))
        end)
end)
--I have tried numerous times everythings in the right spot in the GUIS and frames but it keeps erroing with that same annoying error. Any help?

7 comments

1

What line specifically is giving you this error?

0

8-9

0

@EgoMoose

0

does child have a variable / is child defined?

0

yes @doctortardiswho child is from the event chiladded.

0

When you have an error, paste it exactly as a part of the question.

0

Are you using FilteringEnabled?

1 answer

✓ Accepted answer

Style

Tab your code correctly. You should probably also avoid putting 4 statements on a single line...

:findFirstChild is deprecated. Use :FindFirstChild instead.


FindFirstChild

:FindFirstChild can return nil if the object does not exist.

If you are sure the object will exist, there is no reason to use :FindFirstChild:

TextBox = child.PlayerGui.ScreenGui.Music.Enter

Clearly one of these objects does not exist. In that case, you should use :WaitForChild if you are sure they will eventually exist:

TextBox = child:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").Music.Enter

Style

You should use local variables if those variables are only used in one function. It will save yourself headaches later.

3 comments

0

Doesnt work.

0

Why not? What's it do instead?

0

Dude no need to negative vote me. It just doesnt implement a sound in my playergui. Whenever i press the play button. And theres no errors i did what you said.

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