LOCAL SCRIPT
-----------------------------
-- SERVICES --
-----------------------------
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
-----------------------------
-- VARIABLES --
-----------------------------
local player = Players.LocalPlayer
local gun = script.Parent
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.FilteringFolder
local properties = require(gun.Properties)
local GunCommsEnum = require(ReplicatedStorage.Modules.Enums.GunCommsEnum)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local gunEvent = ReplicatedStorage.Remotes.GunEvent
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
local shotCooldown = 60/properties.FireRate
local mouseButtonDown = false
local shooting = false
local reloading = false
-----------------------------
-- PRIVATE FUNCTIONS --
-----------------------------
local function createAnimationTrack(animId): AnimationTrack
local anim = Instance.new("Animation")
anim.AnimationId = animId
return animator:LoadAnimation(anim)
end
local equipTrack = createAnimationTrack(properties.Animations.Equip)
local shootTrack = createAnimationTrack(properties.Animations.Shoot)
local reloadTrack = createAnimationTrack(properties.Animations.Reload)
local function createSound(parent, soundProperties)
local sound = Instance.new("Sound")
for property, value in soundProperties do
sound[property] = value
end
sound.Parent = parent
return sound
end
local function shoot()
params.FilterDescendantsInstances = {player.Character, workspace.FilteringFolder}
local shotDirection = (mouse.Hit.Position - gun.Handle.Muzzle.WorldCFrame.Position).Unit
if gun:GetAttribute("GunType") == "Standard" then
local result = workspace:Raycast(gun.Handle.Muzzle.WorldCFrame.Position, shotDirection * properties.MaxBulletDistance, params)
if result then
local remoteCompatibleResult = {["Instance"] = result.Instance, ["Position"] = result.Position, ["Normal"] = result.Normal}
gunEvent:FireServer(GunCommsEnum.ToServer.ValidateShot, {RaycastResult = remoteCompatibleResult, InstancePosition = result.Instance.Position})
else
gunEvent:FireServer(GunCommsEnum.ToServer.ValidateShot)
end
print(result)
elseif gun:GetAttribute("GunType") == "Spread" then
local rng = Random.new()
local allPellets = {}
for i = 1, properties.TotalPellets do
if properties.RoundType ~= "Slug" then
shotDirection = ( CFrame.lookAt(gun.Handle.Muzzle.WorldCFrame.Position, mouse.Hit.Position) *
CFrame.Angles(math.rad(rng:NextNumber(-properties.MaxSpread, properties.MaxSpread)), math.rad(rng:NextNumber(-properties.MaxSpread, properties.MaxSpread)), 0) ).LookVector
end
local result = workspace:Raycast(gun.Handle.Muzzle.WorldCFrame.Position, shotDirection * properties.MaxBulletDistance, params)
if result then
local remoteCompatibleResult = {["Instance"] = result.Instance, ["Position"] = result.Position, ["Normal"] = result.Normal}
table.insert(allPellets, {RaycastResult = remoteCompatibleResult, InstancePosition = result.Instance.Position})
end
end
gunEvent:FireServer(GunCommsEnum.ToServer.ValidateShot, allPellets)
print(allPellets)
end
shootTrack:Play()
end
local function reload()
if gun:GetAttribute("AmmoInMag") == properties.MaxMagAmmo or gun:GetAttribute("AmmoReserve") == 0 then
return
end
if reloading then
return
end
reloading = true
local sound = createSound(gun.Handle, properties.ReloadSoundProperties)
sound:Play()
sound.Ended:Connect(function()
sound:Destroy()
end)
reloadTrack:Play()
reloadTrack:AdjustSpeed(1/properties.ReloadDuration)
reloadTrack.Ended:Wait()
if reloading then
print("RELOADING!")
gunEvent:FireServer(GunCommsEnum.ToServer.ValidateReload)
end
reloading = false
end
-----------------------------
-- HANDLERS --
-----------------------------
gun.Equipped:Connect(function()
mouse.Icon = "rbxassetid://11839767092"
equipTrack:Play()
ContextActionService:BindAction("GunShoot", function(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
if shooting then
return
end
if reloading then
return
end
if gun:GetAttribute("AmmoInMag") == 0 then
local sound = createSound(gun.Handle, properties.GunClickSoundProperties)
sound:Play()
sound.Ended:Wait()
sound:Destroy()
return
end
mouseButtonDown = true
shooting = true
if properties.Automatic then
while mouseButtonDown do
shoot()
task.wait(shotCooldown)
if gun:GetAttribute("AmmoInMag") <= 0 then
mouseButtonDown = false
break
end
end
else
shoot()
task.wait(shotCooldown)
end
shooting = false
elseif inputState == Enum.UserInputState.End then
mouseButtonDown = false
end
end, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch)
ContextActionService:BindAction("GunReload", function(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.End then
if shooting then
return
end
reload()
end
end, true, Enum.KeyCode.R)
end)
gun.Unequipped:Connect(function()
mouse.Icon = ""
shootTrack:Stop()
reloadTrack:Stop()
equipTrack:Stop()
mouseButtonDown = false
shooting = false
reloading = false
ContextActionService:UnbindAction("GunShoot")
ContextActionService:UnbindAction("GunReload")
end)
if gun:GetAttribute("GunType") == "Spread" then
shootTrack:GetMarkerReachedSignal("Pump"):Connect(function()
local sound = createSound(gun.Handle, properties.ShotgunPumpSoundProperties)
sound:Play()
sound.Ended:Connect(function()
sound:Destroy()
end)
end)
end
MODULE SCRIPT
local self = {
-- damage properties
Damage = 80,
BodyPartMultipliers = {
Head = 3,
UpperTorso = 1.2,
LowerTorso = 1.2,
LeftFoot = 0.4,
RightFoot = 0.4,
LeftLowerLeg = 0.6,
RightLowerLeg = 0.6,
LeftUpperLeg = 0.8,
RightUpperLeg = 0.8,
LeftHand = 0.4,
RightHand = 0.4,
LeftLowerArm = 0.5,
RightLowerArm = 0.5,
LeftUpperArm = 0.75,
RightUpperArm = 0.75,
HumanoidRootPart = 1.2
},
-- gun properties
Automatic = false, -- does gun keep shooting when mouse is held down?
MaxBulletDistance = 500,-- how far bullets travel in studs
ReloadDuration = 5, -- how many seconds the reload animation will last
FireRate = 80, -- how many rounds per minute
MaxReserveAmmo = 35, -- how much ammo the player can carry in reserve
MaxMagAmmo = 7, -- how much ammo can be stored in a magazine
-- shotgun properties
RoundType = "Slug", -- if set to "Slug" then the rounds wont be randomly shot out
TotalPellets = 1, -- how many pellets in a shotgun round
MaxSpread = 0, -- how severe the spread is in degrees
-- bullet holes
BulletHoleSize = Vector3.new(0.6, 0.6, 0.001), -- change X, Y size to change bullet hole size
BulletHoleParticleAmount = 25,
-- animations
Animations = {
Equip = "rbxassetid://16340605233",
Reload = "rbxassetid://16340614144",
Shoot = "rbxassetid://16340595862"
},
-- sounds
ShootSoundProperties = {SoundId = "rbxassetid://5686379468",
RollOffMode = Enum.RollOffMode.InverseTapered,
RollOffMaxDistance = 85,
RollOffMinDistance = 25,
Volume = 1
},
ShotgunPumpSoundProperties = {SoundId = "rbxassetid://3603402053",
RollOffMode = Enum.RollOffMode.InverseTapered,
RollOffMaxDistance = 25,
RollOffMinDistance = 15,
Volume = 1
},
ReloadSoundProperties = {SoundId = "rbxassetid://6523977613",
RollOffMode = Enum.RollOffMode.InverseTapered,
RollOffMaxDistance = 25,
RollOffMinDistance = 15,
Volume = 0.6
},
GunClickSoundProperties = {SoundId = "rbxassetid://9116544314",
RollOffMode = Enum.RollOffMode.InverseTapered,
RollOffMaxDistance = 25,
RollOffMinDistance = 15,
Volume = 1.5
},
HitmarkerSoundProperties = {SoundId = "rbxassetid://160432334"},
-- misc properties
MuzzleFlashProperties = {Brightness = 8, Color = Color3.fromRGB(255, 175, 64), Range = 7, Shadows = true}
}
return self