Im working on a system to check how many players have a certain tool, and it isn’t working. I made a whole module for it so I could use it for multiple scripts. I called it in the main module and ran a loop through the table to print how many players have each tool and it doesn’t even show up in the output. It’s not a problem with the rest of the main module, I already checked that it still works.
--module in replicated storage
local itemcheck = {}
function itemcheck.GetPlayersWithTool()
local playersWithTool = {
["Flashlight"] = 0;
["Lighter"] = 0;
}
for _, player:Player in ipairs(game.Players:GetChildren()) do
for i, value in ipairs(playersWithTool) do
if player.Backpack:FindFirstChild(i) or player.Character:FindFirstChild(i) then
playersWithTool[i] += 1
end
end
end
return playersWithTool
end
return itemcheck
--module in server script service (called by parent script)
local exampleModule = {}
function exampleModule.exampleFunction()
local itemcheck = require(game.ReplicatedStorage.ItemCheck) --wont let me indent :|
local playersWithTool = itemcheck.GetPlayersWithTool()
for i, v in ipairs(playersWithTool) do
print(playersWithTool[i])
end
end
return exampleModule
Make sure when calling another module from a module you do not have cyclic dependencies, it’s going to make an endless loop, you should add a server-side script to call the functions
I just noticed he use ipairs, wouldn’t pairs work better?
local itemcheck = {}
function itemcheck.GetPlayersWithTool()
local playersWithTool = {
[“Flashlight”] = 0;
[“Lighter”] = 0;
}
for _, player:Player in ipairs(game.Players:GetChildren()) do
for i, value in pairs(playersWithTool) do
if player.Backpack:FindFirstChild(i) or player.Character:FindFirstChild(i) then
playersWithTool[i] += 1
end
end
end
return playersWithTool
end
return itemcheck
2nd module:
local exampleModule = {}
function exampleModule.exampleFunction()
local itemcheck = require(game.ReplicatedStorage.ItemCheck) --wont let me indent
local playersWithTool = itemcheck.GetPlayersWithTool()
for i, v in pairs(playersWithTool) do
print(playersWithTool[i])
end
end
Actually I just realised your issue is due to using “ipairs” because you have a dictionairy, ipairs simply doesn’t work on dictionairies and only works on arrays.
So instead use this:
for key, value in ipairs(playersWithTool) do
print(key, value)
end
Yes it does, but it also works the same with “in”. They both work identical to each other.
Ipairs doesn’t work on dictionairies however, that’s why it does not print.
Also I realise now what OP was trying to index the key with the dictionary, it’s the “index, value” that confused me. It should actually be read like this:
for key, value in playersWithTool do
print(playersWithTool[key])
end
But OP can simply just print the value since indexing the key with the dictionary returns the value stored within that key.
for key, value in playersWithTool do
print(value)
end
This is because ipairs returns an iterator that uses numeric index’, so you’re basically indexing the dictionary with numbers while it only has strings as keys.
My recommendation: Just avoid using pairs or ipairs, they are quite obsolete and pointless compared to a raw iterator.