Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local beamEvent = Instance.new("RemoteEvent")
- beamEvent.Name = "BeamEvent"
- beamEvent.Parent = game.ReplicatedStorage
- local function MoveTowards(current, target, maxDistanceDelta)
- local a = target - current
- local magnitude = a.magnitude
- if magnitude <= maxDistanceDelta or magnitude == 0 then
- return target
- end
- return current + a / magnitude * maxDistanceDelta
- end
- beamEvent.OnServerEvent:connect(function(player, mouseHit, wandEndPoint)
- local beam = Instance.new("Part", workspace)
- beam.BrickColor = script.Color.Value
- beam.FormFactor = "Custom"
- beam.Size = Vector3.new(2, 1, 4)
- beam.Material = "Neon"
- beam.Transparency = 0.11
- beam.Anchored = true
- beam.Locked = true
- beam.CanCollide = false
- beam.Shape = Enum.PartType.Block
- beam.CFrame = CFrame.new(wandEndPoint)
- beam.Touched:connect(function(otherPart)
- -- Beam touched something - make sure it wasn't us
- if player.Character and not otherPart:IsDescendantOf(player.Character) then
- -- Check to see if it was a character
- local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
- if not humanoid then
- humanoid = otherPart.Parent.Parent:FindFirstChild("Humanoid")
- end
- if humanoid then
- -- We hit somebody - add hit code here
- humanoid:TakeDamage(110)
- end
- beam:Destroy()
- end
- end)
- -- Change these to customize the beam movement
- local moveSpeed = 1
- local maxDisplacement = 2
- local oscillationSpeed = 10
- local oscillationAxis = "X" -- Set to "X" or "Y"
- local beamTravelDistance = 150
- local destination = wandEndPoint + (mouseHit - wandEndPoint).unit *
- math.min(beamTravelDistance, (mouseHit - wandEndPoint).magnitude)
- -- Need to manually check for collisions with terrain in between our gun barrel and target point. Terrain dosn't fire Touched event
- local ray = Ray.new(wandEndPoint, (mouseHit - wandEndPoint).unit * beamTravelDistance)
- local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
- if part and part.Name == "Terrain" then
- if (position - wandEndPoint).magnitude < (destination - wandEndPoint).magnitude then
- -- Hit terrain. Set beam to only travel until it hits blocking terrain
- destination = wandEndPoint + (mouseHit - wandEndPoint).unit * (position - wandEndPoint).magnitude
- end
- end
- local isMoving = true
- local rng = Random.new()
- local beamPos = beam.CFrame.p
- local count = 0
- while isMoving do
- local newPos = MoveTowards(beamPos, destination, moveSpeed)
- if newPos:isClose(destination, 1e-7) then
- isMoving = false
- end
- local center = CFrame.new(newPos, destination)
- beamPos = center.p
- count = count + oscillationSpeed % 360
- local offsetCFrame = center * CFrame.new(oscillationAxis == "X" and math.sin(math.rad(count)) * maxDisplacement or 0,
- oscillationAxis == "Y" and math.sin(math.rad(count)) * maxDisplacement or 0, 0)
- beam.CFrame = offsetCFrame
- wait()
- end
- beam:Destroy()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement