Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 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. beamEvent.OnServerEvent:connect(function(player, mouseHit, wandEndPoint)
  15.     local beam = Instance.new("Part", workspace)
  16.     beam.BrickColor = script.Color.Value
  17.     beam.FormFactor = "Custom"
  18.     beam.Size = Vector3.new(2, 1, 4)
  19.     beam.Material = "Neon"
  20.     beam.Transparency = 0.11
  21.     beam.Anchored = true
  22.     beam.Locked = true
  23.     beam.CanCollide = false
  24.     beam.Shape = Enum.PartType.Block
  25.     beam.CFrame = CFrame.new(wandEndPoint)
  26.            
  27.     beam.Touched:connect(function(otherPart)
  28.         -- Beam touched something - make sure it wasn't us
  29.         if player.Character and not otherPart:IsDescendantOf(player.Character) then
  30.             -- Check to see if it was a character
  31.             local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
  32.             if not humanoid then
  33.                 humanoid = otherPart.Parent.Parent:FindFirstChild("Humanoid")
  34.             end
  35.                    
  36.             if humanoid then
  37.                 -- We hit somebody - add hit code here
  38.                 humanoid:TakeDamage(110)
  39.             end
  40.             beam:Destroy()
  41.         end
  42.     end)       
  43.            
  44.     -- Change these to customize the beam movement
  45.     local moveSpeed = 1
  46.     local maxDisplacement = 2
  47.     local oscillationSpeed = 10
  48.     local oscillationAxis = "X" -- Set to "X" or "Y"
  49.     local beamTravelDistance = 150
  50.            
  51.     local destination = wandEndPoint + (mouseHit - wandEndPoint).unit *
  52.         math.min(beamTravelDistance, (mouseHit - wandEndPoint).magnitude)          
  53.            
  54.     -- Need to manually check for collisions with terrain in between our gun barrel and target point. Terrain dosn't fire Touched event
  55.     local ray = Ray.new(wandEndPoint, (mouseHit - wandEndPoint).unit * beamTravelDistance)
  56.     local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
  57.     if part and part.Name == "Terrain" then
  58.         if (position - wandEndPoint).magnitude < (destination - wandEndPoint).magnitude then
  59.             -- Hit terrain. Set beam to only travel until it hits blocking terrain
  60.             destination = wandEndPoint + (mouseHit - wandEndPoint).unit * (position - wandEndPoint).magnitude
  61.         end
  62.     end
  63.            
  64.     local isMoving = true
  65.     local rng = Random.new()
  66.     local beamPos = beam.CFrame.p
  67.     local count = 0
  68.            
  69.     while isMoving do
  70.         local newPos = MoveTowards(beamPos, destination, moveSpeed)
  71.         if newPos:isClose(destination, 1e-7) then
  72.             isMoving = false
  73.         end
  74.         local center = CFrame.new(newPos, destination)
  75.         beamPos = center.p             
  76.  
  77.         count = count + oscillationSpeed % 360             
  78.         local offsetCFrame = center * CFrame.new(oscillationAxis == "X" and math.sin(math.rad(count)) * maxDisplacement or 0,
  79.             oscillationAxis == "Y" and math.sin(math.rad(count)) * maxDisplacement or 0, 0)
  80.                
  81.         beam.CFrame = offsetCFrame
  82.                
  83.         wait()
  84.     end
  85.            
  86.     beam:Destroy()
  87. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement