Very messy sword code

Hi, I have a old sword model and I made this script with it and it’s really messy. Also very unoptimal with firing sword events

If you can clean this, thank you very much

-- Tool
local Tool = script.Parent
local Handle = Tool.Handle
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
-- Folders
local ModuleFolder = ReplicatedStorage.Modules
local GlobalEventFolder = ReplicatedStorage.Events
local EventFolder = Tool.Events
local AnimationFolder = Tool.Animations
local SoundsFolder = Tool.Handle
local SettingsFolder = Tool.Settings
-- Modules
local ClientCast = require(ModuleFolder.ClientCast)
--local CameraShaker = require(ModuleFolder.CameraShaker)
local RaycastHitbox = require(ModuleFolder.RaycastHitboxV4)
-- Initilizing
local Character
local CasterParameters = RaycastParams.new()
CasterParameters.FilterDescendantsInstances = {Character,Handle}
CasterParameters.FilterType = Enum.RaycastFilterType.Blacklist
local ClientCaster = ClientCast.new(Handle, CasterParameters)
local Hitbox = RaycastHitbox.new(Handle)
Hitbox.Visualizer = true
local SwordLevel = SettingsFolder.SwordLevel.Value
-- Tag and Untag
function TagHumanoid(humanoid, player,taggedby)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "Tag"
	Creator_Tag.Value = player
	Creator_Tag:SetAttribute("TaggedBy",taggedby)
	Debris:AddItem(Creator_Tag, 10)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

local function SpawnBackhold(Character,Create)
	if Create == true then
		if Tool.BackHold then
			for i,v in pairs(Character:GetChildren()) do
				if v.Name == "BackHold" then
					v:Destroy()
				end
				if v.Name == "FakeHandle" then
					v:Destroy()
				end
			end
			-- resources needed
			local backMotor = Tool.BackHold:Clone()
			local fakeHandle = Tool.Handle:Clone()
			fakeHandle.Name = "FakeHandle"
			fakeHandle.Parent = Character
			backMotor.Parent = Character
			backMotor.Part0 = Character.Torso
			backMotor.part1 = fakeHandle
		end
	else
		for i,v in pairs(Character:GetChildren()) do
			if v.Name == "BackHold" then
				v:Destroy()
			end
			if v.Name == "FakeHandle" then
				v:Destroy()
			end
		end
	end
end

-- ToolEquipped Variables
local equipAnim = AnimationFolder.Equip
local idleAnim = AnimationFolder.Idle
local equipDebounce = false
local BackHold = Tool.BackHold
local function ToolEquipped()
	Character = Tool.Parent
	SpawnBackhold(Character,false)
	if not equipDebounce then
		ClientCaster:Stop() 
		equipDebounce = true
		local Humanoid = script.Parent.Parent.Humanoid 
		Equip = Humanoid.Animator:LoadAnimation(equipAnim)
		Idle = Humanoid.Animator:LoadAnimation(idleAnim)	
		Equip:Play()
		SoundsFolder.Equip:Play()	
		wait()
		Idle.Looped = true
		Idle:Play()
		equipDebounce = false
	end
end

UnequippedDebounce = false
local function Unequipped()
	ClientCaster:Stop() 
	if Equip.IsPlaying then
		Equip:Stop()
	end
	if Idle.IsPlaying then
		Idle:Stop()
	end
	if not UnequippedDebounce then
		SpawnBackhold(Character,true)
	end
end

-- Tool Activated
local combo = 1
local activatedDebounce = false
local Shake = Tool.Shake

