I am creating a knock back function for my combat module and want all clients to experience a smooth knock back.
The main issue I’m having is that the knock back only looks good for one client at a time and is situational. If the knock back is applied to another player it looks smooth on their client, if the knock back is applied to a dummy it only looks smooth on the attackers client.
I just want to clarify that the knock back is typically using a lot less force however in these examples I am using a greater force to highlight the issue.
This code is stored in a module script combat handler as a separate function which is called when the player lands a successful blow.
local function knockback(plrChar,eChar,speed,sequence)
if plrChar and eChar then
local phrp = plrChar:FindFirstChild("HumanoidRootPart")
local ehrp = eChar:FindFirstChild("HumanoidRootPart")
local attach = Instance.new('Attachment',ehrp)
local lv = Instance.new("LinearVelocity",ehrp)
lv.Name = "Knockback"
lv.Attachment0 = attach
lv.MaxForce = math.huge
if string.len(sequence) <4 then
if players:GetPlayerFromCharacter(ehrp.Parent) then
ehrp:SetNetworkOwner(players:GetPlayerFromCharacter(ehrp.Parent))
else
ehrp:SetNetworkOwner(players:GetPlayerFromCharacter(phrp.Parent))
end
lv.VectorVelocity = phrp.CFrame.LookVector*speed+Vector3.new(0,0,1)
else
if players:GetPlayerFromCharacter(ehrp.Parent) then
ehrp:SetNetworkOwner(players:GetPlayerFromCharacter(ehrp.Parent))
else
ehrp:SetNetworkOwner(players:GetPlayerFromCharacter(phrp.Parent))
end
lv.VectorVelocity = phrp.CFrame.LookVector*speed+Vector3.new(0,0,1) lv.VectorVelocity = phrp.CFrame.LookVector*speed+Vector3.new(0,0,1)
end
game.Debris:AddItem(lv,.15)
lv.Destroying:Connect(function()
ehrp:SetNetworkOwnershipAuto()
end)
end
end
Any help or suggestions would be greatly appreciated and thank you for reading this post.
This is my first ever post in the developer forums so apologies if there is anything wrong with formatting or my post in general.
local function newknockback(plrChar,eChar,speed,sequence)
if plrChar and eChar then
local phrp = plrChar:FindFirstChild("HumanoidRootPart")
local ehrp = eChar:FindFirstChild("HumanoidRootPart")
local attach = Instance.new("Attachment",ehrp)
local lv = Instance.new("LinearVelocity",attach)
lv.Name = "Knockback"
lv.Attachment0 = attach
lv.MaxForce = math.huge
local lookVector = phrp.CFrame.LookVector
lv.VectorVelocity = lookVector * speed
local tween = ts:Create(lv, kbtween, {VectorVelocity = lookVector * 0.1})
tween:Play()
game.Debris:AddItem(attach, 0.3)
end
end
Creating a LocalScript inside a player that triggers from some RemoteEvent
local lv = game.ReplicatedStorage:WaitForChild("LocalVelocityEvent")
lv.OnClientEvent:Connect(function(...) --Ur velocity parameters (force, velocity, etc.)
--creating velocity in player's humanoidrootpart
end)
So on the other side, Server side, i fire event to player who’s getting attacked.
local lv = game.ReplicatedStorage:WaitForChild("LocalVelocityEvent")
local function newknockback(plrChar,eChar,speed,sequence)
if plrChar and eChar then
local phrp = plrChar:FindFirstChild("HumanoidRootPart")
local ehrp = eChar:FindFirstChild("HumanoidRootPart")
if players:GetPlayerFromCharacter(ehrp.Parent) then
lv:FireClient(players:GetPlayerFromCharacter(ehrp.Parent), parameters, parameters, etc..) --You get me
else
local attach = Instance.new("Attachment",ehrp)
local lv = Instance.new("LinearVelocity",attach)
lv.Name = "Knockback"
lv.Attachment0 = attach
lv.MaxForce = math.huge
local lookVector = phrp.CFrame.LookVector
lv.VectorVelocity = lookVector * speed
local tween = ts:Create(lv, kbtween, {VectorVelocity = lookVector * 0.1})
tween:Play()
game.Debris:AddItem(attach, 0.3)
end
end
end
LocalVelocity makes knockbacking other players very smooth, since its not Server pushing them but their Client, ofc safety reasons - They can disable that script with an exploit, but hey! Its still a solution right?
Hi . I do have some solutions. when you hit a ‘victim’ and the victim is a player’s character, you will want to fire a remote event from server to that player’s client . in the remote event perimeter, you will want to send the force od the knockback to the client . if the victim is not a player 's character, you have to handle the knockback on the server . it will be choppy unless you give the attacker’s player network ownership. keep in mind by doing so , you allow the attacker to just void the victim