Lightning Powerup

Hello yet again! I am COWTACO, a fellow scripter and this time I’ve encountered an issue that I couldn’t really manage to solve and I’d be glad if anybody could assist me towards solving it, as it would mean the world to me.

Recently I followed a tutorial in regards to making a lightning powerup, however within that video there wasn’t really any indication as to how you can make shoot it from your character’s hands to your mouse.Hit.Position, so I tried to do so. I did manage to shoot it from my character’s hands, but it never reached my mouse Position and stayed to my player’s arms. I suggest taking a look at the tutorial as it may help us further into solving this issue.

Video Link: ROBLOX SCRIPTING TUTORIAL | CREATING ANIME LIGHTNING - YouTube

Issue Link: https://gyazo.com/627f77b3739107d204f563c06ac29cb1

I tried using a line of code I’ve used for my projectile, but it didn’t work. Please do keep in mind that this game is 2.5D and the lightning will only travel within the x, y axis as the Z axis is locked.

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 * Vector3.new(CFrame.new(mouse.Hit.Position.X, mouse.Hit.Position.Y, Player.Character.HumanoidRootPart.CFrame.LookVector))

		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 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, Player.Character.HumanoidRootPart.CFrame.LookVector)
				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)

There are no errors in the output and everything works as intended except for the lightning which doesn’t move to the mouse Position.

I think you will need something other than mouse.hit. If I remember, if it’s not pointed at a part, it won’t work. Try aiming it at a part instead to see if that’s it.

Nope, didn’t really solve it. Tried it as of now and nothing happened.

Hmm, thanks for trying. I would check something I wrote, but I am away currently and can’t test it out.

Oh, it is fine. If you want you can maybe post it here so I can try it out myself, if you wish to.

1 Like

It will be a while, I am out of state for another week. Sorry

1 Like

I was able to make it follow my mouse, however I would also wish to be able to resize it based on how far/close is to my character and not stay the same. I think the following code needs to be altered, but I am not certain how, so it takes into consideration where my mouse is and how far should the lightning go.

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

I managed to get the following result:

https://gyazo.com/7599d877b065e5c749d5da92165a845d

As you can see from the above video, the lightning stays the same wherever the mouse is clicking. I wish to change that so it resizes depending on where the mouse clicks and that is why I think the above line of code should be changed. If anyone is aware of the answer, I won’t be on my computer for now, but when I will, I’ll gladly try your solution!

1 Like

Since I accomplished what I wanted for this post, I’ll make another asking how I could resize based on mouse position.

1 Like