I attempted to make a laser beam whenever the player misses their target or when they literally shoot at nothing (nil) but the beam is EXTREMELY inaccurate. Can anyone help please?
-- Local Script for when Pistol is activated
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("BulletRayPart")
local PistolFiredEvent = 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 Bullets = GunGui:WaitForChild("Bullets")
local BulletText = BackgroundFrame:WaitForChild("BulletsText")
local Shootingdebounce = false
local ReloadDebounce = false
local Equipped = false
local MAX_RANGE = 200
local function UpdateBullets()
BulletText.Text = Bullets.Value.. "/10"
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 Origin = BulletRayCastPart.Position
local Direction = ((Mouse.Hit.Position - Origin).Unit) * MAX_RANGE
local ExcludeList = {LocalPlayer.Character, script.Parent}
PistolFiredEvent:FireServer(Origin, Direction, {script.Parent, LocalPlayer.Character}, (Mouse.Hit.Position - Origin))
task.wait(0.2)
Shootingdebounce = false
end
end
Pistol.Equipped:Connect(onEquipped)
Pistol.Unequipped:Connect(onUnequipped)
Pistol.Activated:Connect(onActivated)
UserInputService.InputBegan:Connect(onReload)
Bullets.Changed:Connect(UpdateBullets)
-- Server Script for Raycasting and creating the laser
local Debris = game:GetService("Debris")
local SoundService = game:GetService("SoundService")
local PistolFiredEvent = script.Parent
local HeadshotSound = PistolFiredEvent:WaitForChild("Headshot")
local Pistol = PistolFiredEvent.Parent.Parent
local BulletRayCastPart = Pistol:WaitForChild("BulletRayPart")
local MAX_RANGE = 200
local function Laser_Hit(targetInstance, EndPosition, Distance)
local Laser = Instance.new("Part")
Laser.Size = Vector3.new(0.1, 0.1, Distance)
Laser.Material = Enum.Material.Neon
Laser.Transparency = 0.5
Laser.Anchored = true
Laser.CanCollide = false
Laser.CanTouch = false
task.spawn(function()
if targetInstance.Parent:FindFirstChild("ForceField") or not targetInstance.Parent:FindFirstChild("Humanoid") then Laser.Color = Color3.fromRGB(255, 0, 0) return end
if targetInstance.Name == "Head" then
Laser.Color = Color3.fromRGB(255, 255, 0)
local HeadShotSoundClone = HeadshotSound:Clone()
HeadShotSoundClone.Parent = targetInstance
HeadShotSoundClone:Play()
Debris:AddItem(HeadShotSoundClone, 2)
else
Laser.Color = Color3.fromRGB(0, 255, 0)
end
end)
Laser.CFrame = CFrame.lookAt(BulletRayCastPart.Position, EndPosition) * CFrame.new(Vector3.new(0, 0, -Distance/2))
Laser.Parent = game.Workspace
Debris:AddItem(Laser, 0.1)
end
local function Laser_Missed(EndPosition)
local Laser = Instance.new("Part")
Laser.Size = Vector3.new(0.1, 0.1, MAX_RANGE)
Laser.Material = Enum.Material.Neon
Laser.Transparency = 0.5
Laser.Anchored = true
Laser.CanCollide = false
Laser.CanTouch = false
Laser.Color = Color3.fromRGB(255, 0, 0)
Laser.CFrame = CFrame.lookAt(BulletRayCastPart.Position, EndPosition) * CFrame.new(Vector3.new(0, 0, -MAX_RANGE/2))
Laser.Parent = game.Workspace
Debris:AddItem(Laser, 30)
end
PistolFiredEvent.OnServerEvent:Connect(function(player, Origin, Direction, ExcludeList, Missed_End_Position)
local RayCastParameters = RaycastParams.new()
RayCastParameters.FilterType = Enum.RaycastFilterType.Exclude
RayCastParameters.FilterDescendantsInstances = ExcludeList
RayCastParameters.IgnoreWater = true
local RayResults = game.Workspace:Raycast(Origin, Direction, RayCastParameters)
if not RayResults then Laser_Missed(Missed_End_Position) return end
Laser_Hit(RayResults.Instance, RayResults.Position, RayResults.Distance)
if not RayResults.Instance.Parent:FindFirstChild("Humanoid") then return end
local Humanoid = RayResults.Instance.Parent:FindFirstChild("Humanoid")
if RayResults.Instance.Name == "Head" then
Humanoid:TakeDamage(15)
else
Humanoid:TakeDamage(10)
end
end)


