A few questions (globals+filter enabled)

I’ve recently come back to ROBLOX, 2 questions. First is _G no longer a thing for globals?

I tried searching up some examples of Global variable in ROBLOX and couldn’t find anything.
I believe it used to be

_G.x = 5

print(x) → 5 ?

Or is anything that doesn’t have local before it now considered global?

And is there some good tutorials on how to code with Filtering Enabled, I never got into that back in the day before I quit.

In vanilla Lua, yes. Globals are implicitly added to the _G table which is the global environment. In Roblox this is changed for security reasons.

In Lua, variables are global by default, so yes, if you don’t prefix the declaration with local it is a global variable. Using local variables is good practice so you should use them.

Note that “Filtering Enabled” is now an archaic term. All games are forced to use this now so referring to a game as an “FE” game is redundant.

1 Like

Hey another question. I’ve got this code.

local players = {}
local i = 0
function restockPlayers ()
print(“test”)
for i = 1,#players do

end

end

game.Players.PlayerAdded:connect(restockPlayers)

How can I get the newest player who joined? Like how do I get reference to that or store him in a variable?

Would it be this?

local players = {}

local i = 0

function restockPlayers ()

print(“test”)

players[#players+1] = game.Players.PlayerAdded

end

game.Players.PlayerAdded:connect(restockPlayers)

PlayerAdded event listeners get a reference to the player who joined, as an argument.

local Players = game:GetService("Players")
local players_table = { }

local function restock_players(player)
    print("test")
end

Players.PlayerAdded:Connect(restock_players)

And from there you would use the player parameter. Remember to remove the player from the table when they leave, otherwise you will leak memory!

What does this do?

local Players = game:GetService("Players")

Or what would that variable be used for?

Pardon – I forgot to connect the event. game:GetService gets a service by its ClassName.

I have edited the post to reflect this.

Oh I see.

So would this be correct?

local players = {}

local Players = game:GetService("Players")

local function restockPlayers (player)

table.insert(players,player)

end

local function removePlayers (player)

table.remove(players,player)

end

Players.PlayerAdded:connect(restockPlayers)

Players.PlayerRemoving:connect(removePlayers)

– Will this sort my array? Or would it leave gaps, blank indexes I mean. If 3 players join
Player 1 [0] P2 [1] P3 [2]

I remove player 2, does index 1 then become blank? Or does table.remove sort the index sort of speak?

Instead of reinventing the wheel, use Players:GetPlayers(). It returns a new table everytime you call it, and you can guarantee it will always be accurate.

But when you use table.remove to remove a value that is not the last one in a table, it will shift all the elements to the right of it forward one to the left to fill in the gap created.

i.e.

local t = { "a", "b", "c" }
table.remove(t, 2)
print(t[1], t[2]) -- a c

And table.remove removes an element based on an index, not value.

1 Like

ah perfect thanks! (why does a post require 30 characters lol)