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