I want to make a system where a table will include player’s userid, so I used this script that someone kindly scripted:
local List = {}
function PlayerAdded(plr)
table.insert(List, plr.UserId)
end
function PlayerRemoving(plr)
local Ind = table.find(List, plr.UserId)
if Ind then table.remove(List, Ind) end
end
game.Players.PlayerAdded:Connect(PlayerAdded)--listener for when a player gets added
game.Players.PlayerRemoving:Connect(PlayerRemoving)--listener for when a player gets removed
for i,v in pairs(game.Players:GetPlayers()) do--add all of our current players into the list
PlayerAdded(v)
end
I want to share the “List” Variable among other scripts, and I need to use module scripts for this action. But I lack understanding with module script, I watched several youtube videos that covers this peculiar topic but I yet have no understanding on how to use them. Can someone please help me or even explain on how to use module scripts?
If the script is already in a module script, you also have to return List. Then on a different script, all you need to do is require the module script.
Module script:
local List = {}
function PlayerAdded(plr)
table.insert(List, plr.UserId)
end
function PlayerRemoving(plr)
local Ind = table.find(List, plr.UserId)
if Ind then table.remove(List, Ind) end
end
game.Players.PlayerAdded:Connect(PlayerAdded)--listener for when a player gets added
game.Players.PlayerRemoving:Connect(PlayerRemoving)--listener for when a player gets removed
for i,v in pairs(game.Players:GetPlayers()) do--add all of our current players into the list
PlayerAdded(v)
end
return List
Thank you for your answer! This worked as expected, keep in mind I have been struggling for this topic around a month now and I think I get the point around module scripts now.
You can also use _G.List = {} however module script is better or declare a variable without using local. Declaring a variable without local just creates a global variable accessible to other scripts. Still, all of this is not recommended by Roblox, I think. Because what would be the point of using module scripts.