Ah nevermind then, that’s not really the main issue right now.
There’s probably a bunch of solutions but I’ll just give you one I’m just think of right now (and I’ll explain it)
Although it’s probably not the most performant way.
local IronOre = game.Workspace.IronOre
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
-- new variables
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Characted:FindFirstChild("Pickaxe")
local Count = 0
Hitbox.Touched:Connect(function(player)
local function Check()
Count += 1
local Cast = workspace:Raycast(Pickaxe.Position, Pickaxe.CFrame.LookVector * 2)
if Cast then
if Cast.Instance.Name ~= IronOre.Name then return end
else
return
end
end
repeat task.wait(1) Check() until Count == 5
OreTouchedEvent:FireServer(IronOre)
Count = 0
end)
There’s probably a better way but now I’m gonna explain each line
local function Check()
Just creating a new function
Count += 1
Increasing the counter each time (every second)
local Cast = workspace:Raycast(Pickaxe.Position, Pickaxe.CFrame.LookVector * 2)
Casting an invisible ray forward from the pickaxe’s position (make sure that your pickaxe’s LookVector is pointing forward)
if Cast then
Checking if the ray actually hit something
if Cast.Instance.Name ~= IronOre.Name then return end
If the instance that was hit is NOT the IronOre, then we’ll completely exit (the rest of the code will NOT RUN)
else
If our ray didn’t hit anything
return
If our ray didn’t hit anything, then we’ll exit (the rest of the code WILL NOT RUN)
repeat task.wait(1) Check() until Count == 5
Okay this explanation is gonna be the longest, so we will run the Check function every second (hence task.wait(1)
) 5 times
Why? Because the count is increased everytime the Check function runs (and you know now that it runs every second). So by the 5th time, the until condition (Count == 5) will have been met and the script will move onto the next line (firing to the server via your remoteEvent)
Sorry if this is a lot to take in. Hopefully I explained it kinda okay. And yes I know this is not the most performant since I’m firing a ray 5 times but uh I couldn’t think of anything else
You could also use Part:GetTouchingParts
every second but uhhh idk if that’s more performant than raycasting every second