Weird knockback glitch

I’ve been trying to implement a knockback feature into my game. It mostly works, but there is a glaring issue. (I believe) the knockback direction depends on which direction the target’s back is facing. I edited the orientation of the target to make it so that it would equal that of the attacker but spinned 180 degrees, but this does not apply when the target is in shiftlock. This is the server-side code:

fm1de.OnServerEvent:Connect(function(player, v)
	fm1de:FireAllClients(player, v)
	print("hi")
	v.Parent:WaitForChild("HumanoidRootPart").Orientation = player.Character.HumanoidRootPart.Orientation - Vector3.new(0,180,0)
	hit:Play()
	v.Parent:FindFirstChild("Humanoid"):TakeDamage(5)
	v.Parent:FindFirstChild("Humanoid").WalkSpeed = 0
	wait(0.1)
	
	wait(0.4)
	v.Parent:FindFirstChild("Humanoid").WalkSpeed = 10
end)
fm1de.OnClientEvent:Connect(function(player, v)
	local knockbackdir = player.Character.HumanoidRootPart.CFrame.LookVector
	local bg = Instance.new("BodyGyro")
	bg.Parent = v.Parent:WaitForChild("HumanoidRootPart")
	bg.D = 0
	bg.P = 500000
	bg.CFrame = CFrame.lookAt(v.Parent:WaitForChild("HumanoidRootPart").Position, knockbackdir)
	local att = Instance.new("Attachment")
	att.Parent = v.Parent:WaitForChild("HumanoidRootPart")
	local vf = Instance.new("VectorForce")
	vf.Parent = v.Parent:WaitForChild("HumanoidRootPart")
	vf.Attachment0 = att
	vf.Force = Vector3.new(0,0,20000)
	wait(0.25)
	vf:Destroy()
	bg:Destroy()
	att:Destroy()
end)

I probably missed something to do with the attacker’s LookVector. If someone could point that out for me, that would be great.

1 Like

I have a very similar script in my game.
The main point is
rotatedCFrame = CFrame.Angles(0, math.rad(-180), 0)–this is backwards
local simCFrame= char.HumanoidRootPart.CFrame:ToWorldSpace(rotatedCFrame)
local newvector=simCFrame.LookVector
slide.Velocity = newvector * reps
So we are getting the CFrme of the character transformed by -180 degrees on the y axis and using that to calculate the direction of the velocity. Which is backwardsi n this case.

function KnockBack(char,reps)
	if char:FindFirstChild("Knockback") ==nil and reps>1 then
		if game.Players:GetPlayerFromCharacter(char)==nil then
		local	kb=Instance.new("BoolValue")
		kb.Name="Knockback"
		kb.Parent=char
				
		local char=char
		char.Humanoid:ChangeState(Enum.HumanoidStateType.Running)
		local slide = Instance.new("BodyVelocity")
		local rotatedCFrame
		rotatedCFrame = CFrame.Angles(0, math.rad(-180), 0)
		local simCFrame= char.HumanoidRootPart.CFrame:ToWorldSpace(rotatedCFrame)
		slide.MaxForce = Vector3.new(1,0,1) * 1000000 --you can change this to whatever number you see fit. I prefer this one
		local newvector=simCFrame.LookVector
		slide.Velocity = newvector * reps
		slide.Parent = char.HumanoidRootPart
		--giveForcefield(char,1) -- gives you invincibility for 1 second (optimal for the duration of the dodge roll)
		slide.Velocity*= 3
		for count = 1, reps do
			task.wait(0.02)
			slide.Velocity*= 0.75
			end
			
				Debris:AddItem(kb,1)
	
		slide:Destroy()
	
			end
		end	
end

So this is a knockback function. When activated it knocks back the character based on reps which is repetition. The way I programmed it was to get the damage and divide it by like 5 or 10 to get the reps.

Also the problem with using the shift-lock could be due to Humanoid.AutoRotate=true
trying turning it to autorotate=false while you are doing your knockback. This will allow you to transform the orientation of the target
This is using body velocity. Which is getting a direction cfram=char.HumanoidRootPart.CFrame:ToWorldSpace(rotatedCFrame)
This new CFrame has a offset we can apply based on its orientation. cfram=cfram:ToWorldSpace(0,0,2)–since the CFrame is facing backwards positive Z transformation will give us the vector position of behind it.
so the Vector would be cfram.Position

goalposition=Root:ToWorldSpace(CFrame.new(0,0,10)).Position
diference=Root.Position-goaposition

So in short the answer is vf.Force = difference

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