ReplicatedStorage.GunModule:60: invalid argument #2 (string expected, got nil)

I’m making a gunSystem and on this line in the equip function
self.ViewModel = game.ReplicatedStorage.Viewmodelsfolder[self.GunName]:Clone()
I’m getting the error " ReplicatedStorage.GunModule:60: invalid argument #2 (string expected, got nil)"

now I decided to print out all the values in this box
local Gun = {
GunModel = GunTool,
GunName = GunName,
IsEquipped = false,
MaxAmmo = MaxAmmo,
ViewModel = ViewModel,
LoadedAnimations = LoadedAnimations
}
and when they are printed in the contructer they all have values but when they are printed outside of it they are all nil. I have no idea whats going on, and I thank anyone who takes the time to read this!

function GunModule.new(GunTool: Tool)
	
	local GunName = GunTool.Name
	local MaxAmmo = GunTool:GetAttribute("MaxAmmo")
	local AnimationFolder = GunTool.Animations
	local ViewModel = game.ReplicatedStorage.Viewmodelsfolder[GunName]:Clone()
	local Animator = ViewModel.AnimationController.Animator
	
	ViewModel.Parent = game.ReplicatedStorage
	
	local LoadedAnimations = {}
	
	for _, animation in AnimationFolder:GetChildren() do
		local animationTrack = Animator:LoadAnimation(animation)
		LoadedAnimations[animation.Name] = animationTrack
	end
	
	print(GunName)
	
	local Gun = {
		GunModel = GunTool,
		GunName = GunName,
		IsEquipped = false,
		MaxAmmo = MaxAmmo,
		ViewModel = ViewModel,
		LoadedAnimations = LoadedAnimations
	}
		
	setmetatable(Gun, GunModule)
	return Gun
end

function GunModule:Equip() 
	
	print(self.GunName)
	print(self.MaxAmmo)
	print(self.GunModel)
	print(self.GunName)
	
	if self.ViewModel ~= nil then
	   self.ViewModel:PivotTo(Camera.CFrame)
	   self.ViewModel.Parent = Camera
	else
		
		self.ViewModel = game.ReplicatedStorage.Viewmodelsfolder[self.GunName]:Clone()
		self.ViewModel:PivotTo(Camera.CFrame)
		self.ViewModel.Parent = Camera
	end
	
end

My best guess for this error is that for Instance objects, indexing with brackets is just syntactic sugar for calling FindFirstChild.

-- What you wrote
game.ReplicatedStorage.Viewmodelsfolder[self.GunName]:Clone()

-- What actually runs
game.ReplicatedStorage.Viewmodelsfolder:FindFirstChild(self.GunName):Clone()

Since you mentioned everything being nil when ran outside of the constructor, nil is being passed into FindFirstChild when it’s expecting a string.

Based on what you’ve shared, I can’t really see why they’d be nil. How are you calling GunModule.new and GunModule:Equip? Have you tried printing self in GunModule:Equip to see what exactly is being passed into that function?

when I printed self I got this
[“Aim”] = “function”,
[“CheckState”] = “function”,
[“Equip”] = “function”,
[“Fire”] = “function”,
[“LerpNumbers”] = “function”,
[“StopAiming”] = “function”,
[“UnEquip”] = “function”,
[“Update”] = “function”,
[“__Index”] = {},
[“new”] = “function”

also the other stuff is just other functions in my script

Can you show how you’re calling the Equip function? It looks like you’re passing the GunModule object itself as self instead of the object returned from GunModule.new

yes that turned out to be the problem. However now when I use the code it gives me the error

attempt to call missing method ‘Equip’ of table

local Player = game.Players.LocalPlayer
local HRT = Player.Character.HumanoidRootPart
local RS = game:GetService("RunService")
local GunTool = script.Parent
local Camera = game.Workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local GunModule = require(game.ReplicatedStorage.GunModule)

local AK47 = GunModule.new(GunTool)


GunTool.Equipped:Connect(function()	
    AK47:Equip()
	RS.RenderStepped:Connect(function(DeltaTime)
		AK47:Update()
	end)
	
	UIS.InputBegan:Connect(function(input)
		AK47:Aim(input)
	end)

	UIS.InputEnded:Connect(function(input) 
		AK47:StopAiming(input)
	end)
	
	Player.Character.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		AK47:CheckState()
	end)
end)

GunTool.Unequipped:Connect(function()
	GunModule:UnEquip()
end)
 

Looking at your previous comment, you have index set to an empty table?

[“__Index”] = {},

I believe this should be set to GunModule so setmetatable works as intended. Also it should be __index with a lower-case i if it isn’t already in the code.

1 Like

Holy crap that was the problem all along a mispelled capital. thank you man if you had not seen that I would have spent way more time on it.

1 Like

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