Attempt to perform arithmetic (sub) on Vector3 and number

So, I have a camera shaking system for explosions. But for some reason it is producing an error I don’t understand.

This is the error.

Players.ForeverBloxian.PlayerScripts.CamShakeMain:12: attempt to perform arithmetic (sub) on Vector3 and number

This is the line that is erroring

local var0 = game.Players.LocalPlayer.Character.Head.Position - arg1.Position.magnitude / arg1.BlastRadius / 8

If somebody can help me and also help me understand this error, then it would be a great help! :smile:

Have you tried reading the error message? It clearly states that you are trying to subtract a Vector3 and a number.

This error is still a bit confusing. I’m just looking for a fix because my brain has gone crazy trying to figure this out.

Also I’m new to coding so some things are a bit tricky

You can’t subtract a Vector3 and a number. You are trying to subtract a part’s position and a number.

Ahhh. Now I see it. But I still have no idea on how to fix it since this is very confusing. I do understand the error now.

If you need it, here is the full code:

ShakeDist = 25
local CurrentCamera_v0 = workspace.CurrentCamera
local upval1 = game:GetService("Players").LocalPlayer:WaitForChild("PlayerSettings"):WaitForChild("ReducedShake")
local upval2 = require(game.ReplicatedStorage.CameraShaker).new(Enum.RenderPriority.Camera.Value, function(arg1)
	CurrentCamera_v0.CFrame = CurrentCamera_v0.CFrame * arg1
end)
local var60 = game
function var60(arg1)
	if arg1:IsA("Explosion") then
		local Name_v0 = arg1.Name
		if Name_v0 == "Explosion" then
			local var0 = game.Players.LocalPlayer.Character.Head.Position - arg1.Position.magnitude / arg1.BlastRadius / 8
			if var0 < ShakeDist then
				upval2:Start()
				upval2:ShakeOnce(arg1.BlastRadius / 2, 10, 0, 1.5)
			end
		end
	end
end

game.Workspace.DescendantAdded:Connect(var60)

P.S.
This code is from my friend

What exactly are you trying to achieve with it? What will the result look like?

Basically if there is an explosion near the player, their camera will shake.

So var0 is the distance from the explosion?

Yes that is correct
(char limit)

Just like in math, in programming, division and multiplication are prioritized.
Meaning in this line,

local var0 = game.Players.LocalPlayer.Character.Head.Position - arg1.Position.magnitude / arg1.BlastRadius / 8

you are first calculating the arg1.Position.magnitude / arg1.BlastRadius / 8 part, and only then you subtract it.
To prevent that, cover the subtraction in parentheses:

local var0 = (game.Players.LocalPlayer.Character.Head.Position - arg1.Position.magnitude) / arg1.BlastRadius / 8

I am still getting the same error even with this modified line

damn i put the parentheses on the wrong spot

local var0 = (game.Players.LocalPlayer.Character.Head.Position - arg1.Position).magnitude / arg1.BlastRadius / 8

Alright I will try this out! (char limit)

It works! Thanks for helping me! (char limit)

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