Raycast not working?

Probably doing something wrong but
ill aim at the same area, sometimes it will work othertimes it just wont for some reason
like the ray will be invalid / nil for some reason idk
Client Script

repeat task.wait() until game:IsLoaded() and script.Parent:FindFirstChild("Remotes")

-- Variables
local Player = game:GetService('Players').LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Ts = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local Tool = script.Parent
local RemotesFolder = Tool.Remotes
local RSRemotesFolder = game.ReplicatedStorage.Remotes
local Mesh = Tool.GlockMesh
local Handle = Tool.Handle
local BulletPoint = Mesh.BulletPoint
local ClientBullet = RSRemotesFolder.ClientBullet
local BulletFolder = game.ReplicatedStorage.Bullets
local FirePoint = Tool.GlockMesh.BulletPoint
local ShootEvent = RemotesFolder.Shoot
local ReloadEvent = RemotesFolder.Reload
local CheckEvent = RemotesFolder.Check
local BulletCoroutine
local RBullet

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist

-- Tool Data --
local IsAutomatic = true
local LastShotTime = os.clock()
local FireRate = 0.1

local IsHeldDown = false
local Distance = 300
local Bullet = game.ReplicatedStorage.Bullets.NormalBullet
--           --

-- Functions --
local function FireClientBullet(MousePosition)
	local Dbp1 = workspace.DebugPart1
	local Dbp2 = workspace.DebugPart2
	
	local MouseDirection = MousePosition.Direction * Distance
	local MouseRay = workspace:Raycast(MousePosition.Origin, MouseDirection, Params)
	
	local MouseRayPosition = if MouseRay then MouseRay.Position else (MousePosition.Origin + MouseDirection)
	Dbp1.Position = MouseRayPosition

	local BulletDirection = (MouseRayPosition - FirePoint.WorldPosition)
	
	local BulletRay = workspace:Raycast(FirePoint.WorldPosition, BulletDirection, Params)
	local HitPart = if BulletRay then BulletRay.Instance else nil
	local HitPosition = if BulletRay then BulletRay.Position else MouseRayPosition       
	Dbp2.Position = HitPosition
	
	local BClone = Bullet:Clone()
	BClone.Size = Vector3.new(.2, .2, (FirePoint.WorldPosition - HitPosition).Magnitude)
	BClone.CFrame = CFrame.new(FirePoint.WorldPosition:Lerp(HitPosition, .5), HitPosition)
	BClone.Parent = workspace
	
	Ts:Create(BClone, TweenInfo.new(.3, Enum.EasingStyle.Quad), {Transparency = 1}):Play()
	Debris:AddItem(BClone, .4)
end

local function Shoot()
	local Mouse = UserInputService:GetMouseLocation()
	local MousePosition = workspace.CurrentCamera:ViewportPointToRay(Mouse.X, Mouse.Y)
	local DirectTime = os.clock()

	ShootEvent:FireServer(MousePosition)
	FireClientBullet(MousePosition)
end

local function OnEquip()
	local Character = Tool.Parent
	
	Params.FilterDescendantsInstances = {Character}
end

local function OnUnequip()
	Params.FilterDescendantsInstances = {}
end


-- Main
Tool.Activated:Connect(Shoot)
Tool.Equipped:Connect(OnEquip)
Tool.Unequipped:Connect(OnUnequip)

Server Script

-- Variables
local Tool = script.Parent
local Data = Tool.Data
local Remotes = Tool.Remotes
local FirePoint = Tool.GlockMesh.BulletPoint

-- RaycastParams
local RCP = RaycastParams.new()
RCP.FilterType = Enum.RaycastFilterType.Blacklist
RCP.FilterDescendantsInstances = {}

-- Remotes
local ShootRemote = Remotes.Shoot
local ReloadRemote = Remotes.Reload
local CheckRemote = Remotes.Check

-- Script Data
local Distance = 300

-- Data

-- Functions
local function HitPlayer(Character)
	print(Character)
end

local function OnShoot(Player, MousePos)
	local Dbp3 = workspace.DebugPart3
	if Tool.Parent.Name:lower() == "backpack" then return end
	local MouseRay = workspace:Raycast(MousePos.Origin, (MousePos.Direction * Distance), RCP) 
	
	local MouseRayPosition = if MouseRay then MouseRay.Position else (MousePos.Origin + (MousePos.Direction * Distance))
	local BulletDirection = MouseRayPosition - FirePoint.WorldPosition
	
	local BulletRay = workspace:Raycast(FirePoint.WorldPosition, BulletDirection, RCP)
	
	local BulletHit = if BulletRay then BulletRay.Instance else nil
	local BulletPosition = if BulletRay then BulletRay.Position else MouseRayPosition
	
	Dbp3.Position =	BulletPosition
	print(if BulletRay then "BulletRayWorked" else "Didntwork")
	
	print(BulletHit)
	if BulletHit and BulletHit.Parent and BulletHit.Parent:FindFirstChild("HumanoidRootPart") then
		HitPlayer(BulletHit.Parent)
	end
end

local function OnEquip()
	local Character = Tool.Parent
	
	RCP.FilterDescendantsInstances = {Character}
end

local function OnUnequip()
	RCP.FilterDescendantsInstances = {}
end

-- Event Functions
ShootRemote.OnServerEvent:Connect(OnShoot)
Tool.Equipped:Connect(OnEquip)
Tool.Unequipped:Connect(OnUnequip)

Could still use some help on this :pray:

What are you trying to? the result of a ray will return nil, if no target was found.

I am guessing the Raycast is not going in the correct direction.

local MouseDirection = (MousePosition - FirePoint.WorldPosition).Unit * Distance
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.