Module script refusing to parent Part1 to weapon model despite a Motor6D existing

Heya Everyone!!

I’m working on a first person shooter, side project. There is a server script that adds a Motor6D whenever the player spawns in. The Motor6D will pretty much position the weapon alongside being used to animate it.

The issue is that the module script is refusing to parent the weapon model to the Motor6D’s Part1 because it keeps acting as if it doesn’t exist. I really don’t know why it’s acting like this.

WEAPON_CONTROLLER (Binds weapon controls such as firing or reloading)

--[[SERVICES]]--
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

--[[LOCAL PLAYER AND CHARACTER MODEL]]--
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

--[[MODULE]]--
local WEAPON_HANDLER = require(game:GetService("ReplicatedStorage").CLIENT_MODULES.WEAPON_HANDLER)

--[[CONTROLS]]--
WEAPON_HANDLER.New(Character)
local DefaultWeaponInventory = {
	"Pistol" --//Will be switched to the Linked Sword later.
}

local function EQUIP()
	WEAPON_HANDLER:EquipWeapon("Pistol")
end

local function FIRE()
	--//TBA
end

local function RELOAD()
	--//TBA
end

EQUIP()
ContextActionService:BindAction("FireWeapon", FIRE, true, Enum.UserInputType.MouseButton1)
ContextActionService:BindAction("ReloadWeapon", RELOAD, true, Enum.KeyCode.R)

--[[VIEWMODEL]]--
--//This isn't really actually a viewmodel.
--//This is actually just setting the arms visible so I don't need to worry about making an actual viewmodel.
game:GetService("RunService").RenderStepped:Connect(function()
	for _, Part in pairs(Character:GetChildren()) do
		if Part:IsA("Part") and (Part.Name == "Right Arm" or Part.Name == "Left Arm") then
			Part.LocalTransparencyModifier = 0
		end
	end
end)

WEAPON_HANDLER (Holds all the base code of the weapons)

--[[SERVICES]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[FOLDERS]]--
local WEAPONS = ReplicatedStorage:WaitForChild("WEAPONS")
local EVENTS = ReplicatedStorage:WaitForChild("EVENTS")

--[[MODULE]]--
local WEAPON_HANDLER = {}
WEAPON_HANDLER.__index = WEAPON_HANDLER

function WEAPON_HANDLER.New(CharacterModel) --//This is really just getting the player model and humanoid
	local self = {}
	
	self.Character = CharacterModel
	self.Humanoid = self.Character:FindFirstChildWhichIsA("Humanoid")
	self.Motor6D = self.Character["Right Arm"]:FindFirstChild("HandleGrip")
	
	return self
end

function WEAPON_HANDLER:EquipWeapon(RequestedWeapon)
	self.Weapon = WEAPONS:FindFirstChild(RequestedWeapon)
	self.WeaponStats = require(self.Weapon:FindFirstChild("WeaponSettings"))
	
	--//Welding the weapon onto the player.
	self.Motor6D.Part1 = self.Weapon.Handle -- This is where the error keeps appearing.
	
	--//After we're done positioning the weapon to the player, replicate it to the server
	EVENTS.REPLICATE_WEAPON_TO_SERVER:FireServer(RequestedWeapon)
end

function WEAPON_HANDLER:FireWeapon()
	--//TBA
end

function WEAPON_HANDLER:ReloadWeapon()
	--//TBA
end

return WEAPON_HANDLER

I still do this all the time. Your calling :Equip on the module, not the constructed object.

local PlayersWeaponHandler = WEAPON_HANDLER.New(Character)

local function EQUIP()
	PlayersWeaponHandler:EquipWeapon("Pistol")
end

--| Instead of 
WEAPON_HANDLER.New(Character)

local function EQUIP()
	WEAPON_HANDLER:EquipWeapon("Pistol")
end

Oh also your constructor doesn’t initialize the table properly

function WEAPON_HANDLER.New(CharacterModel) --//This is really just getting the player model and humanoid
	local self = setmetatable({}, WEAPON_HANDLER) -- <><><> You're missing this part <><><> --
	
	self.Character = CharacterModel
	self.Humanoid = self.Character:FindFirstChildWhichIsA("Humanoid")
	self.Motor6D = self.Character["Right Arm"]:FindFirstChild("HandleGrip")
	
	return self
end

Thank you very much! I actually really don’t know why I would be using a .New() function but now I understand why.

1 Like

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