Advertisement
Prexxo

Bullet Manager Module

Mar 21st, 2024
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local RunService = game:GetService("RunService")
  4. local Workspace = game:GetService("Workspace")
  5.  
  6. local Modules = require(ReplicatedStorage:WaitForChild("Modules"))
  7. local Network = Modules.Get("Network")
  8. local GunDatabase = Modules.Get("GunDatabase")
  9.  
  10. local Module = {}
  11.  
  12. if RunService:IsServer() then
  13.     local PlayerGunUseCache = {}
  14.  
  15.     local function CanFire(Player)
  16.         local LastFired = PlayerGunUseCache[Player]
  17.  
  18.         if not LastFired then
  19.             PlayerGunUseCache[Player] = os.clock()
  20.             return true
  21.         elseif os.clock()-LastFired > GunDatabase.Gun.ReloadTime then
  22.             PlayerGunUseCache[Player] = os.clock()
  23.             return true
  24.         end
  25.     end
  26.  
  27.     Network.ListenTo("FireGun", function(Player, Origin, Direction)
  28.         if not CanFire(Player) then return end
  29.  
  30.         for _, OtherPlayer in ipairs(Players:GetPlayers()) do
  31.             if OtherPlayer == Player then continue end
  32.  
  33.             Network.FireTo("Client", "ReplicateBullet", OtherPlayer, Origin, Direction)
  34.         end
  35.     end)
  36.    
  37. else--Is client
  38.     local Heartbeat = RunService.Heartbeat
  39.    
  40.     local BulletsFolder = Workspace:WaitForChild("Bullets")
  41.     local Bullet = script:WaitForChild("Bullet")
  42.    
  43.     local BulletDatabase = GunDatabase.Bullet
  44.     local BulletSpeed = BulletDatabase.Speed
  45.     local BulletGravity = BulletDatabase.Gravity
  46.     local AirFriction = BulletDatabase.AirFriction
  47.     local BulletFriction = BulletDatabase.Friction
  48.    
  49.     local BulletList = {}--[1] = {}
  50.     local RayParams = RaycastParams.new()
  51.     RayParams.FilterType = Enum.RaycastFilterType.Blacklist
  52.     RayParams.FilterDescendantsInstances = {BulletsFolder, Workspace.Entities}
  53.    
  54.     function Module.CreateBullet(Position, Direction)
  55.         local NewBullet = Bullet:Clone()
  56.         NewBullet.Position = Position
  57.         NewBullet.Position = Position
  58.         NewBullet.Parent = BulletsFolder
  59.        
  60.         table.insert(BulletList, {
  61.             Model = NewBullet,
  62.             TimeLeft = BulletDatabase.LifeTime,
  63.             Velocity = Direction * BulletDatabase.Speed,
  64.             Position = Position
  65.         })
  66.        
  67.        
  68.     end
  69.    
  70.     --Network.ListenTo("ReplicateBullet", function(OriginCFrame, BulletDirection)
  71.     --  table.insert(BulletList, {
  72.            
  73.     --  })
  74.     --end)
  75.    
  76.     Network.ListenTo("ReplicateBullet", Module.CreateBullet)
  77.    
  78.     Heartbeat:Connect(function(TimeElapsed)
  79.         for i = #BulletList, 1, -1 do
  80.             local BulletData = BulletList[i]
  81.             local Model = BulletData.Model
  82.             local Velocity = BulletData.Velocity
  83.             local TimeLeft = BulletData.TimeLeft
  84.            
  85.             TimeLeft -= TimeElapsed
  86.            
  87.             if TimeLeft < 0 or Velocity.Magnitude <= .5 then
  88.                
  89.                 Model:Destroy()
  90.                 table.remove(BulletList, i)
  91.                 continue
  92.             end
  93.            
  94.             local CurrentPos = Model.Position
  95.            
  96.             local NewVelocity = Velocity + BulletGravity * TimeElapsed
  97.             local Displacement = NewVelocity * TimeElapsed
  98.            
  99.             local NewPos = CurrentPos + NewVelocity * TimeElapsed
  100.            
  101.             local Results = Workspace:Raycast(NewPos - (Displacement.Unit * 0.05), Displacement, RayParams)
  102.            
  103.             if Results then
  104.                 local Normal = Results.Normal
  105.                
  106.                 local Dot = NewVelocity:Dot(Normal)
  107.                 local ReflectedNormal = -Normal * Dot * 2
  108.                
  109.                 local BulletMass = Bullet:GetMass()
  110.                 local CollideeMass = Results.Instance:GetMass()
  111.                
  112.                 local TotalMass = BulletMass + CollideeMass
  113.                
  114.                 local CollisionRatio = CollideeMass/TotalMass  
  115.                
  116.                 BulletData.Velocity = (NewVelocity + ReflectedNormal) * (1-BulletFriction*CollisionRatio)
  117.                 BulletData.Position = Results.Position-- Set to collision Point
  118.                
  119.                 local Sound = script.Hit:Clone()
  120.                 Sound.Parent = Model
  121.                 Sound:Play()
  122.                
  123.                 game.Debris:AddItem(Sound, Sound.TimeLength)
  124.                
  125.             else
  126.                
  127.                 BulletData.Position = NewPos
  128.                 BulletData.Velocity = NewVelocity
  129.             end
  130.            
  131.             Model.Position = BulletData.Position
  132.             BulletData.TimeLeft = TimeLeft
  133.         end
  134.     end)
  135. end
  136.  
  137. return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement