Advertisement
Brehhehol

RBLX - Gun Scripts (Automatic)

Dec 16th, 2023 (edited)
3,733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. -- Add remote event in ReplicatedStorage. (Rename to FireEvent)
  2.  
  3. -- // LOCAL SCRIPT IN GUN \\-- (Parent is the tool NOT the handle)
  4. local gun = script.Parent
  5. local sound = gun.Handle:WaitForChild("SOUND NAME") -- REPLACE WITH SOUND NAME IN HANDLE
  6.  
  7. local player = game.Players.LocalPlayer
  8.  
  9. local mouse = player:GetMouse()
  10. mouse.Icon = 'rbxassetid://2827093428'
  11.  
  12. local ReplicatedStorage = game:GetService('ReplicatedStorage')
  13. local remoteEvent = ReplicatedStorage:WaitForChild('FireEvent')
  14.  
  15. local shooting = false
  16. local equipped = false
  17.  
  18. local spare_ammo = 210
  19. local ammo = 30
  20. local FireRate = 0.13 -- (To find your firerate think of a decimal then multiply it by your amount of ammo and the answer is how many seconds you will be shooting before you run out of ammo.)
  21.  
  22. local mouseConnection
  23.  
  24. local function reload()
  25.     if spare_ammo >= 30 then
  26.         spare_ammo -= 30
  27.         ammo = 30
  28.         -- add reload animation
  29.      else
  30.         ammo = spare_ammo
  31.         spare_ammo -= spare_ammo
  32.         -- add reload animation
  33.     end
  34. end
  35.  
  36. gun.Equipped:Connect(function()
  37.         mouse.Button1Down:Connect(function()
  38.                 shooting = true
  39.                 while shooting and equipped or ammo > 0 do
  40.                     ammo = ammo - 1
  41.                     remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
  42.                     sound:Play()
  43.                     -- add recoil/kick animation
  44.                     mouse.Button1Up:Connect(function()
  45.                             shooting = false
  46.                     end)
  47.                     if ammo == 0 and spare_ammo > 0 then
  48.                         reload()
  49.                     end
  50.                     wait(FireRate)
  51.                 end
  52.         end)
  53. end)
  54.  
  55. gun.Unequipped:Connect(function()
  56.     equipped = false
  57.     mouseConnection:Disconnect()
  58. end)
  59.  
  60. --// SCRIPT IN SERVERSCRIPTSERVICE \\--
  61.  
  62. local ReplicatedStorage = game:GetService('ReplicatedStorage')
  63. local remoteEvent = ReplicatedStorage:WaitForChild('FireEvent')
  64.  
  65. remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
  66.         local bullet = Instance.new('Part')
  67.         bullet.Name = "Bullet"
  68.         bullet.Parent = game.Workspace
  69.         bullet.Shape = Enum.PartType.Cylinder
  70.         bullet.Size = Vector3.new(0.2, 0.2, 0.2)
  71.         bullet.BrickColor = BrickColor.new('Gold')
  72.         bullet.Material = Enum.Materal('Neon')
  73.        
  74.         local speed = 300
  75.         bullet.CFrame = CFrame.new(gunPos, mosPos)
  76.         bullet.Velocity = bullet.CFrame.lookVector * speed
  77.         game:GetService("Debris"):AddItem(bullet, 2)
  78.  
  79.         bullet.Touched:Connect(function(hit)
  80.             if hit.Parent:WaitForChild("Humanoid") and not hit.Name == "Head" then
  81.                 hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
  82.             elseif hit.Parent:WaitForChild("Humanoid") and hit.Name == "Head" then
  83.                 hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 35
  84.             end
  85.         end)
  86. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement