Help with adding Knockback to my combat script

Hello, so i have a script its 3 hits. I’m trying to have the final hit, a kick deal knockback. But I have no idea how i’d do that. I’d greatly appreciate your help.

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))
local RightArm = game.Players.LocalPlayer.Character["Right Arm"]
local LeftArm = game.Players.LocalPlayer.Character["Left Arm"]
local LeftLeg = game.Players.LocalPlayer.Character["Left Leg"]

local CanDamage = false
local Table = {RightArm, LeftArm, LeftLeg}
local NextAnim = 1 
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true
		CanDamage = true

		if NextAnim == 1 then
			NextAnim = 2
			PunchAnim:Play()
					wait(0.5)	
				
	
		elseif NextAnim == 2 then
			NextAnim = 3
			PunchAnim2:Play()
					wait(0.5)	
				
			
		elseif NextAnim == 3 then
			NextAnim = 1
			KickAnim:Play()
					wait(0.5)	
				end
		
		
		
		local CurrentNextAnim = NextAnim
		
		Cooldown = false
		CanDamage = false
		wait(0.5)
		if CurrentNextAnim == NextAnim then 
			NextAnim = 1
		end
	end
end)

for i,v in pairs(Table) do
	v.Touched:Connect(function(h)
		if h.Parent:FindFirstChild("Humanoid") and CanDamage then
			CanDamage = false
			game.ReplicatedStorage.AttackEvent:FireServer(h.Parent)
		end
	end)

The script in question

4 Likes

You could shoot the player character back a ways, by inserting a BodyVelocity object into the HumanoidRootPart.

Example:

elseif NextAnim == 3 then
    NextAnim = 1
    KickAnim:Play()
    HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
    BodyVelocity = Instance.new("BodyVelocity",HumanoidRootPart) --you can customize the properties of the BodyVelocity as you wish
    BodyVelocity.Velocity = Vector3.new(0,5,0) + HumanoidRootPart.CFrame.LookVector * -15 -- make the player travel up and back
	
    wait(0.5)

    BodyVelocity:Destroy()
end

Hope this helps!

1 Like

HMm i just end up spawning while floating upwards into the sky lol

Can I see your script, and an example of what is happening?

1 Like
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))
local RightArm = game.Players.LocalPlayer.Character["Right Arm"]
local LeftArm = game.Players.LocalPlayer.Character["Left Arm"]
local LeftLeg = game.Players.LocalPlayer.Character["Left Leg"]



 local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
 local BodyVelocity = Instance.new("BodyVelocity", HumanoidRootPart)

local CanDamage = false
local Table = {RightArm, LeftArm, LeftLeg}
local NextAnim = 1 
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true
		CanDamage = true

		if NextAnim == 1 then
			NextAnim = 2
			PunchAnim:Play()
					wait(0.5)	
				
	
		elseif NextAnim == 2 then
			NextAnim = 3
			PunchAnim2:Play()
					wait(0.5)	
				
			
		elseif NextAnim == 3 then
    NextAnim = 1
    KickAnim:Play()
    
    BodyVelocity.Velocity = Vector3.new(0,5,0) + HumanoidRootPart.CFrame.LookVector * -15 
	
    wait(0.5)

    BodyVelocity:Destroy()
		end
		
		
		local CurrentNextAnim = NextAnim
		
		Cooldown = false
		CanDamage = false
		wait(0.5)
		if CurrentNextAnim == NextAnim then 
			NextAnim = 1
		end
	end
end)

for i,v in pairs(Table) do
	v.Touched:Connect(function(h)
		if h.Parent:FindFirstChild("Humanoid") and CanDamage then
			CanDamage = false
			game.ReplicatedStorage.AttackEvent:FireServer(h.Parent)
		end
	end)
end

https://i.gyazo.com/f48b57355132fb2b70702abaeca53543.mp4

Instead of creating the BodyVelocity at the start of your script. You are going to need to create it when the player kicks, as I did in my example. Since you are creating it when the game starts, you are making the player constantly fly up.

