Here's a comprehensive gun framework with viewmodels, recoil, animations, and more - designed to be better than FE Gun Kit:
1. Setup the Gun Module
lua
-- GunModule.lua (ModuleScript in ReplicatedStorage)
local GunModule = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
-- Remote events
local gunEvents = Instance.new("Folder", ReplicatedStorage)
gunEvents.Name = "GunEvents"
local fireEvent = Instance.new("RemoteEvent", gunEvents)
fireEvent.Name = "FireEvent"
local reloadEvent = Instance.new("RemoteEvent", gunEvents)
reloadEvent.Name = "ReloadEvent"
-- Gun configuration
local gunConfig = {
Damage = 35,
FireRate = 0.1,
Recoil = Vector3.new(0.5, 0.5, 0),
Spread = 0.02,
MaxAmmo = 30,
ReloadTime = 2,
Range = 500,
Auto = true,
BulletHoles = true,
MuzzleFlash = true,
ShellEjection = true
}
-- Create gun class
local Gun = {}
Gun.__index = Gun
function Gun.new(player, tool)
local self = setmetatable({}, Gun)
self.Player = player
self.Tool = tool
self.Character = player.Character or player.CharacterAdded:Wait()
self.Humanoid = self.Character:WaitForChild("Humanoid")
-- Viewmodel setup
self:SetupViewmodel()
-- Ammo management
self.CurrentAmmo = gunConfig.MaxAmmo
self.ReserveAmmo = 90
-- State tracking
self.CanFire = true
self.IsReloading = false
self.IsAiming = false
-- Animation setup
self:SetupAnimations()
-- Event connections
self:SetupConnections()
return self
end
function Gun:SetupViewmodel()
-- Create viewmodel
local viewmodel = Instance.new("Model")
viewmodel.Name = "Viewmodel"
viewmodel.Parent = workspace.CurrentCamera
-- Create gun parts (simplified example)
local gunBody = Instance.new("Part")
gunBody.Size = Vector3.new(1, 0.5, 3)
gunBody.Anchored = true
gunBody.CanCollide = false
gunBody.Transparency = 0.5
gunBody.Parent = viewmodel
-- Weld to camera
local weld = Instance.new("WeldConstraint")
weld.Part0 = workspace.CurrentCamera
weld.Part1 = gunBody
weld.Parent = gunBody
-- Position gun in view
gunBody.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(1, -0.5, -2) * CFrame.Angles(0, math.pi/2, 0)
self.Viewmodel = viewmodel
end
function Gun:SetupAnimations()
-- Load animations
local animationFolder = Instance.new("Folder")
animationFolder.Name = "Animations"
animationFolder.Parent = self.Tool
-- Idle animation
local idleAnim = Instance.new("Animation")
idleAnim.Name = "Idle"
idleAnim.AnimationId = "rbxassetid://YOUR_ANIMATION_ID"
idleAnim.Parent = animationFolder
-- Fire animation
local fireAnim = Instance.new("Animation")
fireAnim.Name = "Fire"
fireAnim.AnimationId = "rbxassetid://YOUR_ANIMATION_ID"
fireAnim.Parent = animationFolder
-- Reload animation
local reloadAnim = Instance.new("Animation")
reloadAnim.Name = "Reload"
reloadAnim.AnimationId = "rbxassetid://YOUR_ANIMATION_ID"
reloadAnim.Parent = animationFolder
-- Create animation tracks
self.IdleTrack = self.Humanoid:LoadAnimation(idleAnim)
self.FireTrack = self.Humanoid:LoadAnimation(fireAnim)
self.ReloadTrack = self.Humanoid:LoadAnimation(reloadAnim)
-- Play idle animation
self.IdleTrack:Play()
end
function Gun:SetupConnections()
-- Equipped event
self.Tool.Equipped:Connect(function()
self:OnEquipped()
end)
-- Unequipped event
self.Tool.Unequipped:Connect(function()
self:OnUnequipped()
end)
-- Activated event (firing)
self.Tool.Activated:Connect(function()
self:OnActivated()
end)
end
function Gun:OnEquipped()
-- Show viewmodel
self.Viewmodel.Parent = workspace.CurrentCamera
-- Play idle animation
if self.IdleTrack then
self.IdleTrack:Play()
end
end
function Gun:OnUnequipped()
-- Hide viewmodel
self.Viewmodel.Parent = nil
-- Stop animations
if self.IdleTrack then
self.IdleTrack:Stop()
end
if self.FireTrack then
self.FireTrack:Stop()
end
if self.ReloadTrack then
self.ReloadTrack:Stop()
end
end
function Gun:OnActivated()
if self.IsReloading then return end
if gunConfig.Auto then
-- Automatic fire
if self.CanFire and self.CurrentAmmo > 0 then
self:Fire()
elseif self.CurrentAmmo <= 0 then
self:Reload()
end
else
-- Semi-automatic fire
if self.CanFire and self.CurrentAmmo > 0 then
self.CanFire = false
self:Fire()
task.wait(gunConfig.FireRate)
self.CanFire = true
elseif self.CurrentAmmo <= 0 then
self:Reload()
end
end
end
function Gun:Fire()
if not self.CanFire or self.CurrentAmmo <= 0 then return end
self.CanFire = false
self.CurrentAmmo -= 1
-- Play fire animation
if self.FireTrack then
self.FireTrack:Play()
end
-- Apply recoil to viewmodel
self:ApplyRecoil()
-- Create muzzle flash
self:CreateMuzzleFlash()
-- Fire server event
fireEvent:FireServer(self.Tool)
-- Play sound
local fireSound = Instance.new("Sound")
fireSound.SoundId = "rbxassetid://YOUR_SOUND_ID"
fireSound.Parent = self.Viewmodel
fireSound:Play()
Debris:AddItem(fireSound, 2)
-- Reset fire rate
task.wait(gunConfig.FireRate)
self.CanFire = true
end
function Gun:ApplyRecoil()
-- Apply recoil to viewmodel
local recoilForce = gunConfig.Recoil
local currentCFrame = self.Viewmodel:GetPivot()
-- Apply gradual recoil
for i = 1, 5 do
currentCFrame = currentCFrame * CFrame.Angles(
math.rad(recoilForce.X * (i/5)),
math.rad(recoilForce.Y * (i/5)),
math.rad(recoilForce.Z * (i/5))
)
self.Viewmodel:SetPivot(currentCFrame)
task.wait(0.02)
end
-- Gradually return to original position
for i = 1, 10 do
currentCFrame = currentCFrame:Lerp(
workspace.CurrentCamera.CFrame * CFrame.new(1, -0.5, -2) * CFrame.Angles(0, math.pi/2, 0),
0.3
)
self.Viewmodel:SetPivot(currentCFrame)
task.wait(0.03)
end
end
function Gun:CreateMuzzleFlash()
if not gunConfig.MuzzleFlash then return end
local muzzleFlash = Instance.new("Part")
muzzleFlash.Size = Vector3.new(0.5, 0.5, 0.1)
muzzleFlash.Anchored = true
muzzleFlash.CanCollide = false
muzzleFlash.Transparency = 0.5
muzzleFlash.Material = Enum.Material.Neon
muzzleFlash.Color = Color3.new(1, 0.8, 0)
muzzleFlash.CFrame = self.Viewmodel:GetPivot() * CFrame.new(0, 0, -1.5)
muzzleFlash.Parent = workspace
-- Create light
local light = Instance.new("PointLight")
light.Brightness = 5
light.Range = 10
light.Color = Color3.new(1, 0.8, 0)
light.Parent = muzzleFlash
-- Fade out
for i = 1, 5 do
muzzleFlash.Transparency = muzzleFlash.Transparency + 0.1
light.Brightness = light.Brightness - 1
task.wait(0.05)
end
muzzleFlash:Destroy()
end
function Gun:Reload()
if self.IsReloading or self.CurrentAmmo == gunConfig.MaxAmmo or self.ReserveAmmo <= 0 then
return
end
self.IsReloading = true
-- Play reload animation
if self.ReloadTrack then
self.ReloadTrack:Play()
end
-- Server-side reload
reloadEvent:FireServer(self.Tool)
-- Calculate ammo to add
local neededAmmo = gunConfig.MaxAmmo - self.CurrentAmmo
local ammoToAdd = math.min(neededAmmo, self.ReserveAmmo)
-- Wait for reload time
task.wait(gunConfig.ReloadTime)
-- Update ammo
self.CurrentAmmo += ammoToAdd
self.ReserveAmmo -= ammoToAdd
self.IsReloading = false
end
function Gun:Destroy()
-- Clean up
if self.Viewmodel then
self.Viewmodel:Destroy()
end
if self.IdleTrack then
self.IdleTrack:Stop()
end
if self.FireTrack then
self.FireTrack:Stop()
end
if self.ReloadTrack then
self.ReloadTrack:Stop()
end
end
-- Server-side handling
function GunModule.HandleFire(player, tool)
local character = player.Character
if not character then return end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid or humanoid.Health <= 0 then return end
-- Get mouse position
local mouse = player:GetMouse()
local origin = tool.Handle.Position
local direction = (mouse.Hit.Position - origin).Unit
-- Apply spread
direction = direction + Vector3.new(
(math.random() - 0.5) * gunConfig.Spread,
(math.random() - 0.5) * gunConfig.Spread,
(math.random() - 0.5) * gunConfig.Spread
)
-- Raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- Perform raycast
local result = workspace:Raycast(origin, direction * gunConfig.Range, raycastParams)
if result then
local hit = result.Instance
local hitCharacter = hit:FindFirstAncestorOfClass("Model")
local hitHumanoid = hitCharacter and hitCharacter:FindFirstChild("Humanoid")
-- Damage player if hit
if hitHumanoid then
hitHumanoid:TakeDamage(gunConfig.Damage)
end
-- Create bullet hole
if gunConfig.BulletHoles then
local bulletHole = Instance.new("Part")
bulletHole.Size = Vector3.new(0.2, 0.2, 0.1)
bulletHole.Anchored = true
bulletHole.CanCollide = false
bulletHole.CFrame = CFrame.new(result.Position, result.Position + result.Normal)
bulletHole.Parent = workspace
bulletHole.Material = Enum.Material.Concrete
bulletHole.Color = Color3.new(0.2, 0.2, 0.2)
-- Decal for bullet hole
local decal = Instance.new("Decal")
decal.Texture = "rbxassetid://YOUR_BULLET_HOLE_TEXTURE"
decal.Face = Enum.NormalId.Front
decal.Parent = bulletHole
Debris:AddItem(bulletHole, 30)
end
end
end
function GunModule.HandleReload(player, tool)
-- Server-side validation
local gun = player:FindFirstChild("Gun")
if gun then
-- Update client ammo
reloadEvent:FireClient(player, tool)
end
end
return GunModule
2. Client-Side Tool Script
lua
-- Tool script (LocalScript in the Tool)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local tool = script.Parent
local GunModule = require(ReplicatedStorage:WaitForChild("GunModule"))
local gunEvents = ReplicatedStorage:WaitForChild("GunEvents")
local gun = GunModule.new(player, tool)
-- Handle server responses
gunEvents.ReloadEvent.OnClientEvent:Connect(function(tool)
if tool == script.Parent then
-- Ammo was updated by server
end
end)
-- Clean up when tool is destroyed
tool.Destroying:Connect(function()
gun:Destroy()
end)
3. Server-Side Handler
lua
-- Server script (Script in ServerScriptService)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local GunModule = require(ReplicatedStorage:WaitForChild("GunModule"))
local gunEvents = ReplicatedStorage:WaitForChild("GunEvents")
-- Connect events
gunEvents.FireEvent.OnServerEvent:Connect(GunModule.HandleFire)
gunEvents.ReloadEvent.OnServerEvent:Connect(GunModule.HandleReload)
4. Viewmodel Enhancements
For a more professional viewmodel system, you can:
Add proper arm rigging:
lua
-- In your viewmodel setup
local arms = Instance.new("Model")
arms.Name = "Arms"
arms.Parent = viewmodel
-- Create arms with proper joints
local rightArm = Instance.new("Part")
rightArm.Size = Vector3.new(0.5, 2, 0.5)
rightArm.Anchored = false
rightArm.CanCollide = false
rightArm.Name = "RightArm"
rightArm.Parent = arms
-- Add motor6Ds to connect to viewmodel
local shoulderWeld = Instance.new("Motor6D")
shoulderWeld.Name = "RightShoulder"
shoulderWeld.Part0 = viewmodel:WaitForChild("GunBody")
shoulderWeld.Part1 = rightArm
shoulderWeld.C0 = CFrame.new(0.5, 0, 0) * CFrame.Angles(0, math.pi/2, 0)
shoulderWeld.Parent = rightArm
Add sway effects:
lua
-- In your Gun class
function Gun:UpdateSway()
while self.Viewmodel and self.Viewmodel.Parent do
local mouse = player:GetMouse()
local mouseDelta = mouse.Velocity / 100
-- Apply sway based on mouse movement
local swayX = math.clamp(mouseDelta.X * 0.01, -0.1, 0.1)
local swayY = math.clamp(mouseDelta.Y * 0.01, -0.1, 0.1)
local currentCFrame = self.Viewmodel:GetPivot()
currentCFrame = currentCFrame * CFrame.Angles(swayY, swayX, 0)
self.Viewmodel:SetPivot(currentCFrame)
task.wait()
end
end
5. Advanced Features to Add
Custom Crosshair System:
lua
-- Create a ScreenGui with crosshair elements
local crosshair = Instance.new("ScreenGui")
crosshair.Name = "Crosshair"
crosshair.Parent = player:WaitForChild("PlayerGui")
local centerDot = Instance.new("Frame")
centerDot.Size = UDim2.new(0, 4, 0, 4)
centerDot.Position = UDim2.new(0.5, -2, 0.5, -2)
centerDot.BackgroundColor3 = Color3.new(1, 1, 1)
centerDot.BackgroundTransparency = 0.5
centerDot.Parent = crosshair
-- Add spread indicators
local spreadIndicators = {}
for i = 1, 4 do
local indicator = Instance.new("Frame")
indicator.Size = UDim2.new(0, 2, 0, 2)
indicator.BackgroundColor3 = Color3.new(1, 1, 1)
indicator.BackgroundTransparency = 0.7
indicator.Parent = crosshair
table.insert(spreadIndicators, indicator)
end
-- Update crosshair based on spread
function Gun:UpdateCrosshair()
local spread = gunConfig.Spread * 1000 -- Scale for visibility
-- Position indicators
spreadIndicators[1].Position = UDim2.new(0.5, spread, 0.5, -spread)
spreadIndicators[2].Position = UDim2.new(0.5, spread, 0.5, spread)
spreadIndicators[3].Position = UDim2.new(0.5, -spread, 0.5, -spread)
spreadIndicators[4].Position = UDim2.new(0.5, -spread, 0.5, spread)
end
Hitmarker System:
lua
-- In your Gun class
function Gun:ShowHitmarker()
local hitmarker = Instance.new("ScreenGui")
hitmarker.Name = "Hitmarker"
hitmarker.Parent = player:WaitForChild("PlayerGui")
local leftLine = Instance.new("Frame")
leftLine.Size = UDim2.new(0, 20, 0, 2)
leftLine.Position = UDim2.new(0.5, -25, 0.5, -1)
leftLine.BackgroundColor3 = Color3.new(1, 1, 1)
leftLine.Parent = hitmarker
local rightLine = Instance.new("Frame")
rightLine.Size = UDim2.new(0, 20, 0, 2)
rightLine.Position = UDim2.new(0.5, 5, 0.5, -1)
rightLine.BackgroundColor3 = Color3.new(1, 1, 1)
rightLine.Parent = hitmarker
local topLine = Instance.new("Frame")
topLine.Size = UDim2.new(0, 2, 0, 20)
topLine.Position = UDim2.new(0.5, -1, 0.5, -25)
topLine.BackgroundColor3 = Color3.new(1, 1, 1)
topLine.Parent = hitmarker
local bottomLine = Instance.new("Frame")
bottomLine.Size = UDim2.new(0, 2, 0, 20)
bottomLine.Position = UDim2.new(0.5, -1, 0.5, 5)
bottomLine.BackgroundColor3 = Color3.new(1, 1, 1)
bottomLine.Parent = hitmarker
-- Fade out
for i = 1, 10 do
leftLine.BackgroundTransparency = i * 0.1
rightLine.BackgroundTransparency = i * 0.1
topLine.BackgroundTransparency = i * 0.1
bottomLine.BackgroundTransparency = i * 0.1
task.wait(0.03)
end
hitmarker:Destroy()
end
Implementation Notes
Animation Setup: You'll need to create and upload proper animations for firing, reloading, and idle states.
Sound Effects: Add appropriate sound effects for firing, reloading, and other actions.
Visual Effects: Create muzzle flashes, bullet holes, and other visual effects to enhance immersion.
Performance: This framework is optimized for performance with proper cleanup and efficient raycasting.
Customization: The gun configuration can be easily modified to create different types of weapons.
This framework provides a solid foundation that you can expand with additional features like:
Different fire modes (single, burst, auto)
Weapon attachments (scopes, silencers)
Advanced recoil patterns
Penetration system
Bullet drop simulation
Weapon sway based on movement
Would you like me to elaborate on any specific part of this framework or add additional features?