How to get a tool a player is holding using a ModuleScript

The reason why I am using a Module is because it’s just easier for what I’m doing lol but here’s my script and error.

local AddItem = {}
local WeldDataModule = require(game.ReplicatedStorage.Modules.KitchenModules.WeldDataModule)
local NextPossibleAddItemTime = 0.5

function AddItem.ColdDrinkCupLid(Player)
	local ItemPath = workspace.Workspace.Cafe.Kitchen.Items.ColdDrinkCupLids
	local ColdDrinkCupLid = game.ReplicatedStorage.Items.ColdDrinkCupLid
	local Sound = ItemPath.TouchPart.Use
	local Item = Player:FindFirstChildOfClass("Tool") 
	local Active = ItemPath:GetAttribute("Active")
	
	if Active == true then
		ItemPath:SetAttribute("Active", false)
		Item:SetAttribute("CanBeUsed", false) --this is the line that erroring out
		Item:SetAttribute("ItemName", "ColdDrinkCup_Lid")
		
		local Lid = ColdDrinkCupLid:Clone()
		Lid.Parent = Item.Handle
		local Weld = Instance.new("Weld")
		Weld.Parent = Item.Handle
		Weld.Part0 = Item.Handle
		Weld.Part1 = Item.Handle.ColdDrinkCupLid
		Weld.C0 = CFrame.new(WeldDataModule.ColdDrinkCupLid)
		
		wait(NextPossibleAddItemTime)
		
		ItemPath:SetAttribute("Active", true)
	end
end

return AddItem

image
i think its erroring bc Item is nil, not sure tho

It seems this is nil. Perhaps you forgot to send the character in the parameter or something? If you are equipping the tool, the tool is in the character. Otherwise, you have the tool in Player.Backpack.

Have you considered instead of a straight module, using a Class. A class would organize your code better and make it easier to make all the functions.

This extendedly changes the approach of writing the code and does not solve the actual problem. Perhaps you are too invested in the stylistic ways of programming?

It is worth a supplementary detail, but it does not satisfy the actual problem in question.

1 Like

Another thing I noticed, try setting your function from a . to a :

function AddItem:ColdDrinkCupLid(Player)

Reference:

1 Like

No matter how many tools that are in my inventory/backpack, the current tool that’s selected is in the character in workspace.

Edit: I just found out that Player is the player in Players, so how would I get it to the workspace?

the item is in the character not the player instance

1 Like
local ch, tool; 
ch = Player.Character or Player.CharacterAdded:Wait(); tool = ch:FindFirstChildOfClass("Tool")
if tool then
   -- do stuff with the tool
end
1 Like