SERVER SCRIPT
--//Handles back end checking + sounds
--//Variables\\--
local tool = script.Parent.Parent
local handle = tool:WaitForChild("Handle")
local lastShot = tick()
local player = game:GetService("Players").LocalPlayer
local equipped = false
local tank = false
local reloading = false
local uis = game:GetService("UserInputService")
local Rectag = Enum.KeyCode.R
--//Folders\\--
local configs = tool:WaitForChild("Configs")
local remotes = tool:WaitForChild("Remotes")
--//Configs\\--
local ammo = configs:WaitForChild("Ammo")
local reserveAmmo = configs:WaitForChild("ReserveAmmo")
local magSize = configs:WaitForChild("MagSize")
local damage = configs:WaitForChild("Damage")
local fireRate = configs:WaitForChild("FireRate")
local headMultiplier = configs:WaitForChild("HeadMultiplier")
local range = configs:WaitForChild("Range")
local reloadTime = configs:WaitForChild("ReloadTime")
--//Remotes\\--
local canShootRemote = remotes:WaitForChild("CanShoot")
local canReloadRemote = remotes:WaitForChild("CanReload")
local reloadRemote = remotes:WaitForChild("Reload")
local hitRemote = remotes:WaitForChild("Hit")
local shootRemote = remotes:WaitForChild("Shoot")
--//Sounds\\--
local emptySound = handle:WaitForChild("EmptySound")
local reloadSound = handle:WaitForChild("ReloadSound")
local shootSound = handle:WaitForChild("ShootSound")
local headshotSound = handle:WaitForChild("HeadshotSound")
--//Functions\\--
local function canReload(plr)
--//Return true if gun can be reloaded, else return false
if ammo.Value < magSize.Value then --//Not full ammo
if reserveAmmo.Value > 0 then --//Have any ammo to reload with
if not reloading then --//Not currently reloading
if equipped then --//Gun is equipped in the first place
return true
end
end
end
end
--//If script got here, it means that the above wasn't ful-filled so return false
return false
end
local function reload(plr)
--//Initialize
if not canReload() then return end
--//Reload
reloading = true
reloadSound:Play()
wait(reloadTime.Value)
reloading = false
end
local function canShoot(plr, playSound)
--//Return true if gun can be shot else return false
if math.abs(lastShot - tick()) > (60/fireRate.Value) then --//Fire rate math/debounce
if not reloading then --//Gun currently isn't reloading
if equipped then --//Gun is equipped
if ammo.Value > 0 then --//Ensure we have ammo in the mag
return true
else
end
end
end
end
--//If script got here, it means that the above wasn't ful-filled so return false
return false
end
local function shoot()
--//Initialize
if not canShoot(player, true) then return end
shootSound:Play()
--//Handling
lastShot = tick()
ammo.Value = ammo.Value - 1
end
local function hit(plr, part)
--//Initialize || Checks
if not canShoot() then return end
if not part then return end
--//Get humanoid root part
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")
if rootPart then
--//Check distance
if (rootPart.CFrame.p - part.CFrame.p).magnitude <= range.Value then --//Check range
--//Get humanoid
local humanoid
--//Check if part was a hat/acessory
if part.Name == "Handle" then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
else
humanoid = part.Parent:FindFirstChild("Humanoid")
end
if humanoid and humanoid.Health > 0 then --//Do nothing if humanoid is dead
local newDamage = damage.Value
--//Check hit
if part.Name == "Head" then
newDamage = newDamage * headMultiplier.Value
--//Headshot related handling
local soundCopy = headshotSound:Clone()
soundCopy.Parent = part
soundCopy:Play()
end
--//Apply damage
humanoid:TakeDamage(newDamage)
end
end
end
end
local function equip()
equipped = true
player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
end
local function dequip()
equipped = false
end
--//Event listeners\\--
tool.Equipped:Connect(equip)
tool.Unequipped:Connect(dequip)
hitRemote.OnServerEvent:Connect(hit)
shootRemote.OnServerEvent:Connect(shoot)
reloadRemote.OnServerEvent:Connect(reload)
canShootRemote.OnServerInvoke = canShoot
canReloadRemote.OnServerInvoke = canReload
LOCAL SCRIPT
local player = game.Players.LocalPlayer or game.Players.PlayerAdded.Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
local configs = script.Parent:WaitForChild("Configs")
local ammo = configs:WaitForChild("Ammo")
local lastShot = tick()
local tool = script.Parent
local reserveAmmo = configs:WaitForChild("ReserveAmmo")
local remotes = tool:WaitForChild("Remotes")
local canShoot = remotes:WaitForChild("CanShoot")
local magSize = configs:WaitForChild("MagSize")
local damage = configs:WaitForChild("Damage")
local fireRate = configs:WaitForChild("FireRate")
local headMultiplier = configs:WaitForChild("HeadMultiplier")
local range = configs:WaitForChild("Range")
local reloadTime = configs:WaitForChild("ReloadTime")
anim.AnimationId = "rbxassetid://10015586267" -- 10015223797
local function canShoot(plr, playSound)
--//Return true if gun can be shot else return false
if math.abs(lastShot - tick()) > (60/fireRate.Value) then --//Fire rate math/debounce
if not reloading then --//Gun currently isn't reloading
if script.Parent.Parent == character then --//Gun is equipped
if ammo.Value > 0 then --//Ensure we have ammo in the mag
return true
else
--//Play sound if needed
end
end
end
end
--//If script got here, it means that the above wasn't ful-filled so return false
return false
end
function onKeyPressed(inputObject, gameProcessEvent)
if inputObject.KeyCode == Enum.KeyCode.R and script.Parent.Parent == character then
humanoid:LoadAnimation(anim):Play()
reloading = true
local needed = magSize.Value - ammo.Value
reserveAmmo.Value = reserveAmmo.Value - needed
ammo.Value = magSize.Value
--//Ensure resereAmmo doesn't go negative
if reserveAmmo.Value < 0 then
ammo.Value = ammo.Value + reserveAmmo.Value --//Add the negative number as a sub
reserveAmmo.Value = 0 --//Set back to 0
end
reloading = false
wait(reloadTime.Value)
print("AK-47 Animation")
canShoot(player, true)
end
end
uis.InputBegan:Connect(onKeyPressed)

2 comments
Does the reload sound play?
No