Create Better Laser Eyes! #2

Hello this is my second tutorial but in this tutorial I will be showing you how to make Laser Eyes you can see my previous tutorial about the same topic(I won’t be going into detail too much since my previous tutorial has some of the details, so check it out if you get confused); Create some Laser Eyes

Getting Started/Setting it Up

Screen Shot 2021-10-10 at 4.50.52 PM
We are going to start by making a Folder in starter pack, add a server script, local script, and 2 remote events (one for before and one for the updates of the mouse), and a beam .You could name the things inside whatever you want. Inside the local script you can make a screengui. The beam and screen gui should look like this(does not have to be similar or a perfect copy but at least something);

Scripting the Laser

Local Script:

Edit; It should be wait fo chin and not find first child on the humanoid part.

local player = game.Players.LocalPlayer
local Folder = script.Parent
local Debris = game:GetService("Debris")
local RunService = game:GetService("RunService")
local Event = Folder.RemoteEvent
local ActivateEvent = Folder:WaitForChild("ActivateEvent")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local camera = workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local Switch = false
local Key = Enum.KeyCode.T
local MakeConnection = nil
local debounce = false
local PlayerGui = player:WaitForChild("PlayerGui")
local ReloadGui = PlayerGui:FindFirstChild("LaserReloadGui") or script:FindFirstChild("LaserReloadGui"):Clone()
ReloadGui.Parent = PlayerGui
ReloadGui.Enabled = false
local MoveFrame = ReloadGui:FindFirstChild("Frame").Frame
MoveFrame.Size = UDim2.fromScale(.9,.75)
local MaxUsageTime = 10
local ReloadingTime = 5
local Enabled = true

Humanoid.Died:Connect(function()
	if MakeConnection ~= nil then
		MakeConnection:Disconnect()
	end
end)

