How would I make a laser going out of a pistol to visualize the raycast

Alright, so pretty much I’ve been working on this gun tool for my game, and pretty much I want to make it so that whenever my pistol is fired, It sends out a laser that shows what it hit, essentially.

Preferably explain it to me in a way that is more understandable, as I am still kinda beginning to get into the more complicated parts of scripting, like hitboxes and more. So that would be appreciated.

Here is my Local Script for the Pistol:

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local gui = player.PlayerGui.ScreenGui
local mouse = player:GetMouse()

local tool = script.Parent

local reloadSound = tool.ReloadSound

local cooldownTime = 0.3
local cooldown = false

local ammo = 6
local maxAmmo = 6
local reloading = false

local isEquipped = false

local function reload()
	if reloading == false then
		reloadSound:Play()
		reloading = true
		task.wait(1)
		ammo = maxAmmo
		reloading = false
	end
end

tool.Activated:Connect(function(hit)
	if cooldown == false and ammo > 0 and reloading == false then
		ammo -= 1
		cooldown = true
		tool.Shoot:FireServer(mouse.Hit.Position)
		task.wait(cooldownTime)
		cooldown = false
	elseif ammo <= 0 and reloading == false then
		reload()
	end
end)

uis.InputBegan:Connect(function(inpObj, gpe)
	if gpe == true then return end

	if isEquipped == true then
		if inpObj.KeyCode == Enum.KeyCode.R then
			reload()
		end
	end
end)

tool.Equipped:Connect(function(hit)
	isEquipped = true
	if isEquipped == true then
		gui.Enabled = true
	end
end)

tool.Unequipped:Connect(function(hit)
	isEquipped = false
	if isEquipped == false then
		gui.Enabled = false
	end
end)

while true do
	task.wait()
	gui.TextLabel.Text =  ammo.."/"..maxAmmo
end

Here is my ServerSide script for the pistol:

local event = script.Parent.Shoot
local tool = script.Parent
local shootSound = tool.ShootSound
local ImpactSound = tool.ImpactSound

event.OnServerEvent:Connect(function(player, mousePosition)
	shootSound:Play()
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local raycastResult = workspace:Raycast(tool.Handle.Barrel.Position, (mousePosition - tool.Handle.Barrel.Position) * 300, raycastParams)
	
	if raycastResult then
		local rayInstance = raycastResult.Instance
		local model = rayInstance:FindFirstAncestorOfClass("Model")
		
		if model:FindFirstChild("Humanoid") then
			ImpactSound:Play()
			model:FindFirstChild("Humanoid").Health -= 15
		end
	end
end)

What I want:

What I have right now:

a beam from the muzzle to the hit position

yeah, thats what i’m looking for.

Have ready an attachment for the muzzle, parented to the muzzle. Attachment at end of raycast. Parent the raycast attachment to terrain and make WorldPosition the raycast.position.

Add the beam instance, edit to your liking.

Another method. How do you visualize a raycast as a part?

3 Likes

make an attachment on the muzzle (keep it around permanently), make an attachment at the hit position, make a beam between the 2, keep it around for however long

It might have been confusing the way that these other guys said it, but they actually mean a beam instance, like a part.

You can insert one into the workspace and play around with it’s properties. From what you already know, you will probably be able to figure it out. :grin:

Roblox Studio: Basic Laser Beam Gun Tutorial
Making a laser beam that damages players in Roblox
How to Make a Laser Gun - Roblox Studio Tutorial

All these look better than the average.