Issue with finding player

im getting the error
image
its from local char = player.Character or player.CharacterAdded:Wait()
I’m not sure why its returning nil for player,
WeaponHandler.EquipWeapon(player, id, name)
in my other script the player variable is called correctly

local WeaponHandler = {}
local rs     = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("RemoteEvents")

function WeaponHandler.HoldWeapon(player, Id, WeaponName, WeaponType)
	local char     = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end

	local equippedId = char:GetAttribute("WeaponEquipedId")
	
	local tool
	for _, v in ipairs(player.Backpack:GetChildren()) do
		if v:GetAttribute("uniqueId") == Id then
			tool = v
			break
		end
	end
	if not tool then
		warn("HoldWeapon: tool with uniqueId", Id, "not found")
		return
	end
	
	humanoid:EquipTool(tool)
	

	if equippedId == Id then
		print("Holding equipped weapon:", WeaponName)
	else
		print("Holding weapon:", WeaponName)
	end
end

function WeaponHandler.EquipWeapon(player, Id, WeaponName, WeaponType)
	local char = player.Character or player.CharacterAdded:Wait()
	local weaponid = char:GetAttribute("WeaponEquipedId")

	if Id == weaponid then
		local m1event = events:WaitForChild("Combat"):WaitForChild("M1")
		m1event:FireClient(player)
		return
	end

	char:SetAttribute("WeaponEquipedId",Id)
	
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	local tool = player.Backpack:FindFirstChild(WeaponName) or char:FindFirstChild(WeaponName)
	if humanoid and tool then
		WeaponHandler.UnholdWeapon()
		humanoid:EquipTool(tool)
	end

	print("Equipped weapon:", WeaponName)
end

function WeaponHandler.UnholdWeapon(player, Id, WeaponName, WeaponType)
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end

	local equippedId = char:GetAttribute("WeaponEquipedId")

	if equippedId == Id then
		humanoid:UnequipTools()
		print("Unholding equipped weapon:", WeaponName)
	else
		humanoid:UnequipTools()
		print("Unheld weapon:", WeaponName)
	end
end

function WeaponHandler.Requirements(player, WeaponName,HvyStat,MedStat,LightStat,WeaponGrade,Rank)
	return true
end

return WeaponHandler

The issue is actually here. Please share with us where you call the WeaponHandler functions so we can verify if you actually pass the player correctly to the function.



local Players           = game:GetService("Players")
local UserInputService  = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game:GetService("Players").LocalPlayer
local hotbar       = script.Parent
local inventory    = player.PlayerGui:WaitForChild("ToolBarGui"):WaitForChild("Inventory")
local remoteFolder = ReplicatedStorage:WaitForChild("RemoteEvents")
local WeaponHandler = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("WeaponHandler"))

local chr = player.Character or player.CharacterAdded:Wait()


local keyMap = {
	[Enum.KeyCode.One]   = 1, [Enum.KeyCode.KeypadOne]   = 1,
	[Enum.KeyCode.Two]   = 2, [Enum.KeyCode.KeypadTwo]   = 2,
	[Enum.KeyCode.Three] = 3, [Enum.KeyCode.KeypadThree] = 3,
	[Enum.KeyCode.Four]  = 4, [Enum.KeyCode.KeypadFour]  = 4,
	[Enum.KeyCode.Five]  = 5, [Enum.KeyCode.KeypadFive]  = 5,
	[Enum.KeyCode.Six]   = 6, [Enum.KeyCode.KeypadSix]   = 6,
	[Enum.KeyCode.Seven] = 7, [Enum.KeyCode.KeypadSeven] = 7,
	[Enum.KeyCode.Eight] = 8, [Enum.KeyCode.KeypadEight] = 8,
	[Enum.KeyCode.Nine]  = 9, [Enum.KeyCode.KeypadNine]  = 9,
}

local selectedSlot
local selectedIcon
local originalSizes = {}


local function findIconForSlot(index)
	if inventory.Visible then
		local slotFrame = hotbar:FindFirstChild("Slot" .. index)
		if slotFrame then
			for _, child in ipairs(slotFrame:GetChildren()) do
				if child:GetAttribute("ItemType") then
					return child
				end
			end
		end
	else
		for _, child in ipairs(hotbar:GetChildren()) do
			if child:GetAttribute("ItemType") and child:GetAttribute("SlotIndex") == index then
				return child
			end
		end
	end
	return nil
end


local function deselectCurrent()
	if selectedSlot then
		selectedSlot.BackgroundTransparency = 1
		selectedSlot = nil
	end
	if selectedIcon then
		local t = selectedIcon:GetAttribute("ItemType")
		if t == "Weapon" or t == "Item" then
			local char = player.Character
			if char then
				local humanoid = char:FindFirstChildOfClass("Humanoid")
				if humanoid then 
					humanoid:UnequipTools() 
					local id   = selectedIcon:GetAttribute("uniqueId")
					local name = selectedIcon:GetAttribute("ItemName")
					WeaponHandler.UnholdWeapon(player, id, name)
				end
			end
			local orig = originalSizes[selectedIcon]
			if orig then selectedIcon.Size = orig end
		end
	end
	selectedIcon = nil
