Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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!

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. ScrewDeath 153 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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.

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. User#19524 175 — 5y
0
Ah, okay. That makes more sense. I was under the impression that there was only one _G table. pick1ehead 53 — 5y
0
I will take these into consideration, thanks for the details. Nikkasfied 43 — 5y
Ad

Answer this question