local function Activated()
	local player = game.Players:GetPlayerFromCharacter(Tool.Parent)
	if not activatedDebounce and player.Character.PlayerValues.CanSlash.Value == true then
		activatedDebounce = true
		if combo == 1 then
			local swing = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing1)
			swing:Play()
			SoundsFolder.Swing1:Play()
			wait(0.3)
			GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
                        -- I fired this so I can tell if a player slashed the weapon (for stamina usuage)
			Hitbox:HitStart() 
			ClientCaster:Start() 
			wait(0.35)
			ClientCaster:Stop() 
			Hitbox:HitStop() 
			wait(0.3)
			combo = 2
			activatedDebounce = false
		elseif combo == 2 then
			local swing2 = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing2)
			swing2:Play()
			SoundsFolder.Swing2:Play()
			wait(0.3)
			ClientCaster:Start() 
			Hitbox:HitStart() 
			GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
			wait(0.35)
			ClientCaster:Stop() 
			Hitbox:HitStop() 		
			wait(0.3)
			combo = 3
			activatedDebounce = false
		elseif combo == 3 then
			local swing3 = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing3)
			swing3:Play()
			SoundsFolder.Swing3:Play()
			wait(0.3)
			ClientCaster:Start()
			Hitbox:HitStart() 
			GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
			wait(0.35)
			ClientCaster:Stop()
			Hitbox:HitStop() 
			wait(0.3)
			combo = 4
			activatedDebounce = false
		elseif combo == 4 then
			local swing4 = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing4)
			swing4:Play()
			SoundsFolder.Swing2:Play()
			wait(0.3)
			ClientCaster:Start()
			Hitbox:HitStart() 
			GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
			wait(0.35)
			ClientCaster:Stop()
			Hitbox:HitStop() 
			wait(0.3)
			combo = 1
			activatedDebounce = false
		end
	end
end


local Debounce2 = {}
local EventDebounce = false
ClientCaster.HumanoidCollided:Connect(function(RaycastResult, HitHumanoid)
	if HitHumanoid and HitHumanoid ~= script.Parent.Parent.Humanoid and HitHumanoid.Health ~= 0 then	
		if Debounce2[HitHumanoid] then
			return
		end
		local play_er = game.Players:GetPlayerFromCharacter(Tool.Parent)
		if HitHumanoid.Parent:WaitForChild("PlayerValues").Blocking.Value == true then -- If players blocking
			local HitPlayer = game.Players:GetPlayerFromCharacter(HitHumanoid.Parent)
			local BlockSound = Instance.new("Sound")
			BlockSound.Name = "Block"
			BlockSound.Parent = HitHumanoid.Parent.PrimaryPart
			BlockSound.SoundId = "rbxassetid://3041192327"
			BlockSound.RollOffMaxDistance = 70
			BlockSound.RollOffMinDistance = 10
			BlockSound:Play()
			game.Debris:AddItem(BlockSound,BlockSound.TimeLength)
			if not EventDebounce then
				EventDebounce = true
				GlobalEventFolder.Mechanics.Blocked:FireClient(play_er,HitHumanoid,play_er)
				wait(0.15)
				EventDebounce = false
			end
		elseif HitHumanoid.Parent:WaitForChild("PlayerValues").Parrying.Value == true then
			local ParriedSound = Instance.new("Sound")
			ParriedSound.Name = "Block"
			ParriedSound.Parent = HitHumanoid.Parent.PrimaryPart
			ParriedSound.SoundId = "rbxassetid://4516507328"
			ParriedSound.RollOffMaxDistance = 70
			ParriedSound.RollOffMinDistance = 10
			ParriedSound:Play()
			game.Debris:AddItem(ParriedSound,ParriedSound.TimeLength)
			if EventDebounce == false then
				EventDebounce = true
				GlobalEventFolder.Mechanics.Parried:FireClient(play_er,HitHumanoid,play_er)
				wait(0.15)
				EventDebounce = false
			end
		else
			UntagHumanoid(HitHumanoid)
			TagHumanoid(HitHumanoid, play_er,script.Parent.Parent.Name)
			local Damage
			if combo == 1 then
				Damage = SettingsFolder.Slash1Damage.Value
			elseif combo == 2 then
				Damage = SettingsFolder.Slash2Damage.Value
			elseif combo == 3 then
				Damage = SettingsFolder.Slash3Damage.Value
			elseif combo == 4 then
				Damage = SettingsFolder.Slash4Damage.Value
			end
			local HitPlayer = game.Players:GetPlayerFromCharacter(HitHumanoid.Parent)
			HitHumanoid:TakeDamage(Damage)
			Debounce2[HitHumanoid] = true
			if HitHumanoid.Health == 0 or HitHumanoid.Health <= 0 then
				GlobalEventFolder.Sword.PlayerKilled:FireClient(play_er,HitPlayer,HitHumanoid)
				if Tool.Parent.Humanoid.Health ~= 100 then
					Tool.Parent.Humanoid.Health = Tool.Parent.Humanoid.Health + 25
				end
			end
			SoundsFolder.Hit:Play()
			wait(0.7)
			Debounce2[HitHumanoid] = false
		end
	end
end)

