-- Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")
local Sniper = script.Parent
local SniperFiredEvent = script:WaitForChild("SniperFired")
local Barrel = Sniper:WaitForChild("Barrel")
local RaycastPart = Barrel:WaitForChild("Raycast_Pos")
local Flash = RaycastPart:WaitForChild("Flash")
local SniperGui = Sniper:WaitForChild("SniperGui")
local Bullets = SniperGui:WaitForChild("Bullets")
local Ammunition = SniperGui:WaitForChild("BackgroundFrame"):WaitForChild("Ammunition")
local ScopeGui = Sniper:WaitForChild("SniperAimGui")
local LowerBody = Sniper:WaitForChild("Lower Body")
local EquipSound = LowerBody:WaitForChild("EquipSound2")
local UnequipSound = LowerBody:WaitForChild("UnequipSound")
local ReloadSound = LowerBody:WaitForChild("ReloadSound")
local FireSound = LowerBody:WaitForChild("FireSound")
local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = LocalPlayer.Character:WaitForChild("Humanoid")
local PlayerCamera = game.Workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()
local Mouse_Target = Mouse.Target
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local ShootingDebounce = false
local ReloadDebounce = false
local ScopeActivated = false
ScopeGui.Parent = PlayerGui
SniperGui.Parent = PlayerGui
local RegularFOV = 70
local AimingFOV = 10
local Equipped = false
local MAX_RANGE = 2048
local function UpdateBullets()
Ammunition.Text = Bullets.Value.. " / 10"
end
local function onEquipped()
Equipped = true
EquipSound:Play()
SniperGui.Enabled = true
end
local function onUnequipped()
Equipped = false
UnequipSound:Play()
ScopeActivated = false
ScopeGui.Enabled = false
SniperGui.Enabled = false
LocalPlayer.CameraMode = Enum.CameraMode.Classic
PlayerCamera.FieldOfView = RegularFOV
end
local function ReloadSniper()
ReloadDebounce = true
ScopeActivated = false
ScopeGui.Enabled = false
LocalPlayer.CameraMode = Enum.CameraMode.Classic
PlayerCamera.FieldOfView = RegularFOV
ReloadSound:Play()
task.wait(ReloadSound.TimeLength)
Bullets.Value = 1
ReloadDebounce = false
end
local function onReload(input, processed)
if processed == true or ReloadDebounce == true then return end
if input.KeyCode == Enum.KeyCode.R and Equipped == true then
if Bullets.Value <= 0 then
ScopeGui.Enabled = false
LocalPlayer.CameraMode = Enum.CameraMode.Classic
PlayerCamera.FieldOfView = RegularFOV
ReloadSniper()
else
return
end
else
return
end
end
local function Fired()
FireSound:Play()
Bullets.Value -= 1
Flash.Enabled = true
task.wait(0.1)
Flash.Enabled = false
end
local function onActivated()
if ShootingDebounce == true or ReloadDebounce == true or Bullets.Value <= 0 then return end
if ScopeActivated == false then
ShootingDebounce = true
task.spawn(Fired)
local RayCastParameters = RaycastParams.new()
RayCastParameters.FilterType = Enum.RaycastFilterType.Exclude
RayCastParameters.IgnoreWater = true
RayCastParameters.FilterDescendantsInstances = {Sniper, LocalPlayer.Character}
local Origin = RaycastPart.WorldCFrame.Position
local Direction = ((Mouse.Hit.Position - Origin).Unit) * MAX_RANGE
local RayResults = game.Workspace:Raycast(Origin, Direction, RayCastParameters)
if not RayResults then
SniperFiredEvent:FireServer(nil, Mouse.Hit.Position, nil, nil)
else
SniperFiredEvent:FireServer(RayResults.Instance, RayResults.Position, RayResults.Distance, RayResults.Instance.Parent:FindFirstChild("Humanoid"))
end
task.wait(3)
ShootingDebounce = false
else
ShootingDebounce = true
task.spawn(Fired)
if not Mouse_Target then
SniperFiredEvent:FireServer(nil, Mouse.Hit.Position, nil, nil)
else
SniperFiredEvent:FireServer(Mouse_Target, Mouse.Hit.Position, nil, Mouse_Target.Parent:FindFirstChild("Humanoid"))
end
task.wait(3)
ShootingDebounce = false
end
end
local function ToggleAim(input, processed)
if processed == true then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 and Equipped == true then
if ScopeActivated == false then
ScopeActivated = true
ScopeGui.Enabled = true
PlayerCamera.FieldOfView = AimingFOV
LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
else
ScopeActivated = false
ScopeGui.Enabled = false
PlayerCamera.FieldOfView = RegularFOV
LocalPlayer.CameraMode = Enum.CameraMode.Classic
end
end
end
local function PlayerDeath()
if ScopeGui.Enabled == true then ScopeGui:Destroy() end
ScopeActivated = false
PlayerCamera.FieldOfView = RegularFOV
LocalPlayer.CameraMode = Enum.CameraMode.Classic
end
Sniper.Equipped:Connect(onEquipped)
Sniper.Unequipped:Connect(onUnequipped)
Sniper.Activated:Connect(onActivated)
Bullets.Changed:Connect(UpdateBullets)
UserInputService.InputBegan:Connect(ToggleAim)
UserInputService.InputBegan:Connect(onReload)
Humanoid.Died:Once(PlayerDeath)
-- Server Script
local Debris = game:GetService("Debris")
local SniperFiredEvent = script.Parent
local Sniper = SniperFiredEvent.Parent.Parent
local Barrel = Sniper:WaitForChild("Barrel")
local RaycastPart = Barrel:WaitForChild("Raycast_Pos")
local MAX_RANGE = 2048
local function Laser_Hit(target_Instance, target_Position, target_Distance)
local Laser = Instance.new("Part")
Laser.Size = Vector3.new(0.1, 0.1, target_Distance)
Laser.Material = Enum.Material.Neon
Laser.Transparency = 0.5
Laser.Anchored = true
Laser.CanCollide = false
Laser.CanTouch = false
task.spawn(function()
if target_Instance.Name == "Head" then
Laser.Color = Color3.fromRGB(255, 255, 0)
else
Laser.Color = Color3.fromRGB(0, 255, 0)
end
end)
Laser.CFrame = CFrame.lookAt(RaycastPart.WorldCFrame.Position, target_Position) * CFrame.new(Vector3.new(0, 0, -target_Distance/2))
Laser.Parent = game.Workspace
Debris:AddItem(Laser, 50)
end
local function Laser_Missed(target_Position)
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(RaycastPart.WorldCFrame.Position, target_Position) * CFrame.new(Vector3.new(0, 0, -MAX_RANGE/2))
Laser.Parent = game.Workspace
Debris:AddItem(Laser, 50)
end
SniperFiredEvent.OnServerEvent:Connect(function(player, target_Instance, target_Position, target_Distance, target_Humanoid)
if target_Instance == nil or not target_Instance.Parent:FindFirstChild("Humanoid") or target_Instance.Parent:FindFirstChild("ForceField") then Laser_Missed(target_Position) return end
Laser_Hit(target_Instance, target_Position, target_Distance)
local Humanoid = target_Instance.Parent:FindFirstChild("Humanoid")
if target_Instance.Name == "Head" then
Humanoid:TakeDamage(100)
else
Humanoid:TakeDamage(50)
end
end)
I’m attempting to make it show the laser of the raycasting. When I shoot the sniper without aiming (raycast), it works perfectly fine. However, when I shoot it using the aimer, it doesn’t work and there are no errors. In the two images, it hit the Noob but the laser was red instead of green or yellow (green for normal shot and yellow for headshot) and the Noob doesn’t take any damage too. I don’t know what is causing this?