I’m making my own weapon system, and I want it to be easily customizable, I started off by making the linked sword that includes two basic attacks and a lunge attack, I’m just wondering if my script looks good so far or I could do anything more efficiently etc? This is like my 3rd time making anything melee-related
client-sided only
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local modules = ReplicatedStorage.Modules
local configurations = modules.Configurations
local meleeConfigurations = configurations.Melee
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local tool = script.Parent
local configuration = require(meleeConfigurations[tool.Name])
local toolRemotes = tool:WaitForChild("Remotes")
local attackRemote = toolRemotes:WaitForChild("Attack")
local equipped = false
local activated = false
local lastAttack = tick()
local coolingDown = false
local currentAttackCombination = 0
local loadedToolAnimations = {}
local loadedAttackAnimations = {}
local random = Random.new()
local function startAttacking()
if coolingDown then
return
end
if currentAttackCombination >= #configuration.ATTACK_LIST then
currentAttackCombination = 0
end
currentAttackCombination += 1
loadedAttackAnimations[currentAttackCombination]:Play()
attackRemote:FireServer(currentAttackCombination)
coolingDown = true
task.delay(configuration.ATTACK_LIST[currentAttackCombination].DELAY, function()
coolingDown = false
end)
end
local function processAnimations()
for animationName, animationId in pairs(configuration.TOOL_ANIMATIONS) do
local animation = Instance.new("Animation")
animation.AnimationId = animationId
loadedToolAnimations[animationName] = animator:LoadAnimation(animation)
end
for attackIndex, animationId in pairs(configuration.ATTACK_ANIMATIONS) do
local animation = Instance.new("Animation")
animation.AnimationId = animationId
loadedAttackAnimations[attackIndex] = animator:LoadAnimation(animation)
end
end
local function onEquipped(mouse: Mouse)
if equipped then
return
end
equipped = true
loadedToolAnimations["Unequip"]:Stop()
loadedToolAnimations["Equip"]:Play()
loadedToolAnimations["Idle"]:Play()
end
local function onUnequipped()
if not equipped then
return
end
equipped = false
loadedToolAnimations["Idle"]:Stop()
loadedToolAnimations["Equip"]:Stop()
loadedToolAnimations["Unequip"]:Play()
end
local function onActivated()
if activated then
return
end
activated = true
startAttacking()
end
local function onDeactivated()
if not activated then
return
end
activated = false
end
processAnimations()
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
tool.Activated:Connect(onActivated)
tool.Deactivated:Connect(onDeactivated)
the configuration module
local CONFIGURATION = {
ATTACK_LIST = {
[1] = {
DAMAGE = {
MINIMUM = 25,
MAXIMUM = 30
},
DELAY = 0.5,
DAMAGE_MULTIPLIER_ENABLED = false, -- When enabled, the damage of this attack will be multiplied by a certain value.
DAMAGE_MULTIPLIER = 1,
},
[2] = {
DAMAGE = {
MINIMUM = 25,
MAXIMUM = 30
},
DELAY = 0.5,
DAMAGE_MULTIPLIER_ENABLED = false,
DAMAGE_MULTIPLIER = 1,
},
[3] = {
DAMAGE = {
MINIMUM = 25,
MAXIMUM = 30
},
DELAY = 0.5,
DAMAGE_MULTIPLIER_ENABLED = false,
DAMAGE_MULTIPLIER = 1.5,
}
},
CRITICAL_HITS_ENABLED = true,
CRITICAL_HIT_CHANCE = 0.1,
CRITICAL_HIT_DAMAGE_MULTIPLIER = 2,
TOOL_ANIMATIONS = {
["Equip"] = "rbxassetid://94160581",
["Unequip"] = "rbxassetid://94095929",
["Idle"] = "rbxassetid://94108418"
},
ATTACK_ANIMATIONS = {
[1] = "rbxassetid://94161088",
[2] = "rbxassetid://94161333",
[3] = "rbxassetid://94160738"
},
}
return CONFIGURATION