Invalid argument #2 (string expected, got nil)

so i have a slice of code here and it used to work but then i edited it and now it doesnt and i am very sad.

local module = {}

--Services
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local StarterPlayer = game:GetService("StarterPlayer")
local SoundService = game:GetService("SoundService")
local Debris = game:GetService("Debris")

--Folders
local Events = RS.Events
local WeaponsSounds = SoundService.SFX.Weapons
local RSModules = RS.Modules
local SSModules = SS.Modules

--Events
local CombatEvent = Events.Combat
local VFXEvent = Events.VFX

--Modules
local SoundsModule = require(RSModules.Combat.SoundsModule)
local ServerCombatModule = require(SSModules.CombatModule)
local WeaponStatsModules = require(SSModules.Weapons.WeaponStats)
local HelpfulModule = require(SSModules.Other.Helpful)
local StunHandler = require(SSModules.Other.StunHandlerV2)

function module.BodyVelocity(parent, hrp, knockback, stayTime)
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(math.huge, 0, math.huge)
	bv.P = 50000
	bv.Velocity = hrp.CFrame.LookVector * knockback
	bv.Parent = parent
	Debris:AddItem(bv, stayTime)
end

function module.NormalHitboxHit(char, eHum, currentWeapon, Hitbox, hit, hitAnim, swingVFX)
	local eChar = eHum.Parent
	local eHRP = eChar.HumanoidRootPart
	
	
	local WeaponStats = WeaponStatsModules.getStats(currentWeapon)
	local damage = WeaponStats.Damage
	local knockback = WeaponStats.Knockback
	local ragdollTime = WeaponStats.RagdollTime
	local stunTime = WeaponStats.StunTime
	
	if HelpfulModule.CheckForStatus(eChar, char, damage, hit.CFrame, true, true) then return end
	
	eHum:TakeDamage(damage)
	
	ServerCombatModule.stopAnims(eHum)
	
	VFXEvent:FireAllClients("CombatEffects", RS.Effects.Combat[swingVFX], hit.CFrame, 3)
	
	VFXEvent:FireAllClients("Highlight", eChar, .5, Color3.fromRGB(255, 255, 255), Color3.fromRGB(255, 255, 255))

	SoundsModule.PlaySound(WeaponsSounds[weapon].Combat[swingVFX], eChar.Torso)
	
	eHum.Animator:LoadAnimation(hitAnim):Play()
	
	module.BodyVelocity(char.HumanoidRootPart, char.HumanoidRootPart, knockback, 0.25)
	if char:GetAttribute("Combo") == 4 then
		knockback = knockback * 5
		HelpfulModule.Ragdoll(eChar, ragdollTime)
	end

	module.BodyVelocity(eHRP, char.HumanoidRootPart, knockback*1.1, 0.5)
	
	StunHandler.Stun(eHum, stunTime)
	task.wait(0.2)
	ServerCombatModule.stopAnims(eHum)
end

return module

That is the code.
This is the line of code in question that errors:

SoundsModule.PlaySound(WeaponsSounds[weapon].Combat[swingVFX], eChar.Torso)

and this is the playsound function in a separate module script

local module = {}

local Debris = game:GetService("Debris")

function module.PlaySound(Sound, Parent)
	local sound = Sound:Clone()
	sound.Parent = Parent
	sound:Play()
	
	Debris:addItem(sound, 2)
end

return module

Try printing out the two inputs to the function to make sure they’re what you expect:

-- Add these two lines
print(WeaponsSounds[weapon].Combat[swingVFX])
print(eChar.Torso)
-- Above this line
SoundsModule.PlaySound(WeaponsSounds[weapon].Combat[swingVFX], eChar.Torso)
1 Like

That error does not seem to match up with that line of code. Can you please double check that it is correct / provide the entire error (including traceback)

2 Likes

image

just like what it said, it expected a string, but got nothing/something else instead. try extending the code like this so you can get which one is causing the error

SoundsModule.PlaySound(
          WeaponsSounds[weapon].Combat[swingVFX],
          eChar.Torso
)

nevermind, it says argument #2, which means Torso might be the problem

alright it looks like even though it says argument #2 is the thing erroring
WeaponsSounds[weapon].Combat[swingVFX] has something wrong with it
SwingVFX for reference, in this case is a variable set to “blood”

WAIT hold up I think it might be because i need to replace weapon with currentWeapon because of my code, let me test it rq

1 Like

thanks for telling me to print out the first one, i was focused on fiddling with the torso thing because it said argument two but turns out weapon needed to be swapped for currentWeapon, you helped me realize that idk why it said argument two but thanmks

1 Like

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