Animation works, Knockback does not

Hello! I have made a knockback system stretching over 3 scripts. However, I cannot get it to conflict knockback on the recipient, but the animation for the Invoker plays perfectly fine.

Invoker Local
local Players = game:GetService("Players")
local LPlr = Players.LocalPlayer
local char = LPlr.Character or LPlr.CharacterAdded:Wait()
local Animator = char:WaitForChild("Humanoid"):FindFirstChildWhichIsA("Animator")
local ImputS = game:GetService("UserInputService")
local RepStore = game:GetService("ReplicatedStorage")
local kEvent = RepStore.Events.KnockbackM
local Mouse = LPlr:GetMouse()
local Tool = script.Parent
local AnimationID = "rbxassetid://8388173238"

function SendReq()
	local Tar = Mouse.Target

	if Tar and Tar.Parent:FindFirstChild("Humanoid") then
		local Animation = Instance.new("Animation")
		Animation.AnimationId = AnimationID
		local punch = Animator:LoadAnimation(Animation)
		punch:Play()
		kEvent:FireServer(Tar.Parent.PrimaryPart)
	end

end




Tool.Activated:Connect(function() local suc, err = pcall(SendReq) if not suc then warn(err) end end)
Server script
local Event = game.ReplicatedStorage.Events.KnockbackM
local KnockbackE = game.ReplicatedStorage.Events.GetKnockback


Event.OnServerEvent:Connect(function(Plr, Target)
	local Player = game.Players:GetPlayerFromCharacter(Target) or game.Players:GetPlayerFromCharacter(Target.Parent)
	if Player then
		KnockbackE:FireClient(Player,"Verified")
	end
end)
Recipient Local
local KnockBack = game.ReplicatedStorage.Events.GetKnockback
local LPlr = game.Players.LocalPlayer

local Character = LPlr.CharacterAdded:Wait() or LPlr.Character

local Event = KnockBack
local MaxForce = Vector3.new(80000, 80000, 80000)
local Speed = 20

Event.OnClientEvent:Connect(function(Key)
	if Key == "Verified" then

		if Character and Character:FindFirstChild("HumanoidRootPart") then
			local Vel = Instance.new("BodyVelocity")
			Vel.Parent = Character.PrimaryPart
			Vel.MaxForce = MaxForce
			Vel.P = 10
			Vel.Velocity = Character.HumanoidRootPart.LookVector * Speed
		end
	end
end)
3 Likes

I would say that this is why it doesn’t knockback. Since you created the body velocity on the client, it doesn’t replicate on the server for anti-exploit reasons.

Instead, I would use :ApplyImpulse() on the primary part since its easier to use and more efficient. All you need to do is pass in the vector3 velocity you have as an argument.

1 Like

I will try using :ApplyImpulse()

I would really recommend doing the knockback on the server side as an exploiter could very easily disable the knockback for their character.

This is my new script, but still doesn’t work.

I added raycasting instead of mouse.Target, so that the punch will effect the person directly infront of the player.

Local

local Players = game:GetService("Players")
local LPlr = Players.LocalPlayer
local char = LPlr.Character or LPlr.CharacterAdded:Wait()
local Animator = char:WaitForChild("Humanoid"):FindFirstChildWhichIsA("Animator")
local ImputS = game:GetService("UserInputService")
local RepStore = game:GetService("ReplicatedStorage")
local kEvent = RepStore.Events.KnockbackM
local AnimationID = "rbxassetid://8388173238"
local KC = Enum.KeyCode.P
local Humanoid = char:FindFirstChildWhichIsA("Humanoid")

local CurrentState = Enum.HumanoidStateType.Running

Humanoid.StateChanged:Connect(function(_,new)
	CurrentState = new.EnumType
end)



ImputS.InputBegan:Connect(function(InputO) if InputO.KeyCode == KC then local suc, err = pcall(function()
			if CurrentState ~= Enum.HumanoidStateType.Climbing then
				local NRay = workspace:Raycast(LPlr.Character.PrimaryPart.Position,LPlr.Character.PrimaryPart.CFrame.LookVector*90)
				if NRay then
					local tar = NRay.Instance
					local dis = NRay.Distance
					local Animation = Instance.new("Animation")
					Animation.AnimationId = AnimationID
					local punch = Animator:LoadAnimation(Animation)
					if tar.Parent:FindFirstChild("Humanoid") and dis<10 then
						punch:Play()
						kEvent:FireServer(tar.Parent)
					end
					
				end
			end
		end) if not suc then warn(err) end
	end
end)

Server

local KnockBack = game.ReplicatedStorage.Events.KnockbackM
local LPlr = game.Players.LocalPlayer



local Event = KnockBack


Event.OnServerEvent:Connect(function(Character)
	if Character and Character:FindFirstChild("HumanoidRootPart") then
		Character.PrimaryPart:ApplyImpulse(Character.HumanoidRootPart.LookVector * Speed)
	end
	
end)

I changed the raycast to this, so that the player’s own character would be ignored. Now it actually detected the player and does the animation, but no knockback

				local RParams = RaycastParams.new()
				RParams.FilterType = Enum.RaycastFilterType.Blacklist
				RParams.FilterDescendantsInstances = {char}
				local NRay = workspace:Raycast(char.PrimaryPart.Position,char.PrimaryPart.CFrame.LookVector*90,RParams)