Says "Attempted to Perform Arithmetic on Vector3 and Number"

I’m trying to make a wallrunning tutorial from Unity work in Roblox Studio. This code is to make sure that you go the correct direction when wallrunning. The Unity code is:

if (hrp.CFrame.LookVector - wallForward).Magnitude > (hrp.CFrame.LookVector - -wallForward).Magnitude then
		wallForward = Vector3.new(-1, -1, -1) * wallForward
	end

My code is this:

if (hrp.CFrame.LookVector - wallForward).Magnitude > (hrp.CFrame.LookVector - -wallForward).Magnitude then
		wallForward = Vector3.new(-1, -1, -1) * wallForward
end

I’ve tried looking online for a solution and tried using :Dot. Any help would be appreciated!

Roblox handles Vector3 differently from Unity. Specifically, it seems like you’re trying to subtract or multiply a Vector3 and a number, which is not allowed in Roblox Lua.

  • Solution 1: Use the Dot Product
local dotProduct = hrp.CFrame.LookVector:Dot(wallForward)
if dotProduct > 0 then
    wallForward = Vector3.new(-1, -1, -1) * wallForward -- This won't work as is
    -- Instead, invert wallForward manually
    wallForward = wallForward * -1
end
  • Solution 2: Element-wise Multiplication
    If you need to perform element-wise multiplication, you can handle this manually for each component of the Vector3
wallForward = Vector3.new(wallForward.X * -1, wallForward.Y * -1, wallForward.Z * -1)

Here’s a version of your code, using the dot product to compare directions and element-wise multiplication to adjust wallForward:

if wallForward ~= nil then
    -- Use the dot product to compare direction of LookVector and wallForward
    if hrp.CFrame.LookVector:Dot(wallForward) < 0 then
        -- If the dot product is negative, they are facing opposite directions
        wallForward = wallForward * -1 -- Invert wallForward
    end
end

My fault I got it!

	if wallForward ~= nil then
		if (hrp.CFrame.LookVector - wallForward).Magnitude > (hrp.CFrame.LookVector - -wallForward).Magnitude then
			wallForward = Vector3.new(-1, -1, -1) * wallForward
		end
	end

Just so you know, you can multiply and divide vector3’s by numbers, but you can’t add/subtract the two.