Module not working, showing no errors in output

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

can’t you just print v instead?

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 :expressionless:

local playersWithTool = itemcheck.GetPlayersWithTool()

for i, v in pairs(playersWithTool) do
print(playersWithTool[i])
end
end

return exampleModule

I mean wouldn’t even need pairs, it’s recommended to just simply use “in” now to iterate through for loops.

for i, v in playerswithTool do
print(v)
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

Actually I just tested it and seems like when using pairs it returns without any error when using pairs it works well

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.

Both do the same thing

 for key, value in playersWithTool do
	print(playersWithTool[key])
 end
for i, v in pairs(playersWithTool) do
	print(v)
end

v = value i = item, v will return 0 if there are no player with the Lighter/Flashlight, key will return “Flashlight” and “Lighter”

in conclusion both work.

Thank you for explaining this so well! Studio is currently updating so I will try this later.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.