Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --//By: Mineloxer
- --//Handles back end checking + sounds
- --//Variables\\--
- local hitmeplz = {"Arm1","Arm2","Chest","Face","Leg1","Leg2"}
- local player
- local tool = script.Parent.Parent
- local handle = tool:WaitForChild("Handle")
- local hole = tool:WaitForChild("Hole")
- local flash = hole.FlashGui
- local lastShot = tick()
- local equipped = false
- local reloading = false
- --//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()
- --//Handle ammo
- ammo.Value = "REL"
- --//Ensure reserveAmmo 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
- wait(reloadTime.Value)
- local needed = magSize.Value - ammo.Value
- reserveAmmo.Value = reserveAmmo.Value - needed
- ammo.Value = magSize.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
- --//Play sound if needed
- if playSound then
- emptySound:Play()
- end
- 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()
- flash.Enabled = true
- wait(.01)
- flash.Enabled = false
- --//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
- for i,v in pairs(hitmeplz) do
- if part.Parent.Name == v then
- humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
- end
- 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
Add Comment
Please, Sign In to add comment