HowToRoblox

LaserCreator

May 12th, 2021 (edited)
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local tool = script.Parent
  2. local shootPos = tool:WaitForChild("ShootPosition")
  3.  
  4. local shootRE = tool:WaitForChild("ShootRE")
  5. local mouseRE = tool:WaitForChild("MouseRE")
  6.  
  7.  
  8. local mouseCF = CFrame.new()
  9. local camPos = Vector3.new()
  10. local firing = false
  11.  
  12.  
  13.  
  14. shootRE.OnServerEvent:Connect(function(plr, isFiring)
  15.    
  16.     firing = isFiring
  17. end)
  18.  
  19.  
  20. mouseRE.OnServerEvent:Connect(function(plr, mouseHit, cameraPosition)
  21.    
  22.     mouseCF = mouseHit
  23.     camPos = cameraPosition
  24. end)
  25.  
  26.  
  27.  
  28. while wait() do
  29.    
  30.    
  31.     local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
  32.    
  33.     if firing and plr then
  34.        
  35.         local bullet = game.ServerStorage:WaitForChild("Bullet"):Clone()
  36.        
  37.         local ray = Ray.new(camPos, mouseCF.LookVector * 10000)
  38.        
  39.         local part, position = workspace:FindPartOnRayWithIgnoreList(ray, plr.Character:GetDescendants())
  40.        
  41.         if not position then position = mouseCF.LookVector * 10000 end
  42.        
  43.         local distance = (position - shootPos.Position).Magnitude
  44.         local speed = distance / 50
  45.        
  46.         bullet.CFrame = CFrame.new(shootPos.Position, mouseCF.Position)
  47.        
  48.         local ti = TweenInfo.new(speed, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
  49.         local tween = game:GetService("TweenService"):Create(bullet, ti, {Position = position})
  50.         tween:Play()
  51.        
  52.         bullet.Parent = workspace
  53.        
  54.        
  55.         bullet.Touched:Connect(function()end)
  56.        
  57.         touched = {}
  58.        
  59.         coroutine.resume(coroutine.create(function()
  60.            
  61.             while bullet do
  62.                 wait()
  63.                 local touching = bullet:GetTouchingParts()
  64.                
  65.                 for i, part in pairs(touching) do
  66.                    
  67.                     if not part:IsDescendantOf(plr.Character) then
  68.                        
  69.                         local h = part.Parent:FindFirstChild("Humanoid")
  70.  
  71.                         if h and not touched[h] then
  72.  
  73.                             h:TakeDamage(30)
  74.                             touched[h] = true
  75.                         end
  76.  
  77.                         bullet:Destroy()
  78.                     end
  79.                 end
  80.             end
  81.         end))
  82.  
  83.        
  84.         shootPos.ShootSound:Play()
  85.         shootPos.ShootLight.Enabled = true
  86.         wait(0.1)
  87.         shootPos.ShootLight.Enabled = false
  88.        
  89.         wait(1)
  90.     end
  91. end
Add Comment
Please, Sign In to add comment