Why is my raycast acting strangely? (Video Included)

It seems as if the raycast only works for one model at a time, and all else aren’t affected by the raycast. I’m unsure what could cause this, and I’m hoping to rectify it. I’ve been looking for a solution for quite some time now, and hopefully now due to the video, I could possibly receive a solution. The code responsible and the video will be included below.

Localscript

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)
	Count = 0
		end 	
	end)
end)

Serverscript

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:WaitForChild("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)

Video

External Media

Any assistance would be greatly appreciated, as I’ve been stuck on this issue for a couple of days now.

To help those who don’t want to download anything, I have compressed the video to fit within the guidelines.

I’m sorry but your code is a bit confusing to read.

In the client code, what’s the point of raycasting for each part in GetTouchingParts? Why not just check for rocks in GetTouchingParts itself?

Furthermore, why connect to the Touched event when the pickaxe is activated? Why not directly check the hitbox upon activation?

I think streamlining the control flow would make it much easier to debug.

The “Rock” is a model, and what I wanted to do is to be able to detect whenever a pickaxe is activated near the hitbox assigned to each instance of ores which spawn. Allowing the pickaxe a raycast to touch any part was my goal, however, there’s some mistakes as I’m rather new to scripting.

Currently, it seems as if it functions rather rarely, and usually only one or two “Rock” ores are able to be mined, and the rest does absolutely nothing.

I have a feeling it might be something with the distance, try setting it to something high like 10, if it detects anything then its your distance. You can also find a way to create a “debug” part or raycast visualizer part to debug the raycast.

I’ve tried changing the distance of the raycast, however, this wasn’t the solution as it did little to help my situation unfortunately. However, having raycast visualizer might help me.

Ah… I just remembered, the main problem is that the raycast only fires rarely. However, when it fires correctly, everything else falls in line. The problem here is finding what went wrong with the script that makes it work this way…

hold on, I just realized - is the local script inside one of the rocks?

Yes, the localscripts are parented by the models.

However, since the context of the scripts are “Client”, should this matter?

Ok, so you’re using RunContext instead of actual LocalScript objects. That’s fine. Are you sure every rock has one?

They’re all cloned from the one rock model within serverstorage, so the context should remain the same for each model I believe.

If the localscript is being cloned inside the rock then it would listen for the tool being activated everytime you spawn a new rock. I think this can lead to a memory leak, you should make the main raycasting logic inside a script inside the tool itself.

Thank you for noticing! I’ll avoid this memory leak once the main problem has been solved.

I would honestly recommend rewriting the client logic so it’s done by the pickaxe instead of each rock, then continue debugging from there. I feel like the issue could be BECAUSE it’s cloned into each rock in the first place. It would save you a big headache down the line.

Ah, that makes sense. The current client script is a bit odd, so I’ll rewrite this system, with hopefully a better outcome then this current script.