What the heck is wrong with my sword combat

For some reason slashing literally happens TOO soon. If i put it on 0.4 or less instant stop-- it stops working.

I’ve tried everything. But the player can just touch it and then boom, free star

Code:

katana = game.ReplicatedStorage.Katana
class = Instance.new
tables = {
	anim = {
		11906979696; --Slash1
		11906982618; --Slash2
		11906984334; --Slash3
		11906986139; --Slash4
	}
}
slashCombo = 0
slashing = false
baseID = "rbxassetid"
function kill(i)
	i:Destroy()
end
katana.Remotes.Swinged.OnServerEvent:Connect(function(player: Player)
	


		local character:Model = player.Character or player.CharacterAdded:Wait()
		local humanoid:Humanoid = character:WaitForChild("Humanoid")
		local hrp:Part = character:WaitForChild("HumanoidRootPart")
		local animator:Animator = humanoid:WaitForChild("Animator")
		local animation:Animation = Instance.new("Animation")
		local torso:Part = character:WaitForChild("Torso")

		local sound = katana.Sounds['Sword Swing']:Clone()
		sound.Parent = workspace
		sound:Play()
		sound.Ended:Connect(function(soundId: string)
			sound:Destroy()
		end)
		if slashCombo == 0 then
			animation.AnimationId = baseID.."://"..tables.anim[1]
			animator:LoadAnimation(animation):Play()
			slashCombo = 1
		elseif slashCombo == 1 then
			animation.AnimationId = baseID.."://"..tables.anim[2]
			animator:LoadAnimation(animation):Play()
			slashCombo = 2
		elseif slashCombo == 2 then
			animation.AnimationId = baseID.."://"..tables.anim[3]
			animator:LoadAnimation(animation):Play()
			slashCombo = 3
		elseif slashCombo == 3 then
			animation.AnimationId = baseID.."://"..tables.anim[4]
			animator:LoadAnimation(animation):Play()
			slashCombo = 0
		end
		

		local slash = katana.Assets.Slash:Clone()
		slash.Parent = workspace.Fx

		local slashweld = class("Weld", character)
		slashweld.Name = "SlashWeld"
		slashweld.Part0 = torso
		slashweld.Part1 = slash
		slashweld.C0 = CFrame.new(0,0,0) * CFrame.fromEulerAnglesXYZ(0,math.rad(90),0)

		local tweenService = game:GetService("TweenService")

		local tween = tweenService:Create(slash, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {["Transparency"] = 1})
		tween:Play()
		
		local tween2= tweenService:Create(slash["Outer Mesh"], TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {["Scale"] = Vector3.new(12, 6,12)})
		tween2:Play()
		
		local tween3= tweenService:Create(slash["Inner Crescent"]["Inner Mesh"], TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {["Scale"] = Vector3.new(12, 6,12)})
		tween3:Play()
		
		local tween4= tweenService:Create(slash["Inner Crescent"], TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {["Transparency"] = 1})
		tween4:Play()
		
		tween.Completed:Connect(function(playbackState: Enum.PlaybackState)
			slash:Destroy()
			slashweld:Destroy()
			
			
		end)
		
		humanoid.Jumping:Connect(function(active : boolean)
			if active == true then
				if slashing == true then
			local position = class("BodyPosition", hrp)
			position.Position = hrp.Position
			position.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			task.wait(0.2)
				position:Destroy()
				end
			end
	end)
	
	
	if slashing == true then
		local blade = script.Parent.Blade
		blade.Touched:Connect(function(enemyPart: BasePart)
			local fullEnemy = enemyPart.Parent
			local humanoid = fullEnemy:FindFirstChildOfClass("Humanoid")
			if humanoid then
				humanoid:TakeDamage(1)
			end
			
			local blocking = fullEnemy:FindFirstChild("Blocking")
			if blocking then
				if blocking.Value == true then
				local hrp = fullEnemy:FindFirstChild("HumanoidRootPart")
				local goofy = class("Attachment",hrp)
				goofy.Name = "att"
				local hit = katana.Fx.blockhit:Clone()
				hit.Parent = goofy
					hit:Emit(1)
					end
			end
			
		end)
	end
	
	katana.Remotes.Blocking.OnServerEvent:Connect(function(player)
	local blockfx = katana.Fx.blocking:Clone()
	local goofy = class("Attachment",hrp)
	goofy.Name = "att"
		blockfx.Parent = goofy
		local value = class("BoolValue", character)
		value.Value = true
		value.Name = "Blocking"
		katana.Remotes.BlockingEnded.OnServerEvent:Connect(function(player)
			kill(goofy)
			kill(value)
			--value = false
		end)
	end)
	
	slashing = true
	task.wait(0.1)
	slashing = false
end)

gtb (going to bed), see you tmrw

2 Likes

Can you highlight the line that is causing this?

No errors

Like where is the line that you are changing the time in

What time i dont see os.clock in the code

[EDIT]:
slashing = true
task.wait(0.1)
slashing = false

Where is this line where you are changing this value

1 Like

Decrease the chance of a slash?? I can’t test code at all cuz I’m on phone

How do i decrease it? Its not a number it is a boolean value

Maybe only have it have a chance to set slashing to true?

video/pictures to demonstrate the problem? Its difficult to visualise what you’re expecting to happen vs what is actually happening when we don’t see what you see.

1 Like

You are using a lot of nested if statements. I think it’d be easier to accomplish what you’re doing with a for loop.
Here’s an example:
local slashCombo = 0

local tables = {
anim = {
11906979696, --Slash1
11906982618, --Slash2
11906984334, --Slash3
11906986139, --Slash4
}
}

–[[
This is a for loop. It takes the form of:

for [variable] = [start], [end], [step] do

    [Code]

end

In this case, we're assigning 'i' to 0, and increasing it by 1 every run.
'#' is a metatable function that returns the amount of elements in a table.
Here, #tables.anim == 4, so on the first loop, it is 0, then 1, then 2, then 3.
On the 4th run, i == 4, so the loop ends.

–]]
for i = 0, #tables.anim do
if slashCombo == i then
print("Slash Combo " … tostring(slashCombo + 1) … “!”)

    if slashCombo == 3 then
        slashCombo = 0
    else
        slashCombo = slashCombo + 1
    end

    break
end

end

This will print out “Slash Combo 1!”, “Slash Combo 2!”, “Slash Combo 3!”, then “Slash Combo 1!” again.