end


local function selectSlot(index)
	local frame = hotbar:FindFirstChild("Slot" .. index)
	local icon  = findIconForSlot(index)


	if not icon then
		deselectCurrent()
		return
	end


	if icon == selectedIcon then
		deselectCurrent()
		return
	end


	deselectCurrent()


	if frame then
		selectedSlot = frame
		frame.BackgroundTransparency = 0.5
	end


	local t = icon:GetAttribute("ItemType")
	if t == "Weapon" or t == "Item" then
		if not originalSizes[icon] then originalSizes[icon] = icon.Size end
		local os = originalSizes[icon]
		icon.Size = UDim2.new(os.X.Scale * 1.2, os.X.Offset * 1.2,
			os.Y.Scale * 1.2, os.Y.Offset * 1.2)
	end

	selectedIcon = icon


	if icon:GetAttribute("ItemType") == "Weapon" then
		local id   = icon:GetAttribute("uniqueId")
		local name = icon:GetAttribute("ItemName")
		WeaponHandler.HoldWeapon(player, id, name)
	end
end


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end


	local idx = keyMap[input.KeyCode]
	if idx then
		selectSlot(idx)
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 and selectedIcon then
		local t    = selectedIcon:GetAttribute("ItemType")
		local id   = selectedIcon:GetAttribute("uniqueId")
		local name = selectedIcon:GetAttribute("ItemName")

		if t == "Weapon" then
			WeaponHandler.EquipWeapon(player, id, name)
		elseif t == "Item" then
			print("Using item: " .. name)
		end
		return
	end


	if input.KeyCode == Enum.KeyCode.Backspace and selectedIcon then
		local t    = selectedIcon:GetAttribute("ItemType")
		local id   = selectedIcon:GetAttribute("uniqueId")
		local name = selectedIcon:GetAttribute("ItemName")

		if t == "Weapon" then
			
			if chr:GetAttribute("WeaponEquipedId") == id then return end
			print("drop item:", name)
		elseif t == "Item" then
			print("drop item:", name)
		end
	end
end)

here it is

Question: Is it a server or local script?

it is a local script (word limit)

Here are the corrected versions:

Module:

local WeaponHandler = {}
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("RemoteEvents")

function WeaponHandler.HoldWeapon(player, Id, WeaponName, WeaponType)
    if not player then
        warn("HoldWeapon: player is nil")
        return
    end

    local char = player.Character or player.CharacterAdded:Wait()
    local humanoid = char:FindFirstChildOfClass("Humanoid")
    if not humanoid then return end

    local equippedId = char:GetAttribute("WeaponEquipedId")
    
    local tool
    for _, v in ipairs(player.Backpack:GetChildren()) do
        if v:GetAttribute("uniqueId") == Id then
            tool = v
            break
        end
    end
    if not tool then
        warn("HoldWeapon: tool with uniqueId", Id, "not found")
        return
    end
    
    humanoid:EquipTool(tool)
    
    if equippedId == Id then
        print("Holding equipped weapon:", WeaponName)
    else
        print("Holding weapon:", WeaponName)
    end
end

function WeaponHandler.EquipWeapon(player, Id, WeaponName, WeaponType)
    local char = player.Character or player.CharacterAdded:Wait()
    local weaponid = char:GetAttribute("WeaponEquipedId")

    if Id == weaponid then
        local m1event = events:WaitForChild("Combat"):WaitForChild("M1")
        m1event:FireClient(player)
        return
    end

    char:SetAttribute("WeaponEquipedId", Id)
    
    local humanoid = char:FindFirstChildOfClass("Humanoid")
    local tool = player.Backpack:FindFirstChild(WeaponName) or char:FindFirstChild(WeaponName)
    if humanoid and tool then
        WeaponHandler.UnholdWeapon(player, Id, WeaponName, WeaponType)
        humanoid:EquipTool(tool)
    end

    print("Equipped weapon:", WeaponName)
end

function WeaponHandler.UnholdWeapon(player, Id, WeaponName, WeaponType)
    if not player then
        warn("UnholdWeapon: player is nil")
        return
    end

    local char = player.Character or player.CharacterAdded:Wait()
    local humanoid = char:FindFirstChildOfClass("Humanoid")
    if not humanoid then return end

    local equippedId = char:GetAttribute("WeaponEquipedId")

    if equippedId == Id then
        humanoid:UnequipTools()
        print("Unholding equipped weapon:", WeaponName)
    else
        humanoid:UnequipTools()
        print("Unheld weapon:", WeaponName)
    end
end

function WeaponHandler.Requirements(player, WeaponName, HvyStat, MedStat, LightStat, WeaponGrade, Rank)
    return true
end

