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?)
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.
I recommend using ApplyImpulse or a VectorForce for the movement. Also, you should calculate the knockback direction with CFrame.lookat(attackerPos,defenderPos).LookVector.