GlobalEventFolder.Sword.ChangeStatus.OnServerEvent:Connect(function(Player,Status)
	local canSlash = Player.Character:FindFirstChild("PlayerValues").CanSlash
	if Status == "True" then
		canSlash.Value = true
	elseif Status == "False" then
		canSlash.Value = false
	end
end)
Tool.Equipped:Connect(ToolEquipped)
Tool.Unequipped:Connect(Unequipped)
Tool.Activated:Connect(Activated)

Here is my version:

-- // VARIABLES
-- / Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

-- / Tool (Parts?)
local Tool = script.Parent
local Handle = Tool.Handle
local BackHold = Tool.BackHold
local Shake = Tool.Shake

-- Debounces
local activatedDebounce = false
local equipDebounce = false
local UnequippedDebounce = false

local Debounce2 = {}
local EventDebounce = false

-- / Folders
local ModuleFolder = ReplicatedStorage.Modules
local GlobalEventFolder = ReplicatedStorage.Events
local EventFolder = Tool.Events
local AnimationFolder = Tool.Animations
local SoundsFolder = Tool.Handle
local SettingsFolder = Tool.Settings

-- / Modules
local ClientCast = require(ModuleFolder.ClientCast)
local RaycastHitbox = require(ModuleFolder.RaycastHitboxV4)

-- / Animations
local equipAnim = AnimationFolder.Equip
local idleAnim = AnimationFolder.Idle

-- / Initializing
local Character
local Humanoid

local CasterParameters = RaycastParams.new()
CasterParameters.FilterDescendantsInstances = {Character, Handle}
CasterParameters.FilterType = Enum.RaycastFilterType.Blacklist

local ClientCaster = ClientCast.new(Handle, CasterParameters)

local Hitbox = RaycastHitbox.new(Handle)
Hitbox.Visualizer = true

local SwordLevel = SettingsFolder.SwordLevel.Value

-- / Other
local combo = 1

-- // FUNCTIONS
-- / Tagging
function TagHumanoid(humanoid, player,taggedby)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "Tag"
	Creator_Tag.Value = player
	Creator_Tag:SetAttribute("TaggedBy",taggedby)
	
	Debris:AddItem(Creator_Tag, 10)
	
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

-- / Tool
local function SpawnBackhold(Character, Create)
	for i, v in pairs(Character:GetChildren()) do
		local name = v.Name

		if name == "BackHold" or name == "FakeHandle" then
			v:Destroy()
		end
	end
	
	if Create and Tool.BackHold --[[Is Tool.BackHold unnecessary?]] then
		local fakeHandle = Tool.Handle:Clone()
		fakeHandle.Name = "FakeHandle"
		fakeHandle.Parent = Character

		local backMotor = Tool.BackHold:Clone()
		backMotor.Parent = Character
		backMotor.Part0 = Character.Torso
		backMotor.part1 = fakeHandle
	end
end

