This script is connected to a remote event that gets called whenever the player fires a weapon (in this case it’s an automatic)
one thing that i know is missing is a shooting cooldown handled on the server
i didn’t feel the need to add something like this since the gun has only 30 bullets, and firing 30 bullets faster than 960RPM (once every 60ms) would just not be worth cheat-proofing (you can scold me if i’m wrong)
i’m gonna be working with scripts like these a lot, so i really wanna know if there are any improvements i could do:
local ShootEvent = game:GetService("ReplicatedStorage"):WaitForChild("GunEvents"):WaitForChild("MP9Folder").ShootEvent
local DamageIndicatorEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlayerEvents").DamageGUI
ShootEvent.OnServerEvent:Connect(function(player, mouse)
local MP9 = player.Character:FindFirstChildOfClass("Tool")
local Bullets = MP9:WaitForChild("Bullets")
-- // QUALITY OF LIFE
coroutine.wrap(function()
local ShootSound = MP9:WaitForChild("mp9-s_1"):WaitForChild("Receiver"):WaitForChild("Barrel"):WaitForChild("Suppressor"):WaitForChild("ShootSound")
local MuzzleFlash = MP9:WaitForChild("FlashPart")
ShootSound:Play()
MuzzleFlash:WaitForChild("Light").Enabled = true
MuzzleFlash:WaitForChild("FlashImage").Enabled = true
task.wait(0.1)
MuzzleFlash:WaitForChild("Light").Enabled = false
MuzzleFlash:WaitForChild("FlashImage").Enabled = false
end)()
-- // RAYCASTING
local MIN_BULLETS = 1
local MAX_BULLETS = 30
local MAX_DISTANCE = 200
local BULLET_DAMAGE = 18
local HEADSHOT_MULTIPLIER = 1.6
local HEADSHOT_DAMAGE = 0
if Bullets.Value > MAX_BULLETS then player:Kick("The underworld calls to you.") end
if Bullets.Value < MIN_BULLETS then player:Kick("Your manipulation techniques are atrocious.") end
local origin = MP9:WaitForChild("FlashPart")
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local direction = (mouse.Direction.Unit).Unit
local displacement = direction * MAX_DISTANCE
local result = workspace:Raycast(
origin.Position,
displacement,
raycastParams
)
local tracer = Instance.new("Part")
tracer.Parent = workspace
tracer.Anchored = true
tracer.CFrame = CFrame.lookAt(origin.Position + (displacement / 2), origin.Position + displacement)
tracer.Size = Vector3.new(0.1, 0.1, displacement.Magnitude)
tracer.Transparency = 0.6
tracer.CanCollide = false
tracer.Color = Color3.new(0.960784, 0.666667, 0.156863)
game:GetService("Debris"):AddItem(tracer, 0.2)
if result ~= nil then
local enemy = result.Instance.Parent:FindFirstChild("Humanoid")
if enemy == game:GetService("Players"):GetPlayerFromCharacter(result.Instance.Parent) then return end
if result.Instance == enemy.Parent.Head then
HEADSHOT_DAMAGE = math.floor(BULLET_DAMAGE * HEADSHOT_MULTIPLIER)
enemy:TakeDamage(HEADSHOT_DAMAGE)
DamageIndicatorEvent:FireClient(player, HEADSHOT_DAMAGE, enemy)
else
enemy:TakeDamage(BULLET_DAMAGE)
DamageIndicatorEvent:FireClient(player, BULLET_DAMAGE, enemy)
end
end
end)