Getting Attempt to perform Arithmetic (Mul) on Number and Function Error

Hi I’m currently using a Recoil Pattern Script from this Devforum:

But I’m getting this Error whenever I fire the weapon:

image

And I can’t get any Devforum Information about this Error. This is the Script I modified:

local RecoilRestore = 0.5
local RecoilRecovery = 0.1
local RecoilSides = 10
local RecoilBumps = 10
local RecoilFire = 20
local RecoilObjective = nil

local RecoilPattern = {
	{1, RecoilFire, -1, 0.77, -0.1},
	{6, RecoilBumps/2, - RecoilBumps/2, 0.77, -1 * RecoilSides/25},
	{11, RecoilBumps/2, - RecoilBumps/2, 0.77, RecoilSides/25},
}

local PreviousShot = tick()

local function RecoilLerp(A, B, C)
	return A * (1 - C) + (B * C)
end

local function RecoilFire()
	RecoilObjective = (tick() - PreviousShot > RecoilRestore and 1 or RecoilObjective + 1)
	PreviousShot = tick()
	if RecoilObjective == 1 then
		spawn(function()
			wait(RecoilRecovery)
			local RecoveryAmount = -1/2 * RecoilFire
			local RecoveryLeft = RecoveryAmount
			local RecoveryCurrent = 0.5 * RecoveryAmount
			
			while RecoveryLeft < 0.01 do
				RecoveryCurrent = RecoveryCurrent/1.5
				RecoveryLeft = RecoveryLeft - RecoveryCurrent
				Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(RecoveryCurrent), 0, 0)
				RunService.RenderStepped:Wait()
			end
		end)
	else
		local Shuffler = math.random()
		RecoilObjective = RecoilObjective + math.round(Shuffler)
	end
	if RecoilObjective >= 11 then
		RecoilObjective = 2
	end
	for I, V in pairs(RecoilPattern) do
		if RecoilObjective <= V[1] then
			spawn(function()
				local Number = 0
				while math.abs(Number - V[2]) > 0.01 do
					Number = RecoilLerp(Number, V[2], V[4])
					local Capture = Number/10
					Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(Capture), math.rad(Capture * V[5]), 0)
					RunService.RenderStepped:Wait()
				end
				while math.abs(Number - V[3]) > 0.01 do
					Number = RecoilLerp(Number, V[3], V[4])
					local Capture = Number/10
					Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(Capture), math.rad(Capture * V[5]), 0)
					RunService.RenderStepped:Wait()
				end
			end)
			break
		end
	end
end

V[2] points towards a function, for the first item in the RecoilPattern array:

local RecoilPattern = {
    --    v This is a function
	{1, RecoilFire, -1, 0.77, -0.1},
	{6, RecoilBumps/2, - RecoilBumps/2, 0.77, -1 * RecoilSides/25},
	{11, RecoilBumps/2, - RecoilBumps/2, 0.77, RecoilSides/25},
}

^ This too


Essentially, the variable:

local RecoilFire = 20

and the function

local function RecoilFire()

Have a name conflict; try changing the name of the either the function or the variable

1 Like

Wait so what should I change other than those?

Actually, what he has for the RecoilPattern array is perfectly fine, as he first assigns 20 to a variable called “RecoilFire”, adds it to the array, and then defines the function afterwards. If you run this code you can see it for yourself:

local Num = 20

local Table = {
	{1, Num};
	{2, Num}
}

function Num()
	
end

print(Num) -- Prints function 0x586731b7de2a775a

for _,v in pairs(Table) do
	print(v[2]) -- Prints 20
    print(v[2] * 2) -- Prints 40
end

Just wanted to point that out first. Anyways, @officer_wiki, this part that he mentioned is the problem:

You’re multiplying a number and a function. You should use his suggestion to change either the name of the function or the variable. Everything else seems to be fine.

2 Likes

Alright gonna try it after this

Works. I only renamed RecoilFire into FireRecoil.

You should give the solution to @HugeCoolboy2007, I basically just repeated what he said. I just wanted to point out the part where he mentioned v[2] being a function. :slightly_smiling_face:

1 Like

Already but I ran into the same problem

Now It’s on Nil and Number at:

	RecoilObjective = (tick() - PreviousShot > RecoilRestore and 1 or RecoilObjective + 1)

Well seeing that PreviousShot and RecoilRestore are manually assigned values and RecoilObjective is assigned nil, it’s probably:

RecoilObjective + 1

causing the problem. If RecoilObjective is nil and you try to add a number to it, an error would occur. Try rewriting the line like this:

RecoilObjective = (tick() - PreviousShot > RecoilRestore and 1 or not RecoilObjective and 1 or RecoilObjective + 1)
1 Like

It worked perfectly nice. Thanks!

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