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?