Why does my pickaxe tool sometimes work, and most times doesn't work?

I’m trying to create a pickaxe tool for a mining system that I’m creating, but, for some odd reason the pickaxe picks and chooses when it wants to send a raycast. I’ve tried changing the distance, tried going right up to the hitbox limits, however regardless it doesn’t seem to be working most of the time. I’m rather confused on what to do, and any help I get would be most appreciated.

Localscript in charge of sending out the raycast when within the confines of the hitbox

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 params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace:FindFirstChildOfClass("Terrain")}

Pickaxe.Activated:Connect(function()
	Hitbox.Touched:Connect(function() end)
	local function Check()
		for _, part in pairs(Hitbox:GetTouchingParts()) do
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, 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)
2 Likes

Have you checked the output for any errors?

1 Like

Unfortunately, there’s no error that I can see. In fact, I recently tried it and the raycasting worked perfectly, but the ore that I wanted it to mine, simply didn’t disappear all the while an ore which was greater than 2 studs away from me got affected??

Proof of raycast not working as intended
https://gyazo.com/1a6d010953678858b077859529f56bc3

2 Likes

I’m still stuck on this issue unfortunately. Any help would be most appreciated!

2 Likes

I’m hoping a fresh pair of eyes could assist me in figuring this out, as I’m still rather stumped on what to do.

2 Likes

I think it would be cool if you showed us a video of the problem, so we can understand better

Also, have you tried changing a bit and instead of a local script, you can try a server one?

Alright, so the problem I’m having is that the ray doesn’t seem to be interacting with the ore the way it should, as seen here
https://gyazo.com/6a6c4a0e474951950eff3ca78361e54e

This is what it does instead, absolutely nothing.
https://gyazo.com/1a6d010953678858b077859529f56bc3

1 Like

It only “sometimes work”? Because, i have seen that in the first gif, you showed in a baseplate with a bunch of bolders, the second one, is just a black zone (aka black box)

On the testing place, it does as it supposed to, however, in the actual game it only sometimes does as its supposed to, while the rest of the times I’ve tried it, it does nothing.

1 Like

Hmm, this might be something maybe with the model or the script, did you copy the original script and paste on the new part for the raycast?

The models are the exact same, and I only used the testing place as demonstration, as the ores in the testing place has an outdated script. I’m trying a new implementation using raycast, and for some reason the raycast will not work most of the time.

1 Like

Still strange, i can’t seem to help much but, that was my theory, sorry for not helping much, i can still try tho

It’s all good! I do appreciate you coming to my thread and trying to help me out with this.

1 Like

Try waiting for more people, also TRY HELPING THIS GUY GUYS!!!

I’m still looking for an answer… Any help would be greatly appreciated!

I’m still looking for an answer to this! Hopefully I’ll be able to find a solution soon…

i have the same issue maybe
my gun works in studio but only sometimes work in game