Trying to create a knockback feature, help!

I’m trying to create an event that deals damage and knocks the player backwards in a specific direction. That direction is the direction that the one who dealt the damage is facing. This is my script:

local rs = game:GetService("ReplicatedStorage")
local damageevent1 = rs:WaitForChild("DamageEvent1")



damageevent1.OnServerEvent:Connect(function(player, v)
	v.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
	local TS = game:GetService("TweenService")
	local Info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
	local back = Vector3.new(0,0,50)
	TS:Create(v.Parent:WaitForChild("HumanoidRootPart"), Info, {Position = v.Parent:WaitForChild("HumanoidRootPart").Position + back}):Play()
end)

v.Parent is the target of the knockback.
player is the one who activated the function, aka the attacker (i think?)

Please help!

I would personally use a force like vector force or linear velocity rather than tweening the player. For the direction of it, I guess you could take the position of the moving player away from that of the pushing player and that be the direction relative to the world for the force but I haven’t fully thought it through yet.

3 Likes

Get the direction the attacker is facing by using CFrame.LookVector * distance.

local back = player.Character.HumanoidRootPart.CFrame.LookVector * 50
1 Like

I recommend using ApplyImpulse or a VectorForce for the movement. Also, you should calculate the knockback direction with CFrame.lookat(attackerPos,defenderPos).LookVector.

Linear velocity is probably a better option rather than using a tween.

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