I’m trying to have the player that a sword hits be knocked back.
It just does not work. I have tried VectorForces, ApplyImpulse, and Velocity but no matter what I do I cannot get it to work. The furthest I’ve gotten was getting a Rig to be knocked back, but it has never worked with an actual player.
I read that I need to change the network ownership, but I haven’t gotten that to work. I may be doing it wrong.
local players = game:GetService("Players")
local tool = script.Parent
local blade = tool.Handle.Sword_Part
local animationsFolder = tool.Animations
local isHit = false
local debounce = false
local debounceTime = 1
local knockbackForce = 100
local function bladeTouched(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce then
if isHit then return end
isHit = true
print(hit)
local humanoid = hit.Parent.Humanoid
local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
if humanoid and humanoidRootPart then
local character = tool.Parent
local ogVelocity = humanoidRootPart.Velocity
local player = players:GetPlayerFromCharacter(humanoidRootPart.Parent)
humanoidRootPart:SetNetworkOwner(player)
humanoidRootPart.Velocity = (character.HumanoidRootPart.CFrame.LookVector * knockbackForce)
wait(debounceTime)
humanoidRootPart:SetNetworkOwner(nil)
humanoidRootPart.Velocity = ogVelocity
isHit = false
end
end
end
local function onActivated()
if not debounce then
local character = tool.Parent
local humanoid = character.Humanoid
local anim = animationsFolder.Animation
local animTrack = humanoid:LoadAnimation(anim)
animTrack:Play()
debounce = true
wait(debounceTime)
debounce = false
end
end
blade.Touched:Connect(bladeTouched)
tool.Activated:Connect(onActivated)
Any help at all will be appreciated. Thanks!!