How to fix knockback script from flinging player around?

hello, straight to the point, I’m having trouble with my custom-made knockback script. It’s been working fine except for one thing, sometimes when my player lands on their back (i havent made it ragdoll so normally it should just platform stand on the ground) they sit for a second and start flying around.

heres my script:

local knockback = {}
	local function knockback(length, totalforce, height, hitcharacter, caster, ragdolltime)
		local hum = hitcharacter:FindFirstChild("Humanoid")
		local hrp = hitcharacter:FindFirstChild("HumanoidRootPart")
		
		if hitcharacter:FindFirstChild("ForceField") then
			return
		end
		
		hum.PlatformStand = true
		hum.Parent:SetAttribute("stunned", true)
		
		local bv = Instance.new("BodyVelocity")
		bv.Parent = hitcharacter.HumanoidRootPart
		
		bv.MaxForce = Vector3.new(totalforce, totalforce, totalforce)
		
		if caster:FindFirstChild("HumanoidRootPart") then
			bv.Velocity = caster.HumanoidRootPart.CFrame.LookVector * length
		else
			bv.Velocity = caster.CFrame.LookVector * length
		end
		
		bv.Velocity += Vector3.new(0, height, 0)
	
		task.wait(0.1)
		bv:Destroy()

		task.wait(ragdolltime)
		hum.PlatformStand = false
		hum.Parent:SetAttribute("stunned", nil)
	end
return knockback

now if you’re asking for a vid, i’ll send you one on Discord. my name is ‘xodouble’
pls help me out

I think the flinging is the normal roblox physics but maybe theres a way to bypass that.
Try implementing the following code into the part where the player is stunned:

local character = script.Parent --character, can grab from CharacterAdded signal / or placing script under character in StarterCharacter
local humanoid = character:WaitForChild("Humanoid")
local primaryPart = character:WaitForChild("HumanoidRootPart")

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Landed then
		-- cancel out existing velocity
		local velo = primaryPart.AssemblyLinearVelocity
		primaryPart.AssemblyLinearVelocity = velo*Vector3.new(1,0,1)

		-- get distance to ground
		local ray = Ray.new(primaryPart.Position, Vector3.new(0,-20,0))
		local hit, pos = game.Workspace:FindPartOnRay(ray, character)

		--apply ground hit position to character at hip height offset
		local hipHeight = humanoid.HipHeight
		local newPos = pos + Vector3.new(0,hipHeight,0)
		character:MoveTo(primaryPart.Position - primaryPart.Position + newPos) -- changed to moveTo and instead of primaryPart.CFrame used primaryPart.Position
	end
end)

This script should set the players body directly to the ground. If you want your player to only stand when the stunning is gone you can just use the part in the function. at the end of your code.
If you wan’t your player to stand the whole time vertically than I would suggest using directional interpolation with a frame event and raycasting

i tested it out and sometimes id still fling a little, but thats when i was trying to. most of the time when i saw a glimpse of me getting flinged it got fixed. tysm bro

1 Like

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