Object is sometimes not a valid member other times is with LocalScript

  1. I want to find out why the error is caused + how to fix it and why it sometimes doesn’t show any errors at all.

  2. The issue is that on line 3, it sometimes doesn’t seem to register that Music exists so it shows an error: “… is not a valid member of …”. However, it sometimes works perfectly fine without any errors displaying.

  3. I have tried adding a WaitForChild to wait for the Music to load before it’s being activated and print(). I haven’t found any solutions on the devforum. Both didn’t fix the problem.

This LocalScript is in a GUI button. It’s a button to turn on/off the music when clicked. The music is in the Workspace. It’s there and not in SoundService because the music is strictly only playing in the lobby and no where else.

The LocalScript code:

local Button = script.Parent
local Workspace = game:GetService("Workspace")
local Music = Workspace:WaitForChild("Lobby").MusicPlayer.Music:GetDescendants()
-- The rest of the code is just the Button function. That part itself works perfectly fine. The problem is on line 3.

Any help is greatly appreciated!

1 Like

You also have to do WaitForChild on MusicPlayer and Music.

likely related to StreamingEnabled, in addition to using wait for child, I recommend turning this setting off in Workspace Properties > StreamingEnabled


What does Streaming does exactly?

Makes it so it doesn’t load instances to the clients that are far away (until you get close)

It works. I no longer get the errors. Are there any downsides to disabling Streaming?

Streaming is better for performance, so you don’t load the entire world at once. But you basically have to design your entire experience around it if you enable it

1 Like

Disabling streaming isn’t really recommended. It means that the player is loading the entire game at once, which can lead to lots of lag.

1 Like

It’s not a big deal for my game luckily :smile:. My game is almost entirely full of studs. Barely any meshes or textures and the map is pretty small.

If it’s small, why would the MusicPlayer be outside of the streaming radius? I think that you should just add WaitForChild too all instances on line 3 and renewable streaming. You could also see about increasing the streaming radius.

I assume it’s something with the loading the game part. I never get it when I teleport from the lobby to the map. It’s only when it’s loading the game.

What way do you ‘load’ your game?

It’s just the default Roblox loading screen. The spawn is 20 studs-ish away from the Music player. The map and the lobby is within the radius by 24 studs.

Right. I just want you to test this; renable streaming and edit line 3 to be this and let me know if it errors or not. It’s erroring because the script is trying to find MusicPlayer inside Lobby, which hasn’t loaded in yet.

local Music = Workspace:WaitForChild("Lobby"):WaitForChild("MusicPlayer"):WaitForChild("Music"):GetDescendants()
1 Like

That equally works. Thanks for this!

1 Like