Argument 1 missing or nil in module script

It’s my first time attempting oop and I’m trying to make a combat module. Whenever I call the playsound/playanim function in the attack function or anywhere else in the module script, it just says “Argument 1 missing or nil”.

This is the script currently:

SwordModule.__index = SwordModule

local Weapons = {}




function SwordModule.new(tool: Tool, name: string, damage:  number, knockback:number, attackspeed:number)
	local self = setmetatable({},SwordModule)
	self._tool = tool
	self._name = name
	self._damage = damage
	self._knockback = knockback or 5
	self._attackspeed = attackspeed or 1
	self.cd = false
	
	
	
	return self
end

function SwordModule:PlaySound(setting,parent)
	local WeaponSounds = game.SoundService.Weapons:FindFirstChild(self._name)
	if WeaponSounds then
		local currentsound = WeaponSounds:FindFirstChild(setting):Clone()
		if currentsound and parent then
			currentsound.Parent = parent
			currentsound.PlayOnRemove = true
			currentsound:Destroy()

		end
	end
end

function SwordModule:PlayAnim(setting,char)
	local WeaponAnims = game.ReplicatedStorage.WeaponAnimations:FindFirstChild(self._name)
	if WeaponAnims then
		local currentanim = WeaponAnims:FindFirstChild(setting):Clone()
		local hum = char.Humanoid
		local animator = char.Humanoid.Animator
		local currentanimtrack = animator:LoadAnimation(currentanim)
		local connection
		currentanimtrack:Play()
		local weaponidle
		if setting == "Equip" then
			 connection = currentanimtrack.Stopped:Connect(function()
				weaponidle = animator:LoadAnimation(WeaponAnims.Idle)
				weaponidle:Play()
			end)
			self._tool.Unequipped:Connect(function()
				if connection then
					connection:Disconnect()
					currentanimtrack:Stop()
					if weaponidle then
						weaponidle:Stop()
					end
				end
			end)
		end
		
	end
end





function SwordModule:Attack(root)
	local damagemultiplier = root:FindFirstChild("SwordMultiplier")
	if damagemultiplier and damagemultiplier:IsA("NumberValue") then
		self._damage = self._damage * damagemultiplier.Value
		SwordModule:PlaySound("Attack",root)
	end
end




return SwordModule

Can you show us where you used that method?

local char = plr.Character
local hum = char.Humanoid
local sword = script.Parent


local swordremote = game.ReplicatedStorage.RemoteEvents.SwordEvent


local swordmodule = require(game.ReplicatedStorage.Modules.Combat.SwordCombat)
local swordsetup = swordmodule.new(sword,"LinkedSword",10)



sword.Equipped:Connect(function()
	swordremote:FireServer("Equip")
end)

sword.Activated:Connect(function()
	swordremote:FireServer("Attack")
end)

swordremote.OnClientEvent:Connect(function(action)
	if action == "TriggerEquip" then
		--swordsetup:PlaySound("Equip",char.HumanoidRootPart)
		--swordsetup:PlayAnim("Equip",char)
	elseif action == "AttackClient" then
		if swordsetup.cd then return end
		swordsetup.cd = true
		swordsetup:Attack(char.HumanoidRootPart)
	end
end)```

Does the error message say what line? If not, have you tried debugging with breakpoints? FindFirstChild(), for example, will have that type of error saying Argument 1 missing or nil. Not meaing that FindFirstChild() is the issue in particular, just some built-in function that expects an argument and is either not getting one or is getting a nil value instead.

The error message says these lines Argument 1 missing or nil - Client - SwordCombat:24 21:12:41.476 Stack Begin - Studio 21:12:41.477 Script 'ReplicatedStorage.Modules.Combat.SwordCombat', Line 24 - function PlaySound - Studio - SwordCombat:24 21:12:41.477 Script 'ReplicatedStorage.Modules.Combat.SwordCombat', Line 74 - function Attack - Studio - SwordCombat:74 21:12:41.477 Script 'Players.beastmomki.Backpack.LinkedSword.LocalSword', Line 30 - Studio - LocalSword:30 21:12:41.477 Stack End

I honestly don’t know what is happening because when i call the sound function in any other script it works. I found out whenever I hover root or parent it says their name then “:never” right afterwards.I also tried replacing findfirstchild and the same thing happened.

I assume line 24 is this:

local WeaponSounds = game.SoundService.Weapons:FindFirstChild(self._name)

Can you print what self._name is at this point? It looks like it should be correct, but the error points to this location so I’m not sure.

I printed before and after, when i printed before that point, nil was printed, when I did it afterwards it errored.

I’m not sure why this isn’t working. Can you see what self._name is on initialization with SwordModule.new()? I don’t see it being changed anywhere else, so it should be nil there too if it’s nil anywhere, at least I would think.

Somehow its the name of the tool there. I fixed it or atleast so I think. I had to change from just using :playsound to this SwordModule.new(self._tool,self._name):PlaySound("Attack",root)

Can you see if any of those variables exist at the point where it was showing nil, such as knockback or something?

1 Like

Replace this with:

self:PlaySound("Attack", root)

When you called it like that the self passed in is just an empty table.

2 Likes

THANK YOU SO MUCH it worked :smiley: have a good day

It prints as 5, thanks for the help man

1 Like

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