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

"_G." a nil value/Attemp to index a variable starting with _G.?

I am scripting a receptionist check in and it seems to work fine in studio with a localscript, but when I test in online (or in studio testing) "_G." becomes nil or I get an error "attempt to index a nil value," a variable which has _G. in front of it.

script:

script.Parent.Parent.Submit.MouseButton1Click:connect(function()
	
for i,v in pairs(game.Players:GetChildren()) do
	print("Finding User")
	if v.Name == script.Parent.Parent.UsernameInput.Text then
		print("Found User "..v.Name)
		local CurrentUserServing = v.Name
		_G.CurrentUser = v
	end
end

print("Next Step")

------------ FOR CHECK IN 
if script.Parent.Parent.TableType.Text == "Normal" then
	print("Table Type Collected")
	if script.Parent.Parent.SeatNumber.Text == "Two" then
	print("Table Info Established")
	
	local Card = Instance.new("Tool")
	Card.Name = script.Parent.Parent.NumberOfTable.Text.." [] 2 Seater"
	Card.RequiresHandle = false
	Card.Parent = _G.CurrentUser.Backpack
	
	for i,v in pairs(game.Workspace.ReceptionBoard2.MainBoard.SurfaceGui.BackgroundFrame:GetChildren())do
		if script.Parent.Parent.SeatNumber.Text == v.Name then
			v.Occupation.Value = true
			v.Occupation.Value = false
			
			local NameToHold = Instance.new("StringValue")
			NameToHold.Name = "UsernameOwner"
	NameToHold.Value = _G.CurrentUser.Name
    NameToHold.Parent = v	
		end
	end
	end
	end

end)

Thanks!

1 comment

0

I don't think you should be using _G. It's bad practice because it makes you prone to naming conflicts. Maybe consider using a module script? Also, keep in mind that _G is separate for the client and server.

1 answer

If i'm not mistaken, the client (local scripts) does not have permission to write to _G, only to read from it. You would need a server script to be able to write to it. But as ScrewDeath said, using _G just isn't good practice anymore.

3 comments

1

_G can be written from localscripts, only they have their own _G table. So if you defined something with _G on a server script, you'd need to redefine locally.

0

Ah, okay. That makes more sense. I was under the impression that there was only one _G table.

0

I will take these into consideration, thanks for the details.

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