1 Like

Oh alright I’ll try that right now.

Update:

new issue it seems, it seems to push my own character away, intead of the person I’m hitting. I’m guessing we’ll need to tell it to ignore the player using it but idk how to do that exactly. Could you help me?

Ooh! I thought by knock back, you meant your character would be knocked back. To make it happen to the other player, all you have to do is apply the same script to your opponent.

Just update this section of your code, and remove the BodyVelocity code from the section involving your character.

for i,v in pairs(Table) do
	v.Touched:Connect(function(h)
		if h.Parent:FindFirstChild("Humanoid") and CanDamage then
			CanDamage = false
			game.ReplicatedStorage.AttackEvent:FireServer(h.Parent)

            HumanoidRootPart = h.Parent:FindFirstChild("HumanoidRootPart")
            BodyVelocity = Instance.new("BodyVelocity",HumanoidRootPart) --you can customize the properties of the BodyVelocity as you wish
            BodyVelocity.Velocity = Vector3.new(0,5,0) + HumanoidRootPart.CFrame.LookVector * -15 -- make the player travel up and back
	
            wait(0.5)

            BodyVelocity:Destroy()
         end
	end)
end
3 Likes

Now my leg is whacky, and im getting pushed away every time i attaack lol.
https://i.gyazo.com/494a4f5790c70f544c9f54bde6ed4adf.mp4

Never mind dont worry I kind of fixed it.
But the knockback is applying to all the hits and not just the kick

Yes. You are getting pushed back ever time you attack, because we hooked the knock back code to the .Touched event, that fires whenever you touch something.

Try wrapping

if NextAnim == 3 then

end

around your knockback code.
This should limit the knock back to only the kick.

2 Likes

ye thats what i planned on trying. One second il tell u how it goes.

I feel like we’re getting closer but it looks like the other two hits end up doing the knockback and not the kick- Lol
https://i.gyazo.com/d7666d216cdc3d5694fc986d4237bd34.mp4

I think the issue is because I wrote our velocity script in a seperate script, it works there. If i leave it in the local where I wrote The combat script it pushes me. Anyways i dont know how to have it understand if NextAnim == 3 then since it exists in another script is there a way to like call that info?

Can I see the little chunk of code you edited?

local Remote = Instance.new("RemoteEvent")
Remote.Name = "AttackEvent"
Remote.Parent = game:GetService("ReplicatedStorage")

Remote.OnServerEvent:Connect(function(Player, Character)
	if Character:FindFirstChild("Humanoid") then
		Character.Humanoid:TakeDamage(5)
		
		if NextAnim == 3 then
		 local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			 local BodyVelocity = Instance.new("BodyVelocity", HumanoidRootPart)
            BodyVelocity.Velocity = Vector3.new(0,5,0) + HumanoidRootPart.CFrame.LookVector * -15 
	
            wait(0.5)

            BodyVelocity:Destroy()
		end
		end
end)

heres the code I added it onto the damage script i have. I dont think its able to pull the info “if NextAnim == 3 then” because its in the other script

Yes. This script does not have access to NextAnim. You could try passing it through as a parameter.

1 Like

Dont know what that means sorry lol, A parameter?

thats the function thing right?

A parameter is a variable that you pass to the function. Such as the Player and Character you are currently sending

--LocalScript
game.ReplicatedStorage.AttackEvent:FireServer(h.Parent,NextAnim)

--ServerScript
Remote.OnServerEvent:Connect(function(Player,Character,NextAnim)

Alrighty I plugged those in, before i test it, its saying h is an unkown global, what do i do?

Send the code, where the error is coming from.

1 Like

nevermind i made a mistake in the location I placed it its fine now.
I also ended up fixing another issue that was causing the weird foot bending lol.
ALSO!
We’re almost almost there! the knockback is working exactly how i want and the animation is no longer buggy. Just… the knockback applies on hit two and not 3 aka the kick lol

Set the if statement to look for 1 instead of 3. This should make it happen on the kick.

1 Like