Why does my raycast stop working?

I’ve been having an issue with the way I have coded in a raycast, as it doesn’t seem to work after a while of working, or it’ll just not work at all. The system seems to want to decide when it’ll work and when it won’t, and I’ve been trying to figure this out for the past two days… Any help would be greatly appreciated.

The script found within a model by the name of “Rock”

local Rock = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0
local activatedPickaxe
local hitboxConnection = nil

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace:FindFirstChildOfClass("Terrain"), Character}

activatedPickaxe = Pickaxe.Activated:Connect(function()
	if hitboxConnection then hitboxConnection:Disconnect() end
	hitboxConnection = Hitbox.Touched:Once(function() do
	local function Check()
		for _, part in pairs(Hitbox:GetTouchingParts()) do
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 4, params)
			print(Cast)
			if Cast then
				if Cast.Instance.Name ~= Rock.Name then return end
			else
				return
			end
		end
	end

	repeat task.wait(1) Check() until Count == 5
	OreTouchedEvent:FireServer(Rock)
	activatedPickaxe:Disconnect()
	Count = 0
		end 	
	end)
end)

Serverscript which gets called after the localscript finishing

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = ReplicatedStorage.OreTouchedEvent
local OreTransparency = 0
local debounce = false
local debounce2 = false

OreTouchedEvent.OnServerEvent:Connect(function(player, Ore)
	local char = player.Character
	local hum = char:FindFirstChildOfClass("Humanoid")
	if not debounce then
	if hum then
		local pickaxe = char:FindFirstChild("Pickaxe")
		if pickaxe then
			for _, v  in(Ore:GetChildren()) do
				if v:IsA("BasePart") and v.Name ~= "Hitbox" then
					v.Transparency += 0.25
						if v.Transparency == 1 and v.Name == "Detect" then
							if Ore.Name == "Rock" then
								print("Rock!")
							elseif Ore.Name == "IronOre" then
								print("iron!")
							elseif Ore.Name == "AzureOre" then
								print("Azure Ore!")
							end
						end
						end
					end
				debounce = true
				task.wait(3)
				debounce = false
			end
		end				
	end
end)

That is disconnecting your event listener.

Where should I put it instead to disconnect the event?

I’m still looking for a solution to this! Any help is appreciated.

Hopefully I’ll get a new pair of eyes to help me with this!

Let me elaborate on the issue. It seems to only affect one ore, and this ore only goes transparent up until a certain point, where the raycast stops working, and the rock is unable to get to the last transparent stage.
image

I’m still hopefully looking for an answer to this! Any help is greatly appreciated.

my fried attention span doesn’t allow me to read everything, can you explain the problem in one message and include videos + code + all

I’ve already included all the code that i’ve written. But pretty much, the issue stems from the fact that the raycast doesn’t usually cast, and when it does, it only works for a bit and stops working.

don’t you need to entirely remove the :Disconnect() line to fix that?

also what do you mean by “works for a bit and stops working”

Isn’t it because the range is too low? I mean, Handle.CFrame.LookVector * 4 is 4 studs away from you. If you put like 1000 instead of 4?

The raycast doesn’t seem to cast most of the time, as proven by a lack of printing of the cast as shown within my localscript with this line. And when it does print what it’s supposed to, the printing seems to stop at a certain point.

local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 4, params)
			print(Cast)

The raycast is only supposed to cast right in front of the character, so that ores past an unrealistic point doesn’t get affected by the script.

However, I did try this by making it 10 studs in front of the character, however, this didn’t fix the issue unfortunately.

But why are you calling “:Disconnect()”?

To avoid any memory leaks, by disconnecting the event once it concludes.

I may be wrong, but if you Disconnect it, it won’t work again.

just disconnect it only when you unequip the tool, disconnecting it straight after firing the remote evnet will just break the entire code until the tool is reequipped again

I’ve removed the disconnect, and unfortunately the raycast isn’t functioning as i want it to

you gotta elaborate man charrrrrr