return WeaponHandler

Script:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game:GetService("Players").LocalPlayer
local hotbar = script.Parent
local inventory = player.PlayerGui:WaitForChild("ToolBarGui"):WaitForChild("Inventory")
local remoteFolder = ReplicatedStorage:WaitForChild("RemoteEvents")
local WeaponHandler = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("WeaponHandler"))

local chr = player.Character or player.CharacterAdded:Wait()

local keyMap = {
    [Enum.KeyCode.One] = 1, [Enum.KeyCode.KeypadOne] = 1,
    [Enum.KeyCode.Two] = 2, [Enum.KeyCode.KeypadTwo] = 2,
    [Enum.KeyCode.Three] = 3, [Enum.KeyCode.KeypadThree] = 3,
    [Enum.KeyCode.Four] = 4, [Enum.KeyCode.KeypadFour] = 4,
    [Enum.KeyCode.Five] = 5, [Enum.KeyCode.KeypadFive] = 5,
    [Enum.KeyCode.Six] = 6, [Enum.KeyCode.KeypadSix] = 6,
    [Enum.KeyCode.Seven] = 7, [Enum.KeyCode.KeypadSeven] = 7,
    [Enum.KeyCode.Eight] = 8, [Enum.KeyCode.KeypadEight] = 8,
    [Enum.KeyCode.Nine] = 9, [Enum.KeyCode.KeypadNine] = 9,
}

local selectedSlot
local selectedIcon
local originalSizes = {}

local function findIconForSlot(index)
    if inventory.Visible then
        local slotFrame = hotbar:FindFirstChild("Slot" .. index)
        if slotFrame then
            for _, child in ipairs(slotFrame:GetChildren()) do
                if child:GetAttribute("ItemType") then
                    return child
                end
            end
        end
    else
        for _, child in ipairs(hotbar:GetChildren()) do
            if child:GetAttribute("ItemType") and child:GetAttribute("SlotIndex") == index then
                return child
            end
        end
    end
    return nil
end

local function deselectCurrent()
    if selectedSlot then
        selectedSlot.BackgroundTransparency = 1
        selectedSlot = nil
    end
    if selectedIcon then
        local t = selectedIcon:GetAttribute("ItemType")
        if t == "Weapon" or t == "Item" then
            local char = player.Character
            if char then
                local humanoid = char:FindFirstChildOfClass("Humanoid")
                if humanoid then 
                    humanoid:UnequipTools() 
                    local id = selectedIcon:GetAttribute("uniqueId")
                    local name = selectedIcon:GetAttribute("ItemName")
                    WeaponHandler.UnholdWeapon(player, id, name, nil)
                end
            end
            local orig = originalSizes[selectedIcon]
            if orig then selectedIcon.Size = orig end
        end
    end
    selectedIcon = nil
end

local function selectSlot(index)
    local frame = hotbar:FindFirstChild("Slot" .. index)
    local icon = findIconForSlot(index)

    if not icon then
        deselectCurrent()
        return
    end

    if icon == selectedIcon then
        deselectCurrent()
        return
    end

    deselectCurrent()

    if frame then
        selectedSlot = frame
        frame.BackgroundTransparency = 0.5
    end

    local t = icon:GetAttribute("ItemType")
    if t == "Weapon" or t == "Item" then
        if not originalSizes[icon] then originalSizes[icon] = icon.Size end
        local os = originalSizes[icon]
        icon.Size = UDim2.new(os.X.Scale * 1.2, os.X.Offset * 1.2,
            os.Y.Scale * 1.2, os.Y.Offset * 1.2)
    end

    selectedIcon = icon

    if icon:GetAttribute("ItemType") == "Weapon" then
        local id = icon:GetAttribute("uniqueId")
        local name = icon:GetAttribute("ItemName")
        WeaponHandler.HoldWeapon(player, id, name, nil)
    end
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end

    local idx = keyMap[input.KeyCode]
    if idx then
        selectSlot(idx)
        return
    end

    if input.UserInputType == Enum.UserInputType.MouseButton1 and selectedIcon then
        local t = selectedIcon:GetAttribute("ItemType")
        local id = selectedIcon:GetAttribute("uniqueId")
        local name = selectedIcon:GetAttribute("ItemName")

        if t == "Weapon" then
            WeaponHandler.EquipWeapon(player, id, name, nil)
        elseif t == "Item" then
            print("Using item: " .. name)
        end
        return
    end

    if input.KeyCode == Enum.KeyCode.Backspace and selectedIcon then
        local t = selectedIcon:GetAttribute("ItemType")
        local id = selectedIcon:GetAttribute("uniqueId")
        local name = selectedIcon:GetAttribute("ItemName")

        if t == "Weapon" then
            if chr:GetAttribute("WeaponEquipedId") == id then return end
            print("drop item:", name)
        elseif t == "Item" then
            print("drop item:", name)
        end
    end
end)

This works very well, thanks alot!

1 Like

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