Hello, I have created a script for a Fire Sword in my game, this is in ServerScriptService
I was wondering if there’s something useless in this code that I can remove.
--//ReplicatedStorage & Remotes
local replicatedStorage = game:GetService("ReplicatedStorage")
local Slash = replicatedStorage:WaitForChild("FireSwordRemotes"):WaitForChild("SwordSlash")
local equip = replicatedStorage:WaitForChild("FireSwordRemotes"):WaitForChild("Equip")
local unequip = replicatedStorage:WaitForChild("FireSwordRemotes"):WaitForChild("Unequip")
--//Settings
local Firecooldown = 0.10
local cooldown = 1.75
local damage = 20
local debounce = false
local canHitAgain = true
local active = false
--//Animations
local slashAnim = script:WaitForChild("Slash")
local equipAnim = script:WaitForChild("Equip")
--//Functions
local function otherPartHit(otherPart)
local hum = otherPart.Parent.Humanoid
if hum and canHitAgain == true then
canHitAgain = false
hum:TakeDamage(damage)
local fire = script:WaitForChild("Fire"):Clone()
fire.Parent = hum.Parent:WaitForChild("Torso")
for fireDamage = 1, 10, 1 do
wait(0.25)
hum:TakeDamage(1.25)
end
fire:Destroy()
wait(Firecooldown)
canHitAgain = true
end
end
--//Script
unequip.OnServerEvent:Connect(function(player)
--//Hum and Char
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--//AnimTracks
local AnimationTracks = hum:GetPlayingAnimationTracks()
--//Script
for i, track in pairs (AnimationTracks) do
track:Stop()
end
char:WaitForChild("Right Arm"):WaitForChild("Fire"):Destroy()
end)
equip.OnServerEvent:Connect(function(player)
--//Hum and Char
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--//Sound
local equipSound = char:WaitForChild("FireSword"):WaitForChild("Handle"):WaitForChild("Unsheath")
--//AnimTracks
local equipTrack = hum:LoadAnimation(equipAnim)
--//Particle
local fire = script:WaitForChild("Fire"):Clone()
--//Script
equipTrack:Play()
equipSound:Play()
fire.Parent = char:WaitForChild("Right Arm")
end)
Slash.OnServerEvent:Connect(function(player, hit)
active = false
if debounce == false and active == false then
--//Hum and Char
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--//Sword
local blade = char:WaitForChild("FireSword"):WaitForChild("Hitbox")
--//AnimTracks
local slashTrack = hum:LoadAnimation(slashAnim)
--//Sound
local slashSound = char:WaitForChild("FireSword"):WaitForChild("Handle"):WaitForChild("Slash")
slashSound:Play()
slashTrack:Play(0.1, 1, 0.5)
if active == false then
blade.Touched:Connect(otherPartHit)
active = true
debounce = true
wait(cooldown)
debounce = false
end
end
end)
local hum = otherPart.Parent:FindFirstChild("Humanoid")
The code will still run even if you don’t change it, only difference is it won’t spam errors if you use :FindFirstChild(), better for debugging other scripts so you don’t think that the error comes from another script.
--//ReplicatedStorage & Remotes
local replicatedStorage = game:GetService("ReplicatedStorage")
local Slash = replicatedStorage:WaitForChild("FireSwordRemotes"):WaitForChild("SwordSlash")
local equip = replicatedStorage:WaitForChild("FireSwordRemotes"):WaitForChild("Equip")
local unequip = replicatedStorage:WaitForChild("FireSwordRemotes"):WaitForChild("Unequip")
--//Settings
local Firecooldown = 0.10
local cooldown = 1.75
local damage = 20
local debounce = false
local canHitAgain = true
local active = false
--//Animations
local slashAnim = script:WaitForChild("Slash")
local equipAnim = script:WaitForChild("Equip")
--//Functions
local function otherPartHit(otherPart)
local hum = otherPart.Parent.Humanoid
if hum and canHitAgain == true then
canHitAgain = false
hum:TakeDamage(damage)
local fire = script:WaitForChild("Fire"):Clone()
fire.Parent = hum.Parent:WaitForChild("Torso")
for fireDamage = 1, 10, 1 do
wait(0.25)
hum:TakeDamage(1.25)
end
fire:Destroy()
wait(Firecooldown)
canHitAgain = true
end
end
--//Script
unequip.OnServerEvent:Connect(function(player)
--//Hum and Char
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--//AnimTracks
local AnimationTracks = hum:GetPlayingAnimationTracks()
--//Script
for i, track in pairs (AnimationTracks) do
track:Stop()
end
char:WaitForChild("Right Arm"):WaitForChild("Fire"):Destroy()
end)
equip.OnServerEvent:Connect(function(player)
--//Hum and Char
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--//Sound
local equipSound = char:WaitForChild("FireSword"):WaitForChild("Handle"):WaitForChild("Unsheath")
--//AnimTracks
local equipTrack = hum:LoadAnimation(equipAnim)
--//Particle
local fire = script:WaitForChild("Fire"):Clone()
--//Script
equipTrack:Play()
equipSound:Play()
fire.Parent = char:WaitForChild("Right Arm")
end)
Slash.OnServerEvent:Connect(function(player, hit)
active = false
if debounce == false and active == false then
--//Hum and Char
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--//Sword
local blade = char:WaitForChild("FireSword"):WaitForChild("Hitbox")
--//AnimTracks
local slashTrack = hum:LoadAnimation(slashAnim)
--//Sound
local slashSound = char:WaitForChild("FireSword"):WaitForChild("Handle"):WaitForChild("Slash")
slashSound:Play()
slashTrack:Play(0.1, 1, 0.5)
if active == false then
blade.Touched:Connect(otherPartHit)
active = true
debounce = true
wait(cooldown)
debounce = false
end
end
end)
No need to wait for the instances on ReplicatedStorage, they are loaded after ReplicatedFirst. There is no need to wait for instances on OnServerEvent.
for i, track in pairs (AnimationTracks) do
track:Stop()
end
Will return an array, so it’s better to use ipairs.
Here’s how I would write your code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local fireSwordRemotes = ReplicatedStorage.FireSwordRemotes
local slash = fireSwordRemotes.SwordSlash
local equip = ReplicatedStorage.FireSwordRemotes.Equip
local unequip = fireSwordRemotes.Enequip
--// Settings
local FIRE_COOL_DOWN= 0.10
local COOL_DOWN = 1.75
local DAMAGE = 20
local DEBOUNCE = false
local CAN_HIT_AGAIN = true
local ACTIVE = false
--//Animations
local slashAnim = script:WaitForChild("Slash")
local equipAnim = script:WaitForChild("Equip")
--//Functions
local function otherPartHit(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and CAN_HIT_AGAIN then
CAN_HIT_AGAIN = false
humanoid:TakeDamage(DAMAGE )
local fire = script.Fire:Clone()
fire.Parent = humanoid.Parent.Torso
for fireDamage = 1, 10, -1 do
humanoid:TakeDamage(1.25)
wait(0.25)
end
fire:Destroy()
wait(FIRE_COOL_DOWN)
CAN_HIT_AGAIN = true
end
end
--//Script
unequip.OnServerEvent:Connect(function(player)
local character = player.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local animationTracks = humanoid:GetPlayingAnimationTracks()
for _, track in ipairs (animationTracks) do
track:Stop()
end
character["Right Arm"].Fire:Destroy()
end)
equip.OnServerEvent:Connect(function(player)
--//Hum and Char
local character = player.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local equipSound = character.FireSword.Handle.Unsheath
local equipTrack = humanoid:LoadAnimation(equipAnim)
local fire = script.Fire:Clone()
--//Script
equipTrack:Play()
equipSound:Play()
fire.Parent = character["Right Arm"]
end)
slash.OnServerEvent:Connect(function(player, hit)
ACTIVE = false
if player:IsDescendantOf(Players) and not DEBOUNCE then
local character = player.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
--//Sword
local blade = character.FireSword.HitBox
--//AnimTracks
local slashTrack = humanoid:LoadAnimation(slashAnim)
--//Sound
local slashSound = character.FireSword.Handle.Slash
slashSound:Play()
slashTrack:Play(0.1, 1, 0.5)
if not ACTIVE then
blade.Touched:Connect(otherPartHit)
ACTIVE = true
DEBOUNCE = true
wait(COOL_DOWN)
DEBOUNCE = false
end
end
end)