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.
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!
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.