My katana bugs out no matter what I do

So recently, I tried making a katana for my game. It makes you go forward a bit when attacking, and it can go forward a lot if you have a lot of damage. The problem is that no matter how many fixes I try to add, if you use it against a corner, you are guaranteed to glitch out. And I want a balanced game, so this isn’t something good.
Also, if any of the scripts seem weird, the player model is only a humanoidrootpart and a head, so that’s why

Local script:

local Player = game.Players.LocalPlayer
local char = workspace:WaitForChild(Player.Name)
local Mouse = Player:GetMouse()
local debounce = Player.Character:WaitForChild("PhysiCool")
local punches = 0
local firing = false

if Player.PlayerGui:FindFirstChild("AttackGui") then
	Player.PlayerGui.AttackGui:Destroy()
end

local gui = script.AttackGui:Clone()
gui.Parent = Player.PlayerGui
local animation = script:WaitForChild('Animation')
local Humanoid = gui.ViewportFrame.WorldModel.Multi:WaitForChild('AnimationController')
local dance = Humanoid:LoadAnimation(animation)
local panim = Humanoid:LoadAnimation(script.Punch1)
local panim2 = Humanoid:LoadAnimation(script.Punch2)
local panim3 = Humanoid:LoadAnimation(script.Finale)
local unshe = Humanoid:LoadAnimation(script.Pull)

repeat task.wait(0.1) until not debounce.Value
unshe:Play()
char.Head.Uns:Play()
workspace.BulletWaster.Cooler:FireServer("PhysiCool",char.Stats.FireRate.Value*2)
wait(1)
dance:Play()

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = char.Stats.Range.Value*3
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if temp2:IsA("Model") and temp2 ~= char and temp2:FindFirstChild("Stats") and temp2.Stats.Team.Value ~= char.Stats.Team.Value and temp2:FindFirstChild("HumanoidRootPart") and temp2:FindFirstChild("Humanoid") and temp2.Humanoid.Health > 0 then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

local function punch()
	-- Check if the NPC is not on Team 1 (assuming Team 1 is the player's team)
	workspace.DmgEvent.KatEvent:FireServer(char,char.Stats.Damage.Value*10)
end

local function megapunch()
	workspace.DmgEvent.KatEvent:FireServer(char,char.Stats.Damage.Value*15)
end

if Mouse then
Mouse.Button1Down:Connect(function()
		firing = true
		while firing do
			wait(0)
	if not debounce.Value and char.Humanoid.Health>0 then
				punches+=1
				char.Head.KataSwing:Play()
		if punches==1 or punches==2 then
		panim:Play()
			panim:AdjustSpeed(panim.Length/(char.Stats.FireRate.Value*0.8))
			workspace.BulletWaster.Cooler:FireServer("PhysiCool",char.Stats.FireRate.Value*0.8)
		if punches==2 then
			panim:Stop()
			panim2:Play()
			panim2:AdjustSpeed(panim2.Length/(char.Stats.FireRate.Value*0.8))
		end
					char.Head.Swing:Play()
					punch()
		wait(char.Stats.FireRate.Value)
		local target = findNearestTorso(char.HumanoidRootPart.Position)
		if target then
			char.Head.KataHit:Play()
		end
		elseif punches==3 then
				panim3:Play()
					panim3:AdjustSpeed(panim3.Length/(char.Stats.FireRate.Value*3.6))
					workspace.BulletWaster.Cooler:FireServer("PhysiCool",char.Stats.FireRate.Value*3.6)
					megapunch()
					local target = findNearestTorso(char.HumanoidRootPart.Position)
					if target then
						char.Head.KataHit:Play()
					end
				for i = 1,3 do
				char.Head.Sord:Play()
						punch()
						local target = findNearestTorso(char.HumanoidRootPart.Position)
						if target then
							char.Head.KataHit:Play()
						end
						wait(char.Stats.FireRate.Value*1.2)
				end
				punches=0
			end
		end
	end
end)
end

Mouse.Button1Up:connect(function()
	firing = false
end)

ServerScript:

script.Parent.KatEvent.OnServerEvent:Connect(function(plr,char,dmg)
	local guy = char
	guy.Humanoid.PlatformStand = false
	guy.Humanoid.Sit = false
	guy.Head.Orientation = Vector3.new(0, guy.Head.Orientation.Y, 0)
	guy.Head.AssemblyAngularVelocity = Vector3.new(0,0,0)
	guy.Head.RotVelocity = Vector3.new(0,0,0)
	guy.Head.Velocity = Vector3.new(0,0,0)
	local KnockBack = Instance.new("BodyVelocity")
	KnockBack.P = math.huge
	KnockBack.Parent = guy:FindFirstChild("HumanoidRootPart")
	KnockBack.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	KnockBack.Velocity = guy:FindFirstChild("HumanoidRootPart").CFrame.lookVector * dmg
	local Part = char.HumanoidRootPart
	local Radius = 35
	for _, Target in pairs(workspace:GetChildren()) do
		if Target:IsA("Model") and Target:FindFirstChild("Humanoid") and Target:FindFirstChild("HumanoidRootPart") and Target:FindFirstChild("Stats") and Target.Stats.Team.Value ~= 1 then --Checking if we have a valid model
			local Human = Target.Humanoid
			local RootPart = Target.HumanoidRootPart
			if (RootPart.Position - Part.Position + Part.CFrame.lookVector*15).Magnitude < Radius then --Checking if there are Targets in range of the AOE
				Human.Health-=dmg
			end
		end
	end
	guy.Head.Touched:Connect(function()
		if KnockBack or char.Head.RotVelocity.Magnitude>1 then
			KnockBack:Remove()
			guy.Humanoid.PlatformStand = false
			guy.Humanoid.Sit = false
			guy.Head.Orientation = Vector3.new(0, guy.Head.Orientation.Y, 0)
			guy.Head.AssemblyAngularVelocity = Vector3.new(0,0,0)
			guy.Head.RotVelocity = Vector3.new(0,0,0)
			guy.Head.Velocity = Vector3.new(0,0,0)
		end
	end)
	for i = 1,2 do
	task.wait(0.1)
	if char.Head.RotVelocity.Magnitude>1 or char.Head.AssemblyAngularVelocity.Magnitude>1 then
		if KnockBack then
			KnockBack:Remove()
		end
			guy.Humanoid.PlatformStand = false
			guy.Humanoid.Sit = false
			guy.Head.Orientation = Vector3.new(0, guy.Head.Orientation.Y, 0)
			guy.Head.AssemblyAngularVelocity = Vector3.new(0,0,0)
			guy.Head.RotVelocity = Vector3.new(0,0,0)
			guy.Head.Velocity = Vector3.new(0,0,0)
		end
	end
	task.wait(0.3)
	if KnockBack then
		KnockBack:Remove()
	end
	guy.Humanoid.PlatformStand = false
	guy.Humanoid.Sit = false
	guy.Head.Orientation = Vector3.new(0, guy.Head.Orientation.Y, 0)
	guy.Head.AssemblyAngularVelocity = Vector3.new(0,0,0)
	guy.Head.RotVelocity = Vector3.new(0,0,0)
	guy.Head.Velocity = Vector3.new(0,0,0)
end)

You could try lowering the force of the body velocity or using some form of raycasting to delete it upon detecting a wall infront of the player.

I was gonna re-add this to prove that I had tried it and it didn’t work, but I tweaked it a bit and got it to work how I wanted. Thanks for reminding me.

1 Like

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