Attempt to index nil with 'PlaybackLoudness'

Hello! Basically, I was messing with _G variables to try and make a visualizer with it. Everything went fine until the Local Script didn’t wanna work.

It says that there’s an issue with line 14; Attempt to index nil with ‘playbackLoudness’. This is my script:

--[[

Local Handler.

--]]

wait(3)

local VPart1 = _G.VPart1
local VSound = _G.VSound
local MaxLoudness = 250

while true do
	local Amplitude = math.clamp(VSound.PlaybackLoudness / MaxLoudness, 0, math.huge) -- LINE 14
	VPart1.Size = Vector3.new(Amplitude, Amplitude, Amplitude)
	VPart1.Color = Color3.fromRGB(VSound.PlaybackLoudness/3.9, 0, 0)
	wait(0.01)
end

Yes, the global variables are correct, yes, the lua code above is indeed in a local script too, I really don’t know why it won’t work.

So trace it back… Is VSound = nil? If it is go back to the global variable declaration the problem is further back.

_G.VPart1 = script.VPart
_G.VSound = script.VPart.VMusic

I don’t see a problem there-

Where’s script located?

Remember that anything that’s in ServerScriptService will not replicate to the client. If you try to pass such an object to the client, the client will only see nil.

The localscript (the first script) is inside of the Handler. The handler is a server script inside of Workspace. I used something to parent clones of LocalScript to every player’s gui.

_G only shares the entries between scripts of the same context (local scripts, scripts), meaning that if you add an entry to _G on the server, then other scripts on the server will be able to see it, but local scripts cannot, and vice versa

Why are you using _G anyways? You can try accessing the sound from the client if you can

1 Like

Or if you really need to, have an initialisation remote event or function to send the location of the sound from the server to the client when they are ready for it, if for some reason it would be hard for the local script to find the sound.

1 Like

Wait does this mean that if I make a LocalScript with _G Vars, another Local Script will be able to use them too?

You should be able to, yes.

I would advise against using _G, though. I have not seen anyone use _G on Roblox for a valid reason; people using _G on Roblox are just being lazy when there’s other options to pass values around between scripts.

1 Like