local TInfo1 = TweenInfo.new(MaxUsageTime,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
local TInfo2 = TweenInfo.new(ReloadingTime,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
local MakeTween=nil

local function Activate()
	if Enabled == true then
		ReloadGui.Enabled = true
		if Switch == false then
			Switch = true
			ActivateEvent:FireServer(Switch)
			local StartTick = tick()
			MakeTween = TweenService:Create(MoveFrame,TInfo1,{Size = UDim2.fromScale(.1,.75)})
			MakeTween:Play()
			MakeTween.Completed:Connect(function()
				if MakeTween.PlaybackState == Enum.PlaybackState.Completed then
					Enabled = false
					ActivateEvent:FireServer(false)
					local MakeTween2 = TweenService:Create(MoveFrame,TInfo2,{Size = UDim2.fromScale(.9,.75)})
					MakeTween2:Play()
					MakeTween2.Completed:Connect(function()
						Enabled = true
					end)

				end
			end)
			MakeConnection = RunService.RenderStepped:Connect(function(dt)	
				local DTick = tick()
				if DTick - StartTick < MaxUsageTime then
					if Switch == true and not debounce then
						debounce = true
						local mouse = UserInputService:GetMouseLocation()
						local length = 750			
						local unitray = camera:ScreenPointToRay(mouse.X,mouse.Y)
						Event:FireServer(length, unitray)
						wait(math.min(3*dt,.2))
						debounce = false
					end
				elseif DTick - StartTick >= MaxUsageTime then
					if MakeConnection ~= nil then
						MakeConnection:Disconnect()
					end
				end
			end)
		elseif Switch == true then
			Switch = false
			if MakeTween~=nil then
				MakeTween:Cancel()
			end
			ActivateEvent:FireServer(Switch)
			if MakeConnection~=nil then
				MakeConnection:Disconnect()
			end

		end
	end
end

UserInputService.InputBegan:Connect(function(input,gameProcessed)
	if not gameProcessed and input.KeyCode == Key then
		Activate()
	end
end)

We will use render step to fire the event before every Frame on the local script, and use MakeConnection swell so it stops using the render step when it is not necessary. We will then fire the first event to make the effects before update and then fire the remove event that you choose to update. You will choose key and get the frames that you made in the gui(you don’t have to use a gui but maybe a number value so that you can tween the time). We will use tick() to find if we hit the maxtime and disconnect.

Server Script

local Folder = script.Parent
local Debris = game:GetService("Debris")
local RunService = game:GetService("RunService")
local Event = Folder.RemoteEvent
local ActivateEvent = Folder:WaitForChild("ActivateEvent")
local debounce = false
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist	
local LaserDamage = 20/4

local function MakeLaserBeams(Attachment1,Attachment2)
for i=1,2 do
	local LaserBeam1 = Folder.LaserBeam:Clone()		
	LaserBeam1.Enabled = true	
	LaserBeam1.Attachment0 = Attachment1
	LaserBeam1.Attachment1 = Attachment2
		LaserBeam1.Parent = workspace
	end
end

local function MakeAttachments(AddToTable)
	local Attachments={}
	for i=1,2 do
	local Attachment = Instance.new("Attachment")		
	Attachment.Parent = workspace.Terrain	
	table.insert(Attachments,Attachment)
	table.insert(AddToTable,Attachment)	
	end
	return Attachments
end

local Offset1 = CFrame.new(.125,.25,0)	
local Offset2 = CFrame.new(-.125,.25,0)
local function ChangeCFrameTables(GetTable,NewCFrame)
	if GetTable ~= nil then
		for i,v in ipairs(GetTable) do
			local FindOffset = i==1 and Offset1 or i==2 and Offset2
			v.CFrame = NewCFrame*FindOffset
		end
	end
end

local function ClearAllTables(GetTable)
	if GetTable ~= nil then
	for i,v in pairs(GetTable) do
		Debris:AddItem(v,.01)
	end
		table.clear(GetTable)
	end
end

local MakeEffectTable = {}	
ActivateEvent.OnServerEvent:Connect(function(player,Switch)
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:FindFirstChildWhichIsA("Humanoid")
local rootpart = character:FindFirstChild("HumanoidRootPart")
local Head = character:FindFirstChild("Head")	
RayParams.FilterDescendantsInstances = {character}
if Switch == true then	
local GetAttchmentsOdd = MakeAttachments(MakeEffectTable)	
local GetAttchmentsEven = MakeAttachments(MakeEffectTable)	
MakeLaserBeams(GetAttchmentsOdd[1],GetAttchmentsEven[1])	
MakeLaserBeams(GetAttchmentsOdd[2],GetAttchmentsEven[2])	
Event.OnServerEvent:Connect(function(player,length,unitray)
	if debounce == false then
		debounce = true
		local origin = Head.CFrame.p 			
		local UnitDirection = unitray.Direction*length
		local Result = workspace:Raycast(origin, UnitDirection, RayParams)		
		local Interscection = Result and Result.Position or origin+UnitDirection
		local Distance = (origin-Interscection).Magnitude
		local Damageby_Distance = 25/Distance--the higher the denominator the more damage 			
		local OppositeCF = (CFrame.lookAt(Interscection, origin) * CFrame.new(0,0,Distance)	 ).p
		ChangeCFrameTables(GetAttchmentsOdd,CFrame.lookAt(origin,Interscection) )	
		ChangeCFrameTables(GetAttchmentsEven,CFrame.lookAt(Interscection,OppositeCF) )	
		spawn(function()		
			if Result and Result.Instance then
				local findInstance = Result.Instance
				local IsModel = findInstance.Parent:IsA("Model")		
				local HitHumanoid = (IsModel and findInstance.Parent:FindFirstChildWhichIsA("Humanoid") ) or findInstance.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
					if HitHumanoid then		
						local FindDamage = Damageby_Distance + LaserDamage	
						HitHumanoid:TakeDamage(FindDamage)
					end								
				end
			end)
		debounce = false
	end
end)
elseif Switch == false then	
		ClearAllTables(MakeEffectTable)
	end
end)

We will make a table will all the effects so that it will be easier to get all of them and delete them, We will be making functions to be able to make the laser beams(did a for loop so it can be brighter), make attachments, change the cframe of the attachments, and one function to clear all the effects. We will then get all the fired events and fire the functions to make the effects. Lastly, we will get the intersecting point change the cframes of the effects and do damage. If the switch is off we destroy the effects by calling the function to destroy all objects in the table.

Finally we are done I hope it did not take too long but we are done!! I hope you like it this second part was to be able to give you more variety and a more advanced laser eyes. Here is the link of where I made it and I will be updating it if you have suggestions.

https://www.roblox.com/games/7704289275/LaserEyesTest

13 Likes

The game doesn’t work.

I love the post btw.

It is working for me can you explain what is wrong??

did you press and hold T? if not then try it.

I think the error is from here:

Humanoid.Died:Connect(function()
	if MakeConnection ~= nil then
		MakeConnection:Disconnect()
	end
end)
1 Like

The game doesn’t work, please help?

1 Like

How would I update it faster so it doesn’t stay behind the player when you walk around? Also, how do I make the end be at the actual mouse position instead of under it? I can’t seem to figure it out.

1 Like

This post looks pretty cool, nice contribution!

Sorry for not responding, but to make it not lag behind just parent the attachments to the character instead of terrain, and as for the mouse I think I just learned to use camera:viewporttoray instead of screenpointtoray or you can use GuiService;GetGuiInsent and add it to the Y position of the mouse.

1 Like

Thanks I might update it or make the it a resource for people you use, but I will I see. If anyone has any tutorial ideas that I should make tell me.

Or you can just make it uncopylocked.

1 Like

Oh thanks for the advice! I will do it to help show the making of it.

Am making a superhero based game, and i would like to ask when.