BodyVelocity/LookVector help

Is there a way I can always have the person getting hit to knockback away from the player?

local parent = script.Parent
local Handle = parent:WaitForChild("Handle")

CanDmg = false
Can = true
damage = 24.5
cd = 0.44
TouchEnabled = false

parent.Activated:Connect(function()
local function slash()
if Can == true then
Can = false
TouchEnabled = true
CanDmg = true
Handle.Swing:Play()
wait(.36)
CanDmg = false
wait(cd)
Can = true
TouchEnabled = false
end
end
slash()
end)

function on(Touch)
if not TouchEnabled then return end
local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
if Humanoid ~= nil and CanDmg == true then

local effects = script.Blood:clone()
effects.Parent = Humanoid.Torso
effects.Enabled = true
effects.EffectScript.Disabled = false


Humanoid:TakeDamage(damage)
CanDmg = false
Handle.Swing:Stop()
Handle.Hit:Play()
if Humanoid.Health <= 0 then
print("KILLED!")
end


local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(999999,999999,999999)
BV.Velocity = Humanoid.Torso.CFrame.LookVector * -14
BV.Parent = Humanoid.Torso
wait(0.1)
BV:Destroy()


end
end

Handle.Touched:Connect(on)

It should be your own torso and not the enemy’s torso for the LookVector. Although you might as well go with (enemyTorso - yourTorso).Unit instead.

1 Like

how do I find the players humanoid/torso? Tried using parent.Parent but it gave me an error

1 Like

parent.Parent is a tool. Try its parent again and then HumanoidRootPart or Torso.

Therefore: parent.Parent.Parent.Torso.

Like this?

HumTorso = parent.Parent.Parent:FindFirstChild(“Torso”)

Please try them out before you reply next time. For your own information about this, yes, that is correct.

1 Like

Tried to get the torsos CFrame but I got an error

Players.esaias0987.Backpack.Ronin’s Katana.Script:55: attempt to index nil with ‘CFrame’ - Server - Script:55

Apparently the item was in the backpack. Maybe try referencing it when it gets equipped?

Tried it and got the same error.

1 Like

I modified your script and made it easier to read, kind of.

local tool = script.Parent
local Handle = tool:WaitForChild("Handle")

local CanDmg = false
local debounce = true
local damage = 24.5
local cd = 0.44
local TouchEnabled = false

local function slash()
	if not debounce then
		return
	end

	debounce = false
	TouchEnabled = true
	CanDmg = true
	Handle.Swing:Play()
	task.wait(0.36)
	CanDmg = false
	task.wait(cd)
	debounce = true
	TouchEnabled = false
end

local function onTouch(Touch)
	if not TouchEnabled then return end
	-- Here
	local playerTorso = tool.Parent.Parent.Torso
	-- is the update
	local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
	if Humanoid == nil or not CanDmg then
		return
	end

	local effects = script.Blood:clone()
	effects.Parent = Humanoid.Torso
	effects.Enabled = true
	effects.EffectScript.Disabled = false

	Humanoid:TakeDamage(damage)
	CanDmg = false
	Handle.Swing:Stop()
	Handle.Hit:Play()

	if Humanoid.Health <= 0 then
		print("KILLED!")
	end
	local BV = Instance.new("BodyVelocity")
	BV.MaxForce = Vector3.new(999999,999999,999999)
	BV.Velocity = playerTorso.CFrame.LookVector * -14
	BV.Parent = Humanoid.Torso
	task.wait(0.1)
	BV:Destroy()
end

tool.Activated:Connect(slash)
Handle.Touched:Connect(onTouch)

I just find the torso whenever the sword is trying to slash and damage.

2 Likes

use your own humanoid rootpart and your enemys torso/rootpart

local hrp = game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") -- modify this


local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(999999,999999,999999)
BV.Velocity = hrp.CFrame.LookVector * -14 
BV.Parent = Humanoid.Torso
wait(0.1)
BV:Destroy()

Thanks it worked. I did have to change line 29 to “local playerTorso = tool.Parent.Torso” and line 51 to “BV.Velocity = playerTorso.CFrame.LookVector * 14”. thanks for the help!

1 Like

This is also an applicable solution only if it was on LocalScript, just for an extra note about that.

1 Like

yup, thats why i added a comment saying “Modify this”, thanks for pointing it out though

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