Descaii

Bullet Demo

Aug 31st, 2020 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. -- To be used with the quickscripts plugin https://www.roblox.com/library/5602756723/QuickScript
  2.  
  3. local BulletSpeed = 300 -- Feet per second, 300 is around the speed of a paintball, 900 is around the speed of a handgun
  4. local SimulationSpeed = 1 -- How fast time moves, Example; SimulationSpeed = 1/10 would make time flow 10 times slower
  5. local Gravity = 20 -- How much to accellerate bullets downwards, 1 gravity means Velocity.Y increases by -1 every second
  6.  
  7. local TestKey
  8. local Bullets = {}
  9.  
  10. local SpawnBullet = function(Origin, Direction, Speed)
  11.     local BulletData = {
  12.         Position = Origin;
  13.         Velocity = Direction.Unit * Speed;
  14.         LastPosition = nil;
  15.     }
  16.    
  17.     local raycastParams = RaycastParams.new()
  18.     raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
  19.     raycastParams.FilterDescendantsInstances = {workspace.Bullets, workspace.Hits}
  20.     BulletData.raycastParams = raycastParams
  21.    
  22.     local BulletPart = Instance.new("Part", workspace.Bullets)
  23.     BulletPart.Name = "BulletPart"
  24.     BulletPart.Size = Vector3.new(.45,.45,.45)
  25.     BulletPart.Color = Color3.new(1,1,0)
  26.     BulletPart.Anchored = true
  27.    
  28.     BulletData.Part = BulletPart
  29.    
  30.     Bullets[BulletData] = true
  31.    
  32.     return BulletData
  33. end
  34.  
  35. local DeleteBullet = function(BulletData)
  36.     repeat
  37.         BulletData.Part:Destroy()
  38.     until BulletData.Part.Parent == nil or _G.TestKey ~= TestKey
  39.     Bullets[BulletData] = nil
  40. end
  41.  
  42. local UpdateBullet = function(BulletData, Delta)
  43.     BulletData.Velocity = BulletData.Velocity - Vector3.new(0,Gravity,0)*Delta
  44.    
  45.     BulletData.LastPosition = BulletData.Position
  46.     BulletData.Position = BulletData.Position + BulletData.Velocity * (Delta or 1)
  47.    
  48.     local Difference = BulletData.Position - BulletData.LastPosition
  49.     local Distance = Difference.magnitude
  50.    
  51.     BulletData.Part.Size = Vector3.new(0.2, 0.2, math.max(Distance,0.2))
  52.     BulletData.Part.CFrame = CFrame.new(BulletData.Position, BulletData.LastPosition)
  53.     *CFrame.new(0,0,-BulletData.Part.Size.Z/2)
  54.    
  55.     local rayOrigin = BulletData.LastPosition
  56.     local rayDirection = BulletData.Position - BulletData.LastPosition
  57.    
  58.     local raycastResult = workspace:Raycast(rayOrigin, rayDirection, BulletData.raycastParams)
  59.    
  60.     if raycastResult then
  61.         local Marker = Instance.new("Part")
  62.         Marker.Size = Vector3.new(.4,.4,.2)
  63.         Marker.CFrame = CFrame.new(raycastResult.Position, raycastResult.Position + raycastResult.Normal)
  64.         Marker.Color = Color3.new(0.1,0.1,0.1)
  65.         Marker.Parent = workspace.Hits
  66.        
  67.         DeleteBullet(BulletData)
  68.     elseif BulletData.Position.Y < -100 then
  69.         DeleteBullet(BulletData)
  70.     end
  71.    
  72.     return raycastResult
  73. end
  74.  
  75. return function(QuickScripts)
  76.     repeat
  77.         TestKey = math.random(1,1000000)
  78.     until TestKey ~= _G.TestKey
  79.     _G.TestKey = TestKey
  80.    
  81.     if not workspace:FindFirstChild("Bullets") then
  82.         Instance.new("Model",workspace).Name = "Bullets"
  83.     end
  84.    
  85.     if not workspace:FindFirstChild("Hits") then
  86.         Instance.new("Model",workspace).Name = "Hits"
  87.     end
  88.    
  89.     workspace.Bullets:ClearAllChildren()
  90.     workspace.Hits:ClearAllChildren()
  91.    
  92.     local plugin = QuickScripts.plugin
  93.     plugin:Activate(true)
  94.  
  95.     local Mouse = plugin:GetMouse()
  96.    
  97.     local BulletShooter = Mouse.Button1Down:connect(function(k)
  98.         local Camera = workspace.CurrentCamera
  99.         SpawnBullet(Camera.CFrame.p-Vector3.new(0,1,0), Camera.CFrame.LookVector, BulletSpeed)
  100.     end)
  101.    
  102.     local BulletUpdater;BulletUpdater = game:GetService("RunService").RenderStepped:connect(function(Delta)
  103.         if _G.TestKey ~= TestKey then
  104.             BulletUpdater:disconnect()
  105.             BulletShooter:disconnect()
  106.             return
  107.         end
  108.        
  109.         for BulletData in pairs(Bullets) do
  110.             UpdateBullet(BulletData, Delta * SimulationSpeed)
  111.         end
  112.     end)
  113. end
Add Comment
Please, Sign In to add comment