local function ToolEquipped()
	Character = Tool.Parent
	Humanoid = Character.Humanoid
	
	SpawnBackhold(Character, false)
	
	if not equipDebounce then
		ClientCaster:Stop() 
		
		equipDebounce = true
		
        local animator = Humanoid.Animator
		Equip = animator:LoadAnimation(equipAnim)
		Idle = animator:LoadAnimation(idleAnim)
		
		Equip:Play()
		SoundsFolder.Equip:Play()	
		
		wait()
		
		Idle.Looped = true
		Idle:Play()
		
		equipDebounce = false
	end
end

local function Unequipped()
	ClientCaster:Stop()
	
	if Equip.IsPlaying then
		Equip:Stop()
	end
	
	if Idle.IsPlaying then
		Idle:Stop()
	end
	
	if not UnequippedDebounce then
		SpawnBackhold(Character,true)
	end
end

local function Activated()
	local player = Players:GetPlayerFromCharacter(Character)
	
	local canSlash = Character.PlayerValues.CanSlash.Value
	
	if not activatedDebounce and canSlash then
		activatedDebounce = true
		
		local swingString = "Swing"..tostring(combo)
		
		local swing = Humanoid:LoadAnimation(AnimationFolder[swingString])
		swing:Play()
			
		SoundsFolder[swingString]:Play()
			
		wait(0.3)
			
		-- I fired this so I can tell if a player slashed the weapon (for stamina usuage)
		GlobalEventFolder.Sword.Slash:FireClient(player, SwordLevel)
			
		Hitbox:HitStart() 
		ClientCaster:Start() 
			
		wait(0.35)
			
		ClientCaster:Stop() 
		Hitbox:HitStop() 
			
		wait(0.3)
			
		if combo < 4 then
			combo += 1
			
		else
			combo = 1
		end
			
		activatedDebounce = false
	end
end


-- // MAIN
-- / Sword Remotes
GlobalEventFolder.Sword.ChangeStatus.OnServerEvent:Connect(function(Player,Status)
	local canSlash = Player.Character.PlayerValues.CanSlash
	
	if Status == "True" then
		canSlash.Value = true
		
	elseif Status == "False" then
		canSlash.Value = false
	end
end)

-- / ClientCaster Events
ClientCaster.HumanoidCollided:Connect(function(RaycastResult, HitHumanoid)
	if HitHumanoid and HitHumanoid ~= Humanoid and HitHumanoid.Health ~= 0 then	
		
		if Debounce2[HitHumanoid] then
			return
		end
		
		local player = Players:GetPlayerFromCharacter(Character)
		
		local HitCharacter = HitHumanoid.Parent
		local HitPrimaryPart = HitCharacter.PrimaryPart
		local HitPlayerValues = HitCharacter.PlayerValues
		
		if HitPlayerValues.Blocking.Value then -- If players blocking
			local BlockSound = Instance.new("Sound")
			BlockSound.Name = "Block"
			BlockSound.Parent = HitPrimaryPart
			BlockSound.SoundId = "rbxassetid://3041192327"
			BlockSound.RollOffMaxDistance = 70
			BlockSound.RollOffMinDistance = 10
			BlockSound:Play()
			
			Debris:AddItem(BlockSound,BlockSound.TimeLength)
			
			if not EventDebounce then
				EventDebounce = true
				
				GlobalEventFolder.Mechanics.Blocked:FireClient(player, HitHumanoid, player)
				
				wait(0.15)
				
				EventDebounce = false
			end
			
		elseif HitPlayerValues.Parrying.Value then
			
			local ParriedSound = Instance.new("Sound")
			ParriedSound.Name = "Block"
			ParriedSound.Parent = HitPrimaryPart
			ParriedSound.SoundId = "rbxassetid://4516507328"
			ParriedSound.RollOffMaxDistance = 70
			ParriedSound.RollOffMinDistance = 10
			ParriedSound:Play()
			
			Debris:AddItem(ParriedSound,ParriedSound.TimeLength)
			
			if EventDebounce == false then
				
				EventDebounce = true
				
				GlobalEventFolder.Mechanics.Parried:FireClient(player, HitHumanoid, player)
				
				wait(0.15)
				
				EventDebounce = false
			end
			
		else
			UntagHumanoid(HitHumanoid)
			TagHumanoid(HitHumanoid, player, Character.Name)
			
			local Damage = SettingsFolder["Slash"..tostring(combo).."Damage"].Value
			
			local HitPlayer = Players:GetPlayerFromCharacter(HitCharacter)
			
			HitHumanoid:TakeDamage(Damage)
			
			Debounce2[HitHumanoid] = true
			
			if HitHumanoid.Health <= 0 then
				GlobalEventFolder.Sword.PlayerKilled:FireClient(player, HitPlayer, HitHumanoid)
				
				if Humanoid.Health ~= Humanoid.MaxHealth --[[100]] then
					Humanoid.Health += 25
				end
			end
			
			SoundsFolder.Hit:Play()
			
			wait(0.7)
			
			Debounce2[HitHumanoid] = false
		end
	end
end)

