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)