So Im working on a gun system for one of my games and I don’t really know what improvements I can make to the gun system, hence why im posting here. Here I will post my Client Script and The game link.
Client Script:
–// Services //–
local RS = game:GetService(“ReplicatedStorage”)
local CAS = game:GetService(“ContextActionService”)
local RuS = game:GetService(“RunService”)
local Players = game:GetService(“Players”).LocalPlayer–// Modules //–
local BN2 = require(RS.BW.Modules.Lib.BridgeNet2)
local Spring = require(RS.BW.Modules.Lib.SpringModule)
local Utility = require(RS.BW.Modules.Utility)–// Game Folders //–
local gameFolder = RS.BW
local storageFolder = gameFolder.Storage–// Plr Assets //–
local Char = Players.Character or Players.CharacterAdded:Wait()
local Cam = game.Workspace.CurrentCamera
local PlrData = RS.BW.PlayerData:WaitForChild(Players.Name)–// Tables //–
local WeaponTable = {
[1] = storageFolder.GunModels:FindFirstChild(“Primary”),
[2] = storageFolder.GunModels:FindFirstChild(“Secondary”)
}local ammoCounts = {}
–// Sways //–
local SwaySpring = Spring.new()
local BobSpring = Spring.new()–// Variables //–
local currentWeaponIndex = 1local viewmodelConnection
local viewmodel
local gunmodel
local gunSettingslocal currentAmmo = 0
–// Main Script //–
local function Bob(addition, magnitude, frequency)
return math.sin(tick() * addition * frequency) * magnitude
endlocal function LockViewmodel()
viewmodelConnection = RuS.RenderStepped:Connect(function(dt:number)
local Delta = game.UserInputService:GetMouseDelta()-- Adjust the magnitude and frequency of the Bob function to control the bob movement local bobMagnitude = 0.2 -- Adjust the magnitude for the bob effect local bobFrequency = 1 -- Adjust the frequency for the bob effect BobSpring:shove(Vector3.new(Bob(5, bobMagnitude, bobFrequency), Bob(10, bobMagnitude, bobFrequency), Bob(5, bobMagnitude, bobFrequency)) / 15 * (Char.PrimaryPart.AssemblyLinearVelocity.Magnitude) / 15) -- Adjust the scaling factors for sway to control the sway movement local swayScalingX = 0.002 -- Adjust the scaling factor for sway in the X direction local swayScalingY = 0.002 -- Adjust the scaling factor for sway in the Y direction SwaySpring:shove(Vector3.new(-Delta.X * swayScalingX, Delta.Y * swayScalingY, 0)) local UpdatedSway = SwaySpring:update(dt) local UpdatedBob = BobSpring:update(dt) local newCamCF = viewmodel.FakeCam.CFrame:ToObjectSpace(viewmodel.HumanoidRootPart.CFrame) viewmodel:PivotTo( (game.Workspace.CurrentCamera.CFrame * CFrame.new(newCamCF.Position)) * CFrame.new(UpdatedSway.X, UpdatedSway.Y, 0) * CFrame.new(UpdatedBob.X, UpdatedBob.Y, 0) )end)
endlocal function StopAnimations()
local animationTracks = viewmodel.Humanoid:GetPlayingAnimationTracks()for _, track in pairs(animationTracks) do
track:Stop()
end
endlocal mouseHold = false
local function SwitchGun(inputState:Enum.UserInputType, newWeaponIndex)
if inputState == Enum.UserInputState.Begin then
if currentWeaponIndex == newWeaponIndex then return endammoCounts[currentWeaponIndex] = currentAmmo currentWeaponIndex = newWeaponIndex currentAmmo = ammoCounts[currentWeaponIndex] or 0 if WeaponTable[currentWeaponIndex] then if currentWeaponIndex == 1 then gunmodel:Destroy() gunmodel = storageFolder.GunModels.Primary:FindFirstChild(PlrData.Class.Primary.Value):Clone() gunSettings = require(storageFolder.GunSettings.Primary:FindFirstChild(PlrData.Class.Primary.Value)) gunmodel.Parent = viewmodel StopAnimations() elseif currentWeaponIndex == 2 then gunmodel:Destroy() gunmodel = storageFolder.GunModels.Secondary:FindFirstChild(PlrData.Class.Secondary.Value):Clone() gunSettings = require(storageFolder.GunSettings.Secondary:FindFirstChild(PlrData.Class.Secondary.Value)) gunmodel.Parent = viewmodel StopAnimations() end local IdleAnim = viewmodel:FindFirstChild("idleAnim") IdleAnim.AnimationId = gunSettings.Animations.idleAnim Utility.WeldClientSide(viewmodel, gunmodel) viewmodel.Humanoid:LoadAnimation(IdleAnim):Play() endend
endlocal function Fire(inputState:Enum.UserInputType) --// Broken Only works with first equipped weapon could do with the Swapping system
if inputState == Enum.UserInputState.Begin thenif gunSettings.WeaponStats.FireType == "Auto" then if currentAmmo <= 0 then return end mouseHold = true while mouseHold and currentAmmo > 0 do currentAmmo -= 1 gunmodel.Core.Muzzle.Muzzle:Emit() print(currentAmmo) local FireRateDivision = (gunSettings.WeaponStats.RateOfFire / 60) * 2 task.wait(1 / FireRateDivision) end end if gunSettings.WeaponStats.FireType == "Semi" then if currentAmmo <= 0 then return end currentAmmo -= 1 gunmodel.Core.Muzzle.Muzzle:Emit() print(currentAmmo) local FireRateDivision = (gunSettings.WeaponStats.RateOfFire / 60) * 2 task.wait(1 / FireRateDivision) endelseif inputState == Enum.UserInputState.End then
mouseHold = false
end
endlocal function processAction(actionName, inputState, inputObject)
if actionName == “SwitchPrimary” then
SwitchGun(inputState, 1)
elseif actionName == “SwitchSecondary” then
SwitchGun(inputState, 2)
elseif actionName == “Fire” then
Fire(inputState)
end
endlocal function Init()
viewmodel = storageFolder.Viewmodel:FindFirstChild(“Viewmodel”):Clone()if WeaponTable[1] and currentWeaponIndex == 1 then
if PlrData.Class.Primary.Value == nil then
warn(“No data Inside of the Primary Value Class System”)
return
end
gunmodel = storageFolder.GunModels.Primary:FindFirstChild(PlrData.Class.Primary.Value):Clone()
gunSettings = require(storageFolder.GunSettings.Primary:FindFirstChild(PlrData.Class.Primary.Value))elseif WeaponTable[2] and currentWeaponIndex == 2 then
if PlrData.Class.Secondary.Value == nil then
warn(“No data Inside of the Secondary Value Class System”)
return
endgunmodel = storageFolder.GunModels.Secondary:FindFirstChild(PlrData.Class.Secondary.Value):Clone() gunSettings = require(storageFolder.GunSettings.Secondary:FindFirstChild(PlrData.Class.Secondary.Value))end
viewmodel.Parent = Cam
gunmodel.Parent = viewmodellocal idleAnim = Instance.new(“Animation”)
idleAnim.Name = “idleAnim”
idleAnim.Parent = viewmodel
idleAnim.AnimationId = gunSettings.Animations.idleAnimviewmodel.Humanoid:LoadAnimation(idleAnim):Play()
Utility.WeldClientSide(viewmodel, gunmodel)
LockViewmodel()currentAmmo = gunSettings.WeaponStats.Ammo
CAS:BindAction(“SwitchPrimary”, processAction, true, Enum.KeyCode.One, Enum.KeyCode.ButtonR2)
CAS:BindAction(“SwitchSecondary”, processAction, true, Enum.KeyCode.Two, Enum.KeyCode.ButtonR2)
CAS:BindAction(“Fire”, processAction, true, Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2)
endlocal function DisableWeaponSystem()
CAS:UnbindAction(“SwitchPrimary”)
CAS:UnbindAction(“SwitchSecondary”)
CAS:UnbindAction(“Fire”)viewmodelConnection:Disconnect()
viewmodel:Destroy()
gunmodel:Destroy()
endInit()
Preformatted text
END OF CLIENT SCRIPT
This is the game link:
https://www.roblox.com/games/17808946205/BWG-Engine-0-0-5
Can you please help me improve the gun systems edit capability and any other little improvements you think can be made.
Kind Regards
ArcCreations Team