I am quiet new to using Module Scripts but I attempting to create a table that I can reference on my other scripts.
Here is what I currently have:
Summary
local RunService = game:GetService("RunService")
local Ignored = workspace:WaitForChild("Ignored")
local ignoreList = {}
RunService.RenderStepped:Connect(function()
for i, player in ipairs(game:GetService("Players"):GetPlayers()) do
ignoreList[i] = player.Character
end
for i, v in pairs(workspace.Ignored.Barriers:GetChildren()) do -- Barriers
table.insert(ignoreList, v)
end
for i, v in pairs(workspace.Ignored.Lights:GetChildren()) do -- Lights
table.insert(ignoreList, v)
end
end)
print(ignoreList)
return ignoreList
In order to check if this script was working as intended, I added print functions both on the module and on one of the scripts that I am using this table on.
The print function within the module just prints {} and the print function on the other script prints nil.
If anyone could give me some insight on why I may be having this issue, please feel to comment and get me moving in the right direction!
local IgnoreListM = require(script.Parent.IgnoreListM)
print(IgnoreListM)
This prints {} and {}
I am also not sure if it is needed but I did this as well local ignoreList = IgnoreListM.ignoreList
And this prints nil
Overall it seems to me like the table is not being created correctly somehow? Since the prints are empty or nil
EDIT:
As a side note; do I need RunService for the table to be continuous updating as new players are added or does :GetPlayers() automatically update on its own?
It seems that the module works correctly without the RunService being added.
Your module script returns before the RunService.RunStepped function runs. Therefore at the time it returns the table is still equal to nil. I am assuming you are using this to blacklist parts, you could probably just run this loop in your script right before you raycast. I gtg right now, I can help you after more in like an hour.
No, that’s wrong. You can return anything from a ModuleScript, a number, string, function, table, whatever. Often a table is returned to represent a collection of related values. This return value at the bottom of the ModuleScript is the same value that gets returned with require is called on the ModuleScript. So
local ignoreList = require(script.Parent.IgnoreListModule)
makes IgnoreList refer to the same value that the ModuleScript returns, i.e. the ignoreList that’s also in the ModuleScript. You can’t access different variables in the ModuleScript from the requireing script, that’s just not a thing.
The second print is the same as the first one because that’s the right way to get the ignoreList. They’re empty because the ModuleScript doesn’t set up the ignoreList correctly.
Every time you call GetPlayers you get an up-to-date list of players. But GetPlayer can’t know what you’re doing with that list. It doesn’t know that you’re trying to have an up-to-date list of players elsewhere, so it wont keep your list updated.
You could keep the list updated by updating it in a loop, but that’s a bad way of doing it. Instead, listen for the Players.PlayerAdded and Players.PlayerRemoving events to update the list only when something relevant changes. Updating in a loop is a waste of CPU time and doesn’t really guarantee that your list is always up-to-date.
You could do this like so:
local ignoreList = {}
function updateIgnoreList()
local newIgnoreList = {}
for i, player in ipairs(game:GetService("Players"):GetPlayers()) do
table.insert(ignoreList, player.Character)
end
for i, v in pairs(workspace.Ignored.Barriers:GetChildren()) do -- Barriers
table.insert(ignoreList, v)
end
for i, v in pairs(workspace.Ignored.Lights:GetChildren()) do -- Lights
table.insert(ignoreList, v)
end
ignoreList = newIgnoreList
end
updateIgnoreList() --We want the ignoreList to be updated at the start of the game, even if no players join.
game.Players.PlayerAdded:Connect(function(player)
--Could the player have a character already when PlayerAdded fires? Don't think so, but this catches it.
if player.Character then
updateIgnoreList()
end
--Update every time the player respawns
player.CharacterAdded:Connect(updateIgnoreList)
end)
--Update when a player leaves because their character will be Destroyed
game.Players.PlayerRemoving:Connect(updateIgnoreList)
return ignoreList