Should I use _G?

I honestly have no idea what I’m doing to be honest, but made this local script for my character roster and I decided to use _G because I’d like to check in another script when the player hits the “Start” button if _G doesn’t equal nil then like do stuff basically I wanna prevent the player from starting the game without selecting a character basically

local characters = game.ReplicatedStorage.get:InvokeServer()
local roster = game.Players.LocalPlayer.PlayerGui:WaitForChild("Menu").rosterMenu
_G.selected = nil

for i = 1,#characters do
    local button = Instance.new("TextButton",roster)
    button.Name = characters[i]
    button.Text = characters[i]
end

for i,v in pairs(roster:GetChildren()) do
    if v:IsA("TextButton") then
        v.MouseButton1Click:Connect(function()
            _G.selected = v.Name
        end)
    end
end

From my understanding people told me that _G isn’t good to use though I don’t know if that’s true, but yeah anyways I just wanna ask if it’s fine to use _G for this or is there a better way I can do it.

You could use a StringValue, since selected will just contain the Value of a StringValue.

1 Like

_G still works but if you don’t need to share data between scripts then you don’t need to use it at all; you could just use a normal variable.

If you need to be able to share data between scripts, you could use a modulescript to create a shared table, or use bindable events and functions if you need to fire signals between scripts.

Well I have another local script for all my text buttons and I had this is this bad then?

		elseif v.Name == "Start" then
			v.MouseButton1Click:Connect(function()
				if _G.selected ~= nil then
					local selectedChar = _G.selected
					secondMenu.Visible = false
					camera.CameraType = Enum.CameraType.Custom
					re:FireServer(selectedChar)
					print(game.Players.LocalPlayer.Name.." is playing as "..selectedChar)
				else
					print("No Character Selected")
				end
			end)				
		end

In that case I would just use a StringValue object like @sjr04 suggested.

Honestly, I’d advice not to since I’ve heard its outdated and stuff. I tried using _G for an admin panel, didnt work out and just used a shared module instead.