RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"shoot a laser that moves to the mouse location using raycasting when E key is pressed"
-- local script local UserInputService = game:GetService("UserInputService") local RunService = game:GetService("RunService") local mouse = game.Players.LocalPlayer:GetMouse() local function shootLaser() local origin = script.Parent.Position local direction = (mouse.Hit.p - origin).unit * 1000 local ray = Ray.new(origin, direction) local hit, position = game.Workspace:FindPartOnRay(ray, script.Parent, false, true) if hit then local laser = Instance.new("Part") laser.BrickColor = BrickColor.new("Bright red") laser.Anchored = true laser.CanCollide = false laser.FormFactor = Enum.FormFactor.Custom laser.Size = Vector3.new(0.2, 0.2, (position - origin).magnitude) laser.CFrame = CFrame.new((origin + position) / 2, position) laser.Parent = game.Workspace game:GetService("Debris"):AddItem(laser, 0.1) end end UserInputService.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.E then shootLaser() end end) RunService.RenderStepped:Connect(function() if UserInputService:IsKeyDown(Enum.KeyCode.E) then shootLaser() end end)