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)
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
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
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.
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
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.
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?
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
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