What can ModuleScripts access?

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.

I think it means that the Character or Plr is nil, as you cant do :FindFirstChildWhichIsA on nil

1 Like

Can you let us know what you’re running when calling the module?

1 Like
Resolved. Read if you like I guess.

Why is it that when I attempt to print the Player I get a table in the output? (From the ModuleScript)

print(Plr)

results in

table: 0x13e4a6736976eff6

When a RemoteEvent fires, the beginning looks for 2 tools I want to use later in the script.

script.Parent.MouseHoverData.Events.F.OnServerEvent:Connect(function(Plr)
	local LegalLock = CommonFunctions:FindTool(Plr, "Personnel Card")
	local ForcedLock = CommonFunctions:FindTool(Plr, "Dummy Card")
	print(LegalLock, ForcedLock)
end)

Use CommonFunctions.FindTool instead of CommonFunctions:FindTool. When you use : I think the first parameter will be the table.

Sorry never mind I don’t think that’ll work

1 Like

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

1 Like

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).

1 Like