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
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.