Hey! I’m beginning to make a gun system and I’m wondering if this is a good way to sort and organize my code. Your feedback would be greatly appreciated.
It uses the “ContextActionService” to operate.
-- ⚠ WAIT ⚠ --
repeat wait() until game.Players.LocalPlayer.Character
print("Loaded;")
-- --
-- Services --
local ContextActionService = game:GetService("ContextActionService")
-- --
-- Animations --
local AnimationStorage = game.ReplicatedStorage.AnimationSaves
local Load_Idle = AnimationStorage.RifleIdle
local Load_Aim = AnimationStorage.RifleAim
local IdleAnima
local AimAnima
-- --
-- Objects --
local Tool = script.Parent
local Plr = game.Players.LocalPlayer
local Human = Plr.Character.Humanoid
local Animator = Human.Animator
-- --
-- Soft Vars --
local AnimationsLoaded = false
-- --
-- Animation Loader --
local function LoadAnimations()
IdleAnima = Animator:LoadAnimation(Load_Idle)
AimAnima = Animator:LoadAnimation(Load_Aim)
end
-- --
-- Animation End --
local function EndAnimations()
IdleAnima:Stop()
AimAnima:Stop()
end
-- --
-- Base Function --
local function HandleAction(ActionName, InputState, InputObject)
-- Action #1 --
if ActionName == "Aim" then
if InputState == Enum.UserInputState.Begin then
-- Animations --
EndAnimations()
AimAnima:Play()
-- --
elseif InputState == Enum.UserInputState.End then
-- Animations --
EndAnimations()
IdleAnima:Play()
-- --
end
end
end
-- --
Tool.Equipped:Connect(function()
-- Bind Functions --
ContextActionService:BindAction("Aim", HandleAction, false, Enum.UserInputType.MouseButton2)
print("Bound;")
-- --
-- Initialize Animations --
if not AnimationsLoaded then
AnimationsLoaded = true
LoadAnimations()
end
print("Initialized Animations;")
-- --
-- Start Idle --
IdleAnima:Play()
-- --
end)
Tool.Unequipped:Connect(function()
-- End Animations --
EndAnimations()
print("Ended Animations;")
-- --
-- Un-bind Functions --
ContextActionService:UnbindAction("Aim")
print("Un-bound;")
-- --
end)