How do I apply knockback by changing Setting Network Owner?

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!!

For Knockbacks, I simply fire a RemoteEvent to the player’s client and let the client perform the knockback on its character

Where would you put the local script for that? In the tool?

I place it under StarterCharacterScripts

How would I knock the player back though? Using velocity? And should I send info like the humanoidRootPart to the client from the server?

In my use case, I use AssemblyLinearVelocity on the character’s HumanoidRootPart:

-- Example of what I do
Character.HumanoidRootPart.AssemblyLinearVelocity = -(From - CharacterPosition).Unit * Force

-- of course, what's actually being set as the value depends on your use case.
-- So, I believe for your case, it would be:
Character.HumanoidRootPart.AssemblyLinearVelocity = Character.HumanoidRootPart.CFrame.LookVector * Force

For your case, you should send the client the amount of force to apply on itself

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.