Module: attempt to call a nil value

Getting “attempt to call a nil value” and “Requested module experienced an error while loading” errors.
Reason being char(Character) being nil for some reason and I can’t figure out how to fix the problem

HandleBasket Module Code:

local basket = {}
local basketMT = {__index = basket}

local events = game.ReplicatedStorage:WaitForChild("Events")

local function init(self)
	self.joint = Instance.new("Motor6D")
	self.joint.Part0 = self.hand
	self.joint.Parent = self.char

	self.humanoid.Died:Connect(function()
		self.isAlive = false
		self:unequip()
	end)
end

function basket.new(char)
	local self = {}
	
	self.humanoid = char:WaitForChild("Humanoid")
	self.hand = char:WaitForChild("hand")

	self.isEquipped = false
	self.isAlive = true

	init(self)
	return setmetatable(self, basketMT)
end


function basket:equip(repBasket)
	if (not repBasket) then 
		print("not basket")
		self:unequip()
		
		return
	end

	if self.isAlive == false then end
	if self.isEquipped == true then self:unequip() end
	
	print(self.isEquipped)
	print("equip")
	events.Setup:FireServer(repBasket)
	
	self.settings = require(repBasket:WaitForChild("Settings"))
	self.isEquipped = true
end

function basket:unequip()
	print("unequip")
	
	self.isEquipped = false

	if self.basket then self.basket:Destroy() end
end

function basket:click()
	
end

basket:new()

return basket

Controls LocalScript Code:

local caService = game:GetService("ContextActionService")

local repStorage = game.ReplicatedStorage

local plr = game.Players.LocalPlayer
local char = plr.Character

local basket = Instance.new("ObjectValue")
basket.Value = repStorage.Baskets.PicnicBasket
basket.Parent = plr

local hB = require(repStorage.Modules.HandleBasket)
local basket = hB.new(char)

local function equipBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		basket:equip(basket.Value)
		print("equipbind")
	end
	
	return Enum.ContextActionResult.Pass
end

local function clickBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		--basket:click();
	end

	return Enum.ContextActionResult.Pass
end

caService:BindAction("equip", equipBind, false, Enum.KeyCode.F, Enum.KeyCode.ButtonR1)
caService:BindAction("click", clickBind, false, Enum.UserInputType.MouseButton1)

You need to wait for the module to load before you require it. Use WaitForChild to do so.

local hB = require(repStorage:WaitForChild("Modules"):WaitForChild("HandleBasket"))
local basket = hB.new(char)
1 Like

In your first module script you call basket:new() with no parameters. I believe if you remove this line everything should be ok, it is very out of place.

I didn’t even see that that line, with your help and @bitsNbytez it worked.

1 Like

It works now when I wait, thank you!

1 Like