-- / Tool Events
Tool.Equipped:Connect(ToolEquipped)
Tool.Unequipped:Connect(Unequipped)
Tool.Activated:Connect(Activated)

I did not update everything, because I did not know all of what was and was not necessary. However, I tried to minimalize the amount of code by shortening, or removing unnecessary if statements. I did the same with conditions, and variables.

For example, I changed

if combo == 1 then
	local swing = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing1)
	swing:Play()
	SoundsFolder.Swing1:Play()
	wait(0.3)
	GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
                        -- I fired this so I can tell if a player slashed the weapon (for stamina usuage)
	Hitbox:HitStart() 
	ClientCaster:Start() 
	wait(0.35)
	ClientCaster:Stop() 
	Hitbox:HitStop() 
	wait(0.3)
	combo = 2
	activatedDebounce = false
elseif combo == 2 then
	local swing2 = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing2)
	swing2:Play()
	SoundsFolder.Swing2:Play()
	wait(0.3)
	ClientCaster:Start() 
	Hitbox:HitStart() 
	GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
	wait(0.35)
	ClientCaster:Stop() 
	Hitbox:HitStop() 		
	wait(0.3)
	combo = 3
	activatedDebounce = false
elseif combo == 3 then
	local swing3 = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing3)
	swing3:Play()
	SoundsFolder.Swing3:Play()
	wait(0.3)
	ClientCaster:Start()
	Hitbox:HitStart() 
	GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
	wait(0.35)
	ClientCaster:Stop()
	Hitbox:HitStop() 
	wait(0.3)
	combo = 4
	activatedDebounce = false
elseif combo == 4 then
	local swing4 = Tool.Parent.Humanoid:LoadAnimation(AnimationFolder.Swing4)
	swing4:Play()
	SoundsFolder.Swing2:Play()
	wait(0.3)
	ClientCaster:Start()
	Hitbox:HitStart() 
	GlobalEventFolder.Sword.Slash:FireClient(player,SwordLevel)
	wait(0.35)
	ClientCaster:Stop()
	Hitbox:HitStop() 
	wait(0.3)
	combo = 1
	activatedDebounce = false
end

to

local swingString = "Swing"..tostring(combo)
		
local swing = Humanoid:LoadAnimation(AnimationFolder[swingString])
swing:Play()
	
SoundsFolder[swingString]:Play()
	
wait(0.3)

-- I fired this so I can tell if a player slashed the weapon (for stamina usuage)
GlobalEventFolder.Sword.Slash:FireClient(player, SwordLevel)
	
Hitbox:HitStart() 
ClientCaster:Start() 
	
wait(0.35)
	
ClientCaster:Stop() 
Hitbox:HitStop() 
	
wait(0.3)
	
if combo < 4 then
	combo += 1
		
else
	combo = 1
end
	
activatedDebounce = false

I hope this helps. It was a fun task.

4 Likes

Yup the elseifs was the main problem I was looking to fix, this looks clean now thank you

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