Lightning Powerup V2

This is version 2 of the post I uploaded some days ago, which was solved successfully after I change some line of code. However there is now one more thing that I wish to accomplish, but I have no idea how and that is to re-position the points that the lightning will travel to based on mouse Position.

Version One:

Lightning Powerup

With the current code, I cannot fulfill the above, but it has a specific location that the points will be positioned in each gameplay session, meaning it doesn’t change based on mouse Position. Below a video for reference can be found:

https://gyazo.com/7599d877b065e5c749d5da92165a845d

I have tried a lot of solutions, such as searching this hub, but I couldn’t find anything to this issue. For further reference, I did follow a tutorial, so maybe if you watch it, you could understand the solution to this better.

ROBLOX SCRIPTING TUTORIAL | CREATING ANIME LIGHTNING - YouTube

The game is on 2 axis, the Y and X, while the Z axis is locked. If anybody could help me, it would mean the world to me. As said above, I think I located the line of code that needs changing so what I wish to accomplish can happen, but I don’t how to change it.

local SegementPosition = startPos + (endPos - startPos).Unit  * i * 1.2

I’ll also provide the rest of the scripts, just in case the above line of code doesn’t need to be changed:

Module Script

local module = {}

module.createLightning = function(startPos, endPos, segements)
	
	local Players = game:GetService("Players")
	local Player = Players.LocalPlayer
	local mouse = Player:GetMouse()
	local mousePos = mouse.Hit.Position
	local points = {}
	local Model = Instance.new("Model")
	Model.Name = "Lightning"
	Model.Parent = game.Workspace
	
	for i = 0, segements do
		
		local offset = Vector3.new(math.random(-1, 1), math.random(-1, 1), 0)
		local Origin = Player.Character["Right Arm"]
		local Start = Origin.Position
		
		local Position = startPos + (endPos - startPos).Unit * i * 1.2 

		if i == 0 or i == segements then
			offset = Vector3.new(0,0,0)
		end
		points[#points + 1] = Position + offset
		

	end
	
	for i = 1, #points do
		if points[i + 1] ~= nil then
		local Lightning = script.Lightning:Clone()
			Lightning.Size = Vector3.new(0.25,0.25, (points[i] - points[i + 1]).Magnitude)
			Lightning.CFrame = CFrame.new(((points[i] + points[i+1]) / 2), points[i + 1])
			Lightning.Parent = Model	
		end	
	end
	
	return Model
end

return module

Local Script

local Module = require(game.ReplicatedStorage.Scripts.ModuleScript)
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local UserInputService = game:GetService("UserInputService")
local mouse = Player:GetMouse()
local mousePos = mouse.Hit.Position


local function Lightning()
	coroutine.wrap(function()
		while UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) do 
			local MouseToCharacterPosition = Player.Character.HumanoidRootPart.CFrame:PointToObjectSpace(mouse.Hit.Position)
			local Zaxis = MouseToCharacterPosition.Z
			if Zaxis > 0 or Zaxis > -7 then return end
			if Zaxis < -7 then	
			task.wait(0.2)
			local Position = Vector3.new(mouse.Hit.Position.X, mouse.Hit.Position.Y)
				local Model = Module.createLightning(Player.Character["Right Arm"].Position, Position, 10)

			for _, Part in pairs(Model:GetChildren()) do
				local Fade = TweenService:Create(Part, TweenInfo.new(0.2, Enum.EasingStyle.Bounce), {Transparency = 1})
				Fade:Play()
				Fade:Destroy()
			end
			 game:GetService("Debris"):AddItem(Model, 0.2)
			end	
		end	
	end) ()
end

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		Lightning()
		local LightningEvent = game.ReplicatedStorage.CharacterEvents.COWTACOLightning
		LightningEvent:FireServer()
	end
end)

To clarify, I want the points that the lightning will travel to, to position themselves based on the mouse.Position, so if my mouse clicks beyond the limit or less than the limit that can be found in the video and in that one line of code, the lightning travels further or less than the limit. I hope you understood what I wished to do and for further clarification, I’ll be available in the replies. Thank you in advance.