Invalid Argument #2 to ‘new’ Vector 3 Expected got Nil? [SOLVED]

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

Server 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)

Local Script:

local tool = script.Parent

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

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

tool.Activated:Connect(function()
	remote:FireServer(mouse.Target, true, "Part Equipped")
end)

Error Message Is

Vector3 Expected Got Nil

Any Help Will Work And Post With The Script

What line is the error on?

(too little characters)

Try using mouse.Hit.Position instead of mouse.Target

1 Like

Doesn’t work

(Too Little Characters)

line 13

(too little characters)

The second argument in your CFrame.new() constructor being Vector3, is Vector3 a variable you have or is it supposed to be Vector3.new()?

It Was A Mistake In The Coding

(too little characters)

Player:GetMouse() can only be called from a LocalScript and the server is unable to get the object itself, so you will need to replace your code like this:

Local Script:

local tool = script.Parent

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

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

tool.Activated:Connect(function()
	remote:FireServer(mouse.Target, mouse.Hit)
end)

Server Script:

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

remote.OnServerEvent:Connect(function(plr, target, mouseCFrame)
	if target then
		if target.Parent:FindFirstChild("Humanoid") then
			local lookVector = mouseCFrame.LookVector --Gets mouse CFrame
			
			if mouseCFrame.LookVector.Y < 0 then 
				lookVector = mouseCFrame.LookVector * Vector3.new(1,-1,1) --This is only to make player knockback upwards, and not downwards. You can remove this if you want.
			end
						
			local root = target.Parent.HumanoidRootPart
			local velocity = Instance.new("BodyVelocity")
			
			velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			velocity.P = 1500
			velocity.Velocity = lookVector * 5 * Vector3.new(1, 2, 1) --Get mouse direction and increases it, you can mess around with this values until you find a good one
			velocity.Parent = root
			wait(0.5)
			velocity:Destroy()
		end
	end
end)

it works

local script code:

local tool = script.Parent

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

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

tool.Activated:Connect(function()
	remote:FireServer(mouse.Target, mouse.Hit)
end)

Solution Server script code:

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

remote.OnServerEvent:Connect(function(plr, target, mouseCFrame)
	if target then
		if target.Parent:FindFirstChild("Humanoid") then
			local lookVector = mouseCFrame.LookVector --Gets mouse CFrame

			if mouseCFrame.LookVector.Y < 0 then 
				lookVector = mouseCFrame.LookVector * Vector3.new(1,-1,1) --This is only to make player knockback upwards, and not downwards. You can remove this if you want.
			end

			local root = target.Parent.HumanoidRootPart
			local velocity = Instance.new("BodyVelocity")

			velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			velocity.P = 1500
			velocity.Velocity = lookVector * 5 * Vector3.new(5, 5, 5) --Get mouse direction and increases it, you can mess around with this values until you find a good one
			velocity.Parent = root
			wait(0.5)
			velocity:Destroy()
		end
	end
end)

Thanks To SirPotato_a