Error with Magnitude?

Hello! I tried to recreate my hitbox module in order for it to be used for other attacks or systems I might need, so I put it all in a module. However, this is my first time using one so I believe I have made a mistake… Here is my ModuleScript code:

local CharModule = {
	--First character: Mario
	["Mario"] = {
		--Mario Stats
		["Stats"] = {
			["Weight"] = 100;
			["BaseKnockBack"] = 20;
			["KnockBackGrowth"] = 100
		}
	}
}

function CharModule.Attack(c, Range, Damage, Knockback, Force)
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") then
			if (c.HumanoidRootPart.CFrame - v.HumanoidRootPart.CFrame).Magnitude < 5 then
				if ((c.HumanoidRootPart.CFrame - v.HumanoidRootPart.CFrame)):Dot( c.HumanoidRootPart.CFrame.lookVector ) < 0 then

					v.Conditions.IsAttacking.Value = true
					local CurrentDamage = v.Conditions.CurrentDamage
					v.Humanoid:TakeDamage(Damage)
					CurrentDamage.Value += 1
					if v:FindFirstChild("BillboardGui"):FindFirstChild("TextLabel") then
						v.BillboardGui.TextLabel.Text = CurrentDamage.Value .. "%"
					end
					if Knockback == true then
						local Vel = Instance.new("BodyVelocity")
						Vel.Parent = v.HumanoidRootPart
						Vel.MaxForce = Vector3.new(5000, math.floor(math.random(5000)), 0)
						local Weight = CharModule["Mario"]["Stats"]["Weight"]
						local BKB = CharModule["Mario"]["Stats"]["BaseKnockBack"]
						local KBG = CharModule["Mario"]["Stats"]["KnockBackGrowth"]
						Vel.Velocity = c.HumanoidRootPart.CFrame.LookVector * (Weight/4) + CurrentDamage.Value*Force/1.25
						game.Debris:AddItem(Vel, 0.3)
						v.Conditions.IsAttacking.Value = false
					end
				end
			end
		end
	end
end
return CharModule

Server Script now:

local CharModule = require(game.ReplicatedStorage.CharModule)
game.ReplicatedStorage.CombatEvents.Jab.OnServerEvent:Connect(function(Player, Action)
	local char = Player.Character
	print(char.Name)
	if Action == "Jab1" and not char:FindFirstChild("Conditions"):FindFirstChild("IsStunned").Value == true then
		CharModule:Attack(char, 5, 1, false, 0)
	elseif Action == "Jab2" and not char:FindFirstChild("Conditions"):FindFirstChild("IsStunned").Value == true then
		CharModule:Attack(char, 5, 2, false, 0)
	elseif Action == "Jab3" and not char:FindFirstChild("Conditions"):FindFirstChild("IsStunned").Value == true then
		CharModule:Attack(char, 5, 3, true, 3)
	end
end)

I printed char to see if it existed, and it does. Therefore, I’m not sure what the error is…
The error message I’m getting is: “attempt to index nil with 'CFrame” on line 16 of the module script, nothing for the server. Please help!

1 Like

Hi,

I think you need to define attack with a : not a . here

function CharModule.Attack(c, Range, Damage, Knockback, Force)
should be
function CharModule:Attack(c, Range, Damage, Knockback, Force)

Because then that matches up with you calling CharModule:Attack in your server script

I hope that helps,

1 Like

Alright I will try it out, give me a second

It worked! Thanks, I was actually really doubtful this worked but it did, thank you again and sorry for the doubt…

1 Like