Hello! Trying to get more familiar with ModuleScripts here.
Thinking of how I am going to want to look for tools a player has multiple times, in different scripts, I decided to try and make a ModuleScript function which will do this for me.
Module Script
(To note this comes from a ServerScript)
local Common = {}
function Common.FindTool(Plr, Tool)
local Character = Plr.Character
if Character:FindFirstChildWhichIsA("Tool").Name == Tool then
return Character[Tool]
elseif Plr.Backpack:FindFirstChildWhichIsA("Tool").Name == Tool then
return Plr.Backpack[Tool]
else
return false
end
end
return Common
Whenever trying to run the :FindFirstChildWhichIsA() command, I get the following error:
ServerScriptService.CommonFunctions:5: attempt to index nil with 'FindFirstChildWhichIsA'
(And before you ask, yes I tried the :FindFirstChildOfClass() command and it gives the same error.)
Any help?
Edit:
Found out I get a table of arguments on the ModuleScript.
function Common.FindTool(args)
local Plr = args[1]
print(Plr)
Plr really does return nil, but I don’t know why.
It looks like you are sending a table(not a Instance a player must be an Instance) and you try to get the Plr.Character which doesnt exist in the table so it returns nil and nil means nothing you can only use the findfirst blah function on Instance’s
Actually, @riceking10 was correct. You used CommonFunctions:FindTool instead of CommonFunctions.FindTool, which causes complications. Essentially, table:thing(param) is the same as table.thing(table, param). Therefore, you using a colon makes it pass in CommonFunctions as the first parameter. The fix is easy, just use CommonFunctions.FindTool or make it function Common:FindTool(... in the ModuleScript (if you do this then in the function it will set self to CommonFunctions).