Invalid Argument #2 to 'new' Vector 3 Expected got Nil?

i’m trying to make a knockback script on a tool but that error appears

Script Code:

local tool = script.Parent

local remote = game.ReplicatedStorage:WaitForChild("Hitted")

remote.OnServerEvent:Connect(function(plr, target)
	if target then
		local mouse = plr:GetMouse()
		if target.Parent:FindFirstChild("Humanoid") then
			local root = target.Parent.HumanoidRootPart
			local velocity = Instance.new("BodyVelocity")
			velocity.MaxForce = Vector3.new(9999999,9999999,9999999)
			velocity.Velocity = CFrame.new(plr.Character.HumanoidRootPart.Position, Vector3, mouse).lookVector * 5
			velocity.Parent = root
			wait(0.5)
			velocity:Destroy()
		end
	end
end)

Error Message Is

Vector3 Expected Got CFrame

Any Help Will Work And Post With The Script

Which line gives the error?


1 Like

it’s on line 12

wait() doesn’t take vector3…

The BodyVelocity velocity property requires Vector3 value not CFrame.

The property determines the goal velocity. Not direction.

Can you be more specific on what you’re trying to achieve? To create a knockback relative to the mouse position, you’ll have to get the direction between the mouse 3D position and the character’s position by normalizing the difference between (goal-origin) and then negating it. Also, .Velocity receives a Vector3, not a CFrame, that’s why you’re getting the error.

If you want to perform the knockback on the target NPC/Character, it’s as simple as replacing the root part and creating the BodyVelocity inside of it.

local function DeleteBody(bodyvelocity)
	bodyvelocity:Destroy()
end

local function CreateBody()
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Velocity = (-(Mouse.Hit.Position-HRP.Position).Unit * 50) + Vector3.new(0, 5, 0)-- Adding to the Y axis to elevate the character
	BodyVelocity.Parent = HRP

	task.delay(.5, DeleteBody, BodyVelocity) -- you can also destroy it with Debris
end

Part.Velocity needs a Vector3 Value. Try using CFrame.Position

You can’t get the player’s mouse from a server script.

https://developer.roblox.com/en-us/api-reference/function/Player/GetMouse

Not to mention you’re trying to pass the mouse object itself as an argument to the CFrame class constructor function.

1 Like
local tool = script.Parent

local remote = game.ReplicatedStorage:WaitForChild("Hitted")

remote.OnServerEvent:Connect(function(plr, target)
	if target then
		local mouse = plr:GetMouse()
		if target.Parent:FindFirstChild("Humanoid") then
			local root = target.Parent.HumanoidRootPart
			local velocity = Instance.new("BodyVelocity")
			velocity.MaxForce = Vector3.new(9999999,9999999,9999999)
			velocity.Velocity = Vector3.new(plr.Character.HumanoidRootPart.Position, 0, mouse).lookVector * 5
			velocity.Parent = root
			wait(0.5)
			velocity:Destroy()
		end
	end
end)

BodyVelocity expects a Vector3, not CFrame, hence the error. (Line 12)

In addition, as Forummer said, you can’t get a players mouse from a server script.

You cannot derive lookVector from a Vector3.
https://developer.roblox.com/en-us/api-reference/datatype/CFrame