Knockback system

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("Punch")

Remote.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")

	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.FilterDescendantsInstances = {Character}

	local Origin = Character.HumanoidRootPart.Position
	local Direction = Character.HumanoidRootPart.CFrame.LookVector * 5
	local Result = workspace:Raycast(Origin, Direction, Params)
	
	local Speed = 40
	local Force = 80000
	
	if Result then
		local Damage = 10 
		local Part = Result.Instance
		local Enemy = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
		
		local TotalForce = Force

		local KnockBack = Instance.new("BodyVelocity")
		KnockBack.Parent = Part.Parent:FindFirstChild("HumanoidRootPart")

		KnockBack.MaxForce = Vector3.new(TotalForce,TotalForce,TotalForce)
		KnockBack.Velocity = Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector * Speed 

		if Enemy and Enemy:GetState() ~= Enum.HumanoidStateType.Dead then
			Enemy:TakeDamage(Damage)
		end
	end
end)

how can I make it not to be knockback to infinity, but after the hit, to have that knockback only for 1 second

1 Like

Make a coroutine wrap and task.wait(duration) and destroy the knockback velocity.

Use VectorForce instead, it’s practically the same as BodyVelocity.

explain, i don’t understand where i will place task.wait()

btw, not work with VectorForce, work with BodyVelocity

just use Debris. Debris:AddItem(your_item, duration) and it will delete the item after that duration, no need for extra lines

bodyvelocity is also deprecated and i suggest you use something like linearvelocity

how to use another Velocity if bodyVelocity is not good?

local function cwrap(func) coroutine.wrap(func)() end

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("Punch")

Remote.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")

	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.FilterDescendantsInstances = {Character}

	local Origin = Character.HumanoidRootPart.Position
	local Direction = Character.HumanoidRootPart.CFrame.LookVector * 5
	local Result = workspace:Raycast(Origin, Direction, Params)
	
	local Speed = 40
	local Force = 80000
	
	if Result then
		local Damage = 10 
		local Part = Result.Instance
		local Enemy = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
		
		local TotalForce = Force

		local KnockBack = Instance.new("BodyVelocity")
		KnockBack.Parent = Part.Parent:FindFirstChild("HumanoidRootPart")

		KnockBack.MaxForce = Vector3.new(TotalForce,TotalForce,TotalForce)
		KnockBack.Velocity = Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector * Speed
		
		cwrap(function()
		    task.wait(2) -- Adjust to your liking
		    KnockBack:Destroy()
		end)

		if Enemy and Enemy:GetState() ~= Enum.HumanoidStateType.Dead then
			Enemy:TakeDamage(Damage)
		end
	end
end)
1 Like

learn how to use it, there are various types of velocity with different methods to use them

but, where i use Debris:AddItem(your_item, duration) ??

That typically means your VectorForce isn’t strong enough, either way it should work as intended.

right after parenting the item, you define game.Debris or game:GetService("Debris") first then call Debris:AddItem(BodyVelocity, duration)

not work, knockbach it’s infinity

an example, please, I’m new to scripting and it’s hard for me to understand, please

looks like he put cwap(function() instead of coroutine.wrap(function() which probably errored

local Debris = game.Debris
local KnockBack = Instance.new("BodyVelocity")
KnockBack.Parent = Part.Parent:FindFirstChild("HumanoidRootPart")
Debris:AddItem(Knockback, 1)

same problem, this humanoid for 1 hit have infinity knockback

did you even try? im pretty sure it works. define Debris first

I was talking about that script, I forgot to attach it, lol

thank you very much, now it works, but what exactly is debris? to know when and how to use it

debris will add an item temporarily for a few seconds, without stopping the entire code unlike task.wait() or wait(). it’s basically the same as using coroutine.wrap() and adding your thread, but shorter.

1 Like