HowToRoblox

GunServer

May 23rd, 2020
2,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local tool = script.Parent
  2.  
  3. local flashPart = tool:WaitForChild("FlashPart")
  4. local chamber = tool:WaitForChild("Chamber")
  5.  
  6. local trigger = tool:WaitForChild("Trigger")
  7. local triggerPoint1 = tool:WaitForChild("TriggerNotPressed")
  8. local triggerPoint2 = tool:WaitForChild("TriggerPressed")
  9.  
  10. local ammo = script:WaitForChild("Ammo")
  11.  
  12. local equipSound = script:WaitForChild("Equip")
  13. local reloadSound = script:WaitForChild("Reload")
  14. local shootSound = script:WaitForChild("Shoot")
  15.  
  16. local onBulletShot = script.Parent:WaitForChild("OnBulletShot")
  17. local onReload = script.Parent:WaitForChild("OnReload")
  18. local onCursorChange = script.Parent:WaitForChild("OnCursorChange")
  19.  
  20. local isReloading = false
  21.  
  22. local tweenService = game:GetService("TweenService")
  23.  
  24. local idleAnim = tool:WaitForChild("Idle")
  25. local idleAnimTrack
  26.  
  27. local shootAnim = tool:WaitForChild("Shoot")
  28.  
  29. local reloadAnim = tool:WaitForChild("Reload")
  30.  
  31.  
  32. local function playSound(sound)
  33.    
  34.     sound:Play()
  35. end
  36.  
  37. local function reload()
  38.    
  39.     if isReloading or ammo.Value == 10 then return end
  40.     isReloading = true
  41.    
  42.     onCursorChange:FireClient(game.Players:GetPlayerFromCharacter(tool.Parent), game.ServerStorage.CrosshairReloading.Texture)
  43.    
  44.     playSound(reloadSound)
  45.    
  46.     local reloadAnimTrack = tool.Parent.Humanoid:LoadAnimation(reloadAnim)
  47.     reloadAnimTrack:Play()
  48.    
  49.     chamber.Transparency = 1
  50.     for i, childOfChamber in pairs(chamber:GetChildren()) do
  51.        
  52.         if childOfChamber:IsA("MeshPart") then childOfChamber.Transparency = 1 end
  53.     end
  54.    
  55.     local chamberClone = game.ServerStorage.Chamber:Clone()
  56.     chamberClone.WeldConstraint.Part0 = chamberClone
  57.     chamberClone.WeldConstraint.Part1 = tool.Parent.LeftHand
  58.    
  59.     chamberClone.CFrame = tool.Parent.LeftHand.CFrame * CFrame.Angles(0, 90, 0) + Vector3.new(0, -0.196, 0)
  60.    
  61.     chamberClone.Parent = tool.Parent
  62.    
  63.     reloadAnimTrack.Stopped:Wait()
  64.    
  65.     chamberClone:Destroy()
  66.    
  67.     chamber.Transparency = 0
  68.     for i, childOfChamber in pairs(chamber:GetChildren()) do
  69.        
  70.         if childOfChamber:IsA("MeshPart") then childOfChamber.Transparency = 0 end
  71.     end
  72.    
  73.     reloadSound.Ended:Wait()
  74.    
  75.     ammo.Value = 10
  76.    
  77.     isReloading = false
  78.    
  79.     if game.Players:GetPlayerFromCharacter(tool.Parent) then onCursorChange:FireClient(game.Players:GetPlayerFromCharacter(tool.Parent), game.ServerStorage.Crosshair.Texture) end
  80. end
  81.  
  82. local function shootFunc(plr, hit, target)
  83.    
  84.     if isReloading then return end
  85.    
  86.     if ammo.Value < 1 then
  87.        
  88.         reload()
  89.        
  90.         return
  91.     end
  92.    
  93.     local ray = Ray.new(flashPart.Position, (hit.Position - flashPart.Position).unit * 100)
  94.    
  95.     local part, pos, globalVec = workspace:FindPartOnRay(ray, tool.Parent, false, true)
  96.    
  97.     if part then   
  98.         if part.Parent:FindFirstChild("Humanoid") then part.Parent.Humanoid:TakeDamage(30) end
  99.     end
  100.    
  101.     local bulletRay = Instance.new("Part")
  102.     bulletRay.CanCollide = false
  103.     bulletRay.Anchored = true
  104.    
  105.     local size = (hit.Position - flashPart.Position).Magnitude
  106.     if size > 2048 then size = 2048 end
  107.    
  108.     bulletRay.CFrame = CFrame.new(flashPart.Position, pos) * CFrame.new(0, 0, -size / 2)
  109.     bulletRay.Size = Vector3.new(0.1, 0.1, size)
  110.    
  111.     bulletRay.Color = Color3.fromRGB(255, 223, 0)
  112.    
  113.     bulletRay.Parent = workspace
  114.    
  115.    
  116.     local bulletHole = game.ServerStorage.BulletHole:Clone()
  117.    
  118.     bulletHole.CFrame = CFrame.new(hit.Position, hit.Position + globalVec)
  119.    
  120.     bulletHole.Parent = workspace
  121.    
  122.    
  123.     game:GetService("Debris"):AddItem(bulletRay, 0.2)
  124.     game:GetService("Debris"):AddItem(bulletHole, 5)
  125.    
  126.    
  127.     playSound(shootSound)
  128.    
  129.     ammo.Value = ammo.Value - 1
  130.    
  131.     tool.Parent.Humanoid:LoadAnimation(shootAnim):Play()
  132.    
  133.     for i, flashEffect in pairs(flashPart:GetChildren()) do
  134.        
  135.         if flashEffect.Name == "FlashGui" then flashEffect.Enabled = true end
  136.         if flashEffect.Name == "Light" then flashEffect.Enabled = true end
  137.     end
  138.    
  139.     wait(0.06)
  140.    
  141.     for i, flashEffect in pairs(flashPart:GetChildren()) do
  142.        
  143.         if flashEffect.Name == "FlashGui" then flashEffect.Enabled = false end
  144.         if flashEffect.Name == "Light" then flashEffect.Enabled = false end
  145.     end
  146. end
  147.  
  148.  
  149. tool.Equipped:Connect(function()
  150.    
  151.     onCursorChange:FireClient(game.Players:GetPlayerFromCharacter(tool.Parent), game.ServerStorage.Crosshair.Texture)
  152.    
  153.     local ammoGui = script.AmmoGui:Clone()
  154.     ammoGui.AmmoStatus.Text = ammo.Value .. "/10"
  155.    
  156.     ammoGui.Parent = game.Players:GetPlayerFromCharacter(tool.Parent).PlayerGui
  157.    
  158.     playSound(equipSound)
  159.    
  160.     if not idleAnimTrack then idleAnimTrack = tool.Parent.Humanoid:LoadAnimation(idleAnim) end
  161.    
  162.     if not idleAnimTrack.IsPlaying then idleAnimTrack:Play() end
  163. end)
  164.  
  165. tool.Unequipped:Connect(function()
  166.    
  167.     onCursorChange:FireClient(tool.Parent.Parent, "")
  168.    
  169.     if tool.Parent.Parent.PlayerGui:FindFirstChild("AmmoGui") then tool.Parent.Parent.PlayerGui.AmmoGui:Destroy() end
  170.    
  171.     playSound(equipSound)
  172.    
  173.     if idleAnimTrack and idleAnimTrack.IsPlaying then idleAnimTrack:Stop() end
  174. end)
  175.  
  176. onBulletShot.OnServerEvent:Connect(shootFunc)
  177.  
  178. onReload.OnServerEvent:Connect(reload)
  179.  
  180. ammo:GetPropertyChangedSignal("Value"):Connect(function()
  181.  
  182.     if game.Players:GetPlayerFromCharacter(tool.Parent) then game.Players:GetPlayerFromCharacter(tool.Parent).PlayerGui.AmmoGui.AmmoStatus.Text = ammo.Value .. "/10" end
  183. end)
Add Comment
Please, Sign In to add comment