In my mining game, I have a pickaxe tool that you can use by either clicking once for the tool to be used once, or by holding it down and having the pickaxe mine automatically. However, the issue with this is that whenever the player holds the mouse button while the pickaxe is on debounce, the pickaxe will not begin to mine after the debounce is complete. It is very strange, because it makes the pickaxe occasionally just not work if the player doesn’t use it correctly.
local CollectionService = game:GetService("CollectionService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local mouse = Player:GetMouse()
local Pickaxe = script.Parent
local Configs = Pickaxe:WaitForChild("Configurations")
local Speed = Configs.Speed.Value
local Power = Configs.Power.Value
local MineAnimation = Humanoid:LoadAnimation(Pickaxe.MineAnimation1)
local MineRE = game.ReplicatedStorage.RemoteEvents.Mine
local Mining = false
local tickNextPick = 0
local function Mine()
MineAnimation:Play()
if CollectionService:HasTag(mouse.Target, "Mineable") then
local Range = (HumanoidRootPart.Position - mouse.Target.Position).magnitude
if Range <= 20 then
MineRE:FireServer(mouse.Target, Power)
end
end
end
Pickaxe.Activated:Connect(function()
if tick() > tickNextPick then
Mining = true
while Mining do
Mine()
wait(Speed)
end
end
end)
Pickaxe.Deactivated:Connect(function()
if Mining then
Mining = false
tickNextPick = tick() + Speed
end
end)
if anyone knows how I could fix this, I would appreciate it!
tick() is deprecated so i use os.clock() for basically all of my timing
Wait so the problem with the pickaxe is that you cant hold the mouse down if you just finishing mining?
you could try this:
local function Mine(Speed)
MineAnimation:Play()
if CollectionService:HasTag(mouse.Target, "Mineable") then
local Range = (HumanoidRootPart.Position - mouse.Target.Position).magnitude
if Range <= 20 then
MineRE:FireServer(mouse.Target, Power)
tickNextPick = tick() + Speed -- or use os.clock() I like it a lot more
end
end
end
Pickaxe.Activated:Connect(function()
Mining = true
while Mining do
if tick() >= tickNextPick then
Mine()
wait(Speed)
end
end
end)
Pickaxe.Deactivated:Connect(function()
-- you could just
Mining = false
if Mining then
Mining = false
-- tickNextPick = tick() + Speed -- I would remove this
end
end)
The issue is that every time you click your mouse button this fires, so when it’s using the while Mining do debounce loop if you lift and click again you have the problem.
Pickaxe.Activated:Connect(function()
if tick() > tickNextPick then
Mining = true
while Mining do
Mine()
wait(Speed)
end
end
end)
try
if tick() > tickNextPick then
Mining = true
while Mining do
Mine()
wait(.05)
end
wait(Speed)
end
I tried this, and it just resulted in the player being able to mine extremely fast, and the pickaxe functionality being bricked after use. Not sure why, but I’ll try to look and see whats causing this in this code. Thank you for the help though.
Im getting an error for this part: tickNextPick = tick() + Speed
The error: Players.AwesomeAiden_8.Backpack.DevPick.PickScript:26: attempt to perform arithmetic (add) on number and nil - Client - PickScript:26
I’m not too familiar os.clock. Would I have to make edits to the code to implement it (besides replacing all the tick() with os.clock() )? Because I am considering using it instead of tick.
I fixed that weird error, but now when I try to click while on debounce, it causes a massive lag spike and does nothing. Probably rapidly sending remote events??? I’m getting prints from the servers end when I do it, so I should probably deal with that.