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