Hello this is my first tutorial but in this tutorial I will be showing you how to make Laser Eyes
Setting it up
We are going to start by making a Tool in starter pack, add a server script, local script, and remote event, you could name it whatever you want(Make sure it has require handle off).
Then we are going to add a part/meshpart in server storage but I am going to be using a mesh part I made with this tutorial: VFX Tutorial | ROBLOX
Next put it in server storage and I am going to be naming it LaserPart(Make sure it is anchored and can collide off).
Scripting the Laser
local Tool = script.Parent
local player = game.Players.LocalPlayer
--local Mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local Event = Tool:WaitForChild("RemoteEvent")
local RunService = game:GetService("RunService")
local Equipped = false
local camera = workspace.CurrentCamera
Tool.Equipped:Connect(function()
Equipped = true
end)
Tool.Activated:Connect(function()
RunService.Heartbeat:Connect(function()
if Equipped then
local mouse = UserInputService:GetMouseLocation()
local unitray = camera:ScreenPointToRay(mouse.X , mouse.Y)
Event:FireServer(unitray)
end
wait()
end)
end)
Tool.Unequipped:Connect(function()
Equipped = false
end)
We will use heartbeat to fire the event every Frame on the local script and use the wait just in case it causes lots of lag, and use the equip variable so it won’t fire the event when the tool is equipped(We are using unitary since for some reason using mouse hit p would make the part go up if anyone knows why could you tell me).
local Tool = script.Parent
local Event = Tool:WaitForChild("RemoteEvent")
local Switch = true
local Equipped = false
local Length = 500
local Damage = 24
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Debris = game:GetService("Debris")
Tool.Equipped:Connect(function()
Equipped = true
end)
local Folder = Instance.new("Folder", workspace)
Folder.Name = "LaserParts"
local function CFramePointRightSide(Pos1 ,HitPosition) -- Use this if you are using a cylinder and for the size change the x axis and for the cframe
local directionToFace = (Pos1 - HitPosition).unit
local worldUp = Vector3.new(0,1,0)
local zAxisFace = directionToFace:Cross(worldUp)
local yAxisFace = directionToFace:Cross(zAxisFace)
return CFrame.fromMatrix(Pos1, directionToFace, yAxisFace, -zAxisFace)
end
Tool.Activated:Connect(function()
if Switch then
Switch = false
Beam = game.ServerStorage:WaitForChild("LaserPart"):Clone()
Beam.Parent = Folder
Event.OnServerEvent:Connect(function(player, unitray)
if Equipped and not Switch then
local character = player.Character
local Humanoid = character:WaitForChild("Humanoid")
if Humanoid and Humanoid:GetState() ~= Enum.HumanoidStateType.Dead and character:WaitForChild("HumanoidRootPart") then
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Head = character:WaitForChild("Head")
rayParams.FilterDescendantsInstances = {Beam, character, Folder}
local origin = Head.CFrame.Position
local direction = unitray.Direction * Length--(MouseHit - origin).Unit * Length
local LaserRay = workspace:Raycast(origin, direction, rayParams)
if LaserRay then
else
LaserRay = {}
LaserRay.Position = origin + direction
end
local Distance = (origin - LaserRay.Position).Magnitude
Beam.Size = Vector3.new(1,1,Distance)
Beam.CFrame = CFrame.lookAt(origin, LaserRay.Position) * CFrame.new(0,0, -Distance/2)
if LaserRay and LaserRay.Instance then
local object = LaserRay.Instance
if object.Parent:FindFirstChildWhichIsA("Humanoid") then
local Enemy = object.Parent:FindFirstChildWhichIsA("Humanoid")
Enemy:TakeDamage(Damage)
end
end
end
end
wait()
end)
elseif not Switch then
Switch = true
Debris:AddItem(Beam, .01)
end
end)
Tool.Unequipped:Connect(function()
Equipped = false
Debris:AddItem(Beam, .01)
end)
Lastly, we have to make the server script work we start with some variables to and make a click variable to turn off and on the laser. We clone the laser part before the event is fired then we get the character and make a ray, and we could now cframe and change the size of the laser part by the laser position.We then get the part that was hit and find a humanoid and damage then. Finallyyy we are done! Hopefully you liked this tutorial since it was my first one.