Trying to make a "laser raycast" when firing a pistol

I’m trying to make a laser part so that when I fire the pistol, it will make this “laser raycast” thing but for some reason it keeps going under the map and I don’t know why? (I teleported my character model to see where the laser part was)

I was trying to follow this youtube tutorial: https://www.youtube.com/watch?v=_aQCVl-ZGqo and it worked perfectly but when I tried to use it for my pistol it didn’t work.

Here is the pistol script (Local Script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")

local Pistol = script.Parent
local Handle = script.Parent:WaitForChild("Handle")
local BulletRayCastPart = Pistol:WaitForChild("BulletRay")
local PistolFiredFunction = script:WaitForChild("PistolFired")

local LocalPlayer = game.Players.LocalPlayer
local PlayerCamera = workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local Flash = Handle:WaitForChild("Flash")

local Sounds = {
	EquipSound = Handle:WaitForChild("EquipSound2"),
	UnequipSound = Handle:WaitForChild("UnequipSound"),
	ReloadSound = Handle:WaitForChild("Reload"),
	TickSound = Handle:WaitForChild("Tick"),
	FireSound = Handle:WaitForChild("FireSound")
}

local GunGui = Pistol:WaitForChild("AmmunitionGui"):Clone()
GunGui.Parent = PlayerGui

local BackgroundFrame = GunGui:WaitForChild("BackgroundFrame")
local BulletText = BackgroundFrame:WaitForChild("Bullets")
local Bullets = BulletText:WaitForChild("BulletsQuantity")

local Shootingdebounce = false
local ReloadDebounce = false
local Equipped = false

local function UpdateBullets()
	BulletText.Text = Bullets.Value
end

local function Fired()
	Sounds["FireSound"]:Play()
	Bullets.Value -= 1
	Flash.Enabled = true
	task.wait(0.1)
	Flash.Enabled = false
end

local function onEquipped()
	Equipped = true
	Sounds["EquipSound"]:Play()
	GunGui.Enabled = true
end

local function onUnequipped()
	Equipped = false
	Sounds["UnequipSound"]:Play()
	GunGui.Enabled = false
end

local function onReload(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.R and Equipped == true then
		if Bullets.Value < 10 and ReloadDebounce == false then
			ReloadDebounce = true
			Sounds["ReloadSound"]:Play()
			task.wait(2)
			Bullets.Value = 10
			ReloadDebounce = false
		else
			return
		end
	else
		return
	end
end

local function onActivated()
	if Shootingdebounce == true or ReloadDebounce == true then return end
	if Bullets.Value > 0 then
		Shootingdebounce = true
		task.spawn(Fired)
		
		local RayCastParameters = RaycastParams.new()
		RayCastParameters.FilterType = Enum.RaycastFilterType.Exclude
		RayCastParameters.FilterDescendantsInstances = {LocalPlayer.Character, script.Parent}
		RayCastParameters.IgnoreWater = true
		
		local Origin = BulletRayCastPart.Position
		local Direction = (Mouse.Hit.Position - BulletRayCastPart.Position).Unit * 500
		
		local RayResults = game.Workspace:Raycast(Origin, Direction, RayCastParameters)
		
		if not RayResults then
			task.wait(0.2)
			Shootingdebounce = false
		else
			if RayResults.Instance.Parent:FindFirstChild("Humanoid") and not RayResults.Instance.Parent:FindFirstChild("ForceField") then
				PistolFiredFunction:InvokeServer(RayResults.Instance, RayResults.Position, RayResults.Distance)
				task.wait(0.2)
				Shootingdebounce = false
			else
				task.wait(0.2)
				Shootingdebounce = false
			end
		end
	end
end

Pistol.Equipped:Connect(onEquipped)
Pistol.Unequipped:Connect(onUnequipped)
Pistol.Activated:Connect(onActivated)
UserInputService.InputBegan:Connect(onReload)
Bullets.Changed:Connect(UpdateBullets)

Here is the script for when the pistol is fired

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")

local Pistol = script.Parent
local Handle = script.Parent:WaitForChild("Handle")
local BulletRayCastPart = Pistol:WaitForChild("BulletRay")
local PistolFiredFunction = script:WaitForChild("PistolFired")

local LocalPlayer = game.Players.LocalPlayer
local PlayerCamera = workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local Flash = Handle:WaitForChild("Flash")

local Sounds = {
	EquipSound = Handle:WaitForChild("EquipSound2"),
	UnequipSound = Handle:WaitForChild("UnequipSound"),
	ReloadSound = Handle:WaitForChild("Reload"),
	TickSound = Handle:WaitForChild("Tick"),
	FireSound = Handle:WaitForChild("FireSound")
}

local GunGui = Pistol:WaitForChild("AmmunitionGui"):Clone()
GunGui.Parent = PlayerGui

local BackgroundFrame = GunGui:WaitForChild("BackgroundFrame")
local BulletText = BackgroundFrame:WaitForChild("Bullets")
local Bullets = BulletText:WaitForChild("BulletsQuantity")

local Shootingdebounce = false
local ReloadDebounce = false
local Equipped = false

local function UpdateBullets()
	BulletText.Text = Bullets.Value
end

local function Fired()
	Sounds["FireSound"]:Play()
	Bullets.Value -= 1
	Flash.Enabled = true
	task.wait(0.1)
	Flash.Enabled = false
end

local function onEquipped()
	Equipped = true
	Sounds["EquipSound"]:Play()
	GunGui.Enabled = true
end

local function onUnequipped()
	Equipped = false
	Sounds["UnequipSound"]:Play()
	GunGui.Enabled = false
end

local function onReload(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.R and Equipped == true then
		if Bullets.Value < 10 and ReloadDebounce == false then
			ReloadDebounce = true
			Sounds["ReloadSound"]:Play()
			task.wait(2)
			Bullets.Value = 10
			ReloadDebounce = false
		else
			return
		end
	else
		return
	end
end

local function onActivated()
	if Shootingdebounce == true or ReloadDebounce == true then return end
	if Bullets.Value > 0 then
		Shootingdebounce = true
		task.spawn(Fired)
		
		local RayCastParameters = RaycastParams.new()
		RayCastParameters.FilterType = Enum.RaycastFilterType.Exclude
		RayCastParameters.FilterDescendantsInstances = {LocalPlayer.Character, script.Parent}
		RayCastParameters.IgnoreWater = true
		
		local Origin = BulletRayCastPart.Position
		local Direction = (Mouse.Hit.Position - BulletRayCastPart.Position).Unit * 500
		
		local RayResults = game.Workspace:Raycast(Origin, Direction, RayCastParameters)
		
		if not RayResults then
			task.wait(0.2)
			Shootingdebounce = false
		else
			if RayResults.Instance.Parent:FindFirstChild("Humanoid") and not RayResults.Instance.Parent:FindFirstChild("ForceField") then
				PistolFiredFunction:InvokeServer(RayResults.Instance, RayResults.Position, RayResults.Distance)
				task.wait(0.2)
				Shootingdebounce = false
			else
				task.wait(0.2)
				Shootingdebounce = false
			end
		end
	end
end

Pistol.Equipped:Connect(onEquipped)
Pistol.Unequipped:Connect(onUnequipped)
Pistol.Activated:Connect(onActivated)
UserInputService.InputBegan:Connect(onReload)
Bullets.Changed:Connect(UpdateBullets)

Here is the script that the youtuber used

local LaserPart = script.Parent
local Noob = game.Workspace:WaitForChild("Noob")

local Laser = Instance.new("Part")
Laser.Parent = game.Workspace
Laser.CanCollide = false
Laser.CanTouch = false
Laser.Anchored = true
Laser.Material = Enum.Material.Neon
Laser.Transparency = 0.5

while task.wait(0.1) do
	local RayCastParameters = RaycastParams.new()
	RayCastParameters.FilterType = Enum.RaycastFilterType.Exclude
	RayCastParameters.FilterDescendantsInstances = {script.Parent}
	RayCastParameters.IgnoreWater = true
	local Direction = (Noob.PrimaryPart.Position - LaserPart.Position)
	local RayResults = game.Workspace:Raycast(LaserPart.Position, Direction, RayCastParameters)
	
	if not RayResults then
		Laser.Color = Color3.fromRGB(255, 0, 0)
		print("Nothing got hit!")
	else
		Laser.Color = Color3.fromRGB(0, 255, 0)
		local StartingCFrame = CFrame.new(LaserPart.Position, RayResults.Position)
		local CF = StartingCFrame:ToWorldSpace(CFrame.new(0, 0, -RayResults.Distance/2))
		Laser.CFrame = CF
		Laser.Size = Vector3.new(1, 1, RayResults.Distance)
	end
end

I don’t know what went wrong but I think I should’ve done everything correctly?

I’d be looking at the differences between the two over the script … at 1st.

What exactly are you trying to get this “Laser Raycast Thing” to do?

If you’re trying to make the laser stretch to where the raycast hits, you should know that you arent’t modifying the CFrame or Size of the laser unlike the youtuber’s script.

I’m trying to make this “laser raycast” thing to just make a new part (looks like a small long laser depending on the distance) that comes out of the pistol and depending if it’s a headshot it will turn yellow or green if not. I’m trying to make the laser’s CFrame or position to the tip of the gun to the what the raycast hit (RayResults.Instance) but it for some reason teleports under the map and I don’t understand why?

I don’t know what I’m doing wrong. Can you help me? :frowning:

your local script and gun script are the exact same. did you copy and paste the wrong script into the post? because there’s not any point within the scripts where you changed the CFrame or the size to do what the video showed

OH! I’m so sorry, I just realized. Let me change it.

This is the server script for my pistol where I tried to add the “laser raycast” thing

local Debris = game:GetService("Debris")

local PistolFiredFunction = script.Parent
local Pistol = PistolFiredFunction.Parent.Parent
local RayCastPart = Pistol:WaitForChild("BulletRay")
local StartPos = RayCastPart.Position

local function CreateLaser(targetInstance, EndPos, Distance)
	local Laser = Instance.new("Part")
	Laser.CanCollide = false
	Laser.CanTouch = false
	Laser.Anchored = true
	Laser.Material = Enum.Material.Neon
	Laser.Transparency = 0.5
	Laser.Size = Vector3.new(1, 1, Distance)
	Laser.Parent = game.Workspace
	
	task.spawn(function()
		if targetInstance.Name == "Head" then
			Laser.Color = Color3.fromRGB(255, 255, 0)
		else
			Laser.Color = Color3.fromRGB(0, 255, 0)
		end
	end)
	
	local StartingCFrame = CFrame.new(StartPos, EndPos)
	local CF = StartingCFrame:ToWorldSpace(CFrame.new(0, 0, -Distance/2))
	Laser.CFrame = CF
end

PistolFiredFunction.OnServerInvoke = function(player, targetInstance, EndPos, Distance)
	CreateLaser(targetInstance, EndPos, Distance)
	if targetInstance.Name == "Head" then
		targetInstance.Parent.Humanoid.Health -= 15
	else
		targetInstance.Parent.Humanoid.Health -= 10
	end
end

try using some print functions to see what the it thinks the origin and the end pos is. Also print out the distance

Okay, I’ll try that out! Thank You! :smiley:





I teleported my character model to both the starting CFrame and CF and they both for some reason teleported me to that same spot where the laser was under the map.


I shot the Noob a couple times more but from a farther distance and the “raycast lasers” were kind of bugging out but came from the same position

sorry man but im stumped. Im not sure if its a calculation issue or what. to me it seems like it should be working.

I honestly have absolutely no idea what I did wrong.

Respectfully I aint reading allat :pray::sob:
Anyway I did look through this and saw the images and stuff so I’m aware of what you’re looking for.

I made a quick function on mobile so check for errors but it should work.
Here:

local function CreateLazer(Origin:Vector3, EndPosition:Vector3, Distance:number)
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(1,1,Distance)
	Part.Material = Enum.Material.Neon
	Part.Transparency = 0.5
	Part.Anchored = true
	Part.CanCollide = false
	
	Part.CFrame = CFrame.lookAt(Origin, EndPosition) * CFrame.new(Vector3.new(0,0,-Distance/2))
	
	Part.Parent = workspace
end

I included typecasting to tell you what it takes in but I’ll explain params eitherway. Origin = your origin point / raycast origin, EndPosition = where your raycast hit, Distance = raycast distance

Uh… it didn’t work and I tried shooting it at different distances

Whats your origin point?

This text will be blurredThis text will be blurredThis text will be blurredThis text will be blurred

My origin point is right in front of the pistol (invisible part)

I tested it on my own and it works Idk wsp w urs
Before:
image

After:
image

Different distance:
image

Mind sending me the gun to check it out?