Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. local beamEvent = Instance.new("RemoteEvent")
  2. beamEvent.Name = "BeamEvent"
  3. beamEvent.Parent = game.ReplicatedStorage
  4.  
  5. local function MoveTowards(current, target, maxDistanceDelta)
  6.     local a = target - current
  7.     local magnitude = a.magnitude
  8.     if magnitude <= maxDistanceDelta or magnitude == 0 then
  9.         return target
  10.     end
  11.     return current + a / magnitude * maxDistanceDelta
  12. end
  13.  
  14. local function PositionBeam(p1, p2, beam, beamWidth)
  15.     local x = CFrame.new((p1 + p2) / 2, p2)
  16.     local mag = ( p1 - p2 ).magnitude
  17.     beam.Size = Vector3.new(beamWidth, beamWidth, mag)
  18.     beam.CFrame = x
  19. end
  20.  
  21. beamEvent.OnServerEvent:connect(function(player, mouseHit, wandEndPoint)
  22.     -- Change these to customize the beam movement
  23.     local moveSpeed = 8
  24.     local maxDisplacement = 2
  25.     local beamTravelDistance = 200
  26.     local beamWidth = 0.3
  27.     local offsetInterval = 1
  28.    
  29.     local beam = Instance.new("Part", workspace)
  30.     beam.BrickColor = script.Color.Value
  31.     beam.FormFactor = "Custom"
  32.     beam.Size = Vector3.new(beamWidth, beamWidth, 4)
  33.     beam.Material = "Neon"
  34.     beam.Transparency = 0.11
  35.     beam.Anchored = true
  36.     beam.Locked = true
  37.     beam.CanCollide = false
  38.     beam.Shape = Enum.PartType.Block
  39.    
  40.     local tip = CFrame.new(wandEndPoint)
  41.     local rotated = tip * CFrame.fromAxisAngle(player.Character.Head.CFrame:toAxisAngle())
  42.     local worldSpace = rotated:toWorldSpace(CFrame.new(0, 0, -beam.Size.Z / 2))
  43.     beam.CFrame = worldSpace
  44.            
  45.     beam.Touched:connect(function(otherPart)
  46.         -- Beam touched something - make sure it wasn't us
  47.         if player.Character and not otherPart:IsDescendantOf(player.Character) then
  48.             -- Check to see if it was a character
  49.             local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
  50.             if not humanoid then
  51.                 humanoid = otherPart.Parent.Parent:FindFirstChild("Humanoid")
  52.             end
  53.                    
  54.             if humanoid then
  55.                 -- We hit somebody - add hit code here
  56.                 humanoid:TakeDamage(110)
  57.             end
  58.             beam:Destroy()
  59.         end
  60.     end)       
  61.            
  62.     local destination = wandEndPoint + (mouseHit - wandEndPoint).unit * beamTravelDistance         
  63.            
  64.     -- Need to manually check for collisions with terrain in between our gun barrel and target point. Terrain dosn't fire Touched event
  65.     local ray = Ray.new(wandEndPoint, (mouseHit - wandEndPoint).unit * beamTravelDistance)
  66.     local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
  67.     if part and part.Name == "Terrain" then
  68.         if (position - wandEndPoint).magnitude < (destination - wandEndPoint).magnitude then
  69.             -- Hit terrain. Set beam to only travel until it hits blocking terrain
  70.             destination = wandEndPoint + (mouseHit - wandEndPoint).unit * (position - wandEndPoint).magnitude
  71.         end
  72.     end
  73.            
  74.     local isMoving = true
  75.     local rng = Random.new()
  76.     local beamPos = beam.CFrame.p
  77.     local count = 0
  78.     local firstFire = true
  79.     local previousPosition
  80.            
  81.     while isMoving do
  82.         local newPos = MoveTowards(beamPos, destination, moveSpeed)
  83.         if newPos:isClose(destination, 1e-7) then
  84.             isMoving = false
  85.             break
  86.         end
  87.         local center = CFrame.new(newPos, destination)
  88.         beamPos = center.p             
  89.        
  90.         if count % offsetInterval == 0 then
  91.             if firstFire then
  92.                 firstFire = false
  93.                 beam.CFrame = center
  94.                 previousPosition = center * CFrame.new(0, 0, -beam.Size.Z/2)
  95.             else
  96.                 local nextBeamPosition = center * CFrame.new(rng:NextNumber(-maxDisplacement, maxDisplacement), rng:NextNumber(-maxDisplacement, maxDisplacement), 0)
  97.                 PositionBeam(previousPosition.p, nextBeamPosition.p, beam, beamWidth)
  98.                 previousPosition = nextBeamPosition
  99.             end
  100.         end
  101.        
  102.         count = count + 1
  103.         if count == 100 then count = 0 end
  104.         wait()
  105.     end
  106.            
  107.     beam:Destroy()
  108. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement