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)
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…
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.
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.