Mining System Glitching

Im making a mining system that will give you money after you break an ore but the ore doesnt break. Here’s a video

Script:

local plr = game.Players.LocalPlayer
local char = plr.Character

local mouse = plr:GetMouse()

local tool = script.Parent

local Mine = tool:WaitForChild("Mine")
local MTrack = char.Humanoid.Animator:LoadAnimation(Mine)

local mining = false

local TS = game:GetService("TweenService")

local function RespawnTargetOre(target)
	if plr:FindFirstChild("mined") then
		plr.mined.Value += target.MaxHP.Value
	end
	local TClone = target:Clone()
	TClone.HealthBar:Destroy()
	TClone.HP.Value = TClone.MaxHP.Value
	game.ReplicatedStorage.REs.Mined:FireServer(target.MaxHP.Value, target.Name)
	wait(30)
	TClone.Parent = workspace.Map.Ores
end

tool.Activated:Connect(function()
	local target = mouse.Target.Parent
	
	if not mining and (char.HumanoidRootPart.Position - mouse.Target.Position).Magnitude <= 30 then
		mining = true
		MTrack:Play()
		if target:FindFirstChild("HP") and target:FindFirstChild("MaxHP") then
			if target:FindFirstChild("HP").Value == 1 then
				task.spawn(RespawnTargetOre, target)
			else
				target:FindFirstChild("HP").Value -= 1
				if not target:FindFirstChild("HealthBar") then
					local HBar = game.ReplicatedStorage.HealthBar:Clone()
					HBar.Parent = target
				end
				TS:Create(target.HealthBar.Frame.Frame, TweenInfo.new(0.25, Enum.EasingStyle.Quad), {Size = UDim2.new(target.HP.Value/target.MaxHP.Value,0,1,0)}):Play()
			end
		end
		MTrack.Stopped:Wait()
		mining = false
	end
end)
6 Likes

Is there any error log?
If it is, please show me.

1 Like

No error, I checked the console.

2 Likes

Idk if this will work but maybe you could do :Destroy and then give them the money afterwards like player.leaderstats.Cash = player.leaderstats.Cash + 10

Or whatever you named your parameters for the player

It’s a local script so it wont replicate to the server

not sure why theres 1 instead of 0
also its a local script

Cuz the ore isnt supposed to exist when it’s HP is 0. The event is tool.Activated so whenever they click HP reduces. I think you get what im saying

Maybe you could Destroy() first and then give the money?

I believe i know the issue. It could be that the value isn’t equaled to one when you mine it so it never fires the functions. So instead you want to check if it’s equal to 1 or under.

Instead of doing

You should use this, it will check if the 1 is equal to or greater than the HP value

If target:FindFirstChild(“HP”).Value <= 1 then

But the money is still being added which proves that it is running. The issue started occurring when I added the game.ReplicatedStorage.REs.Mined:FireServer(target.MaxHP.Value, target.Name) part. So I think it might be something related to that.

Would it work like this?

if target:FindFirstChild("HP") and target:FindFirstChild("MaxHP") then
	if target:FindFirstChild("HP").Value == 1 then
		task.spawn(RespawnTargetOre, target)
	end
	target:FindFirstChild("HP").Value -= 1
	if not target:FindFirstChild("HealthBar") then
	    local HBar = game.ReplicatedStorage.HealthBar:Clone()
	    HBar.Parent = target
	end
	TS:Create(target.HealthBar.Frame.Frame, TweenInfo.new(0.25, Enum.EasingStyle.Quad), {Size = UDim2.new(target.HP.Value/target.MaxHP.Value,0,1,0)}):Play()		
end

wdym by that?
(character limittt)

could you share the code that is ran on the server after the event is fired?

The issue appears to be server sided

Change it to what I posted? You check every time if the ore is at one hp and then finish the code?

game.ReplicatedStorage.REs.Mined.OnServerEvent:Connect(function(plr, amt, ore)
	if ore == "Coal" then
		if amt == 2 then
			plr.leaderstats.Cash.Value += 2
		else
			plr:Kick("Exploiting")
			warn(plr.Name.." has been kicked for exploiting.")
		end
	elseif ore == "Iron" then
		if amt == 5 then
			plr.leaderstats.Cash.Value += 5
		else
			plr:Kick("Exploiting")
			warn(plr.Name.." has been kicked for exploiting.")
		end
	elseif ore == "Gold" then
		if amt == 10 then
			plr.leaderstats.Cash.Value += 10
		else
			plr:Kick("Exploiting")
			warn(plr.Name.." has been kicked for exploiting.")
		end
	elseif ore == "Diamond" then
		if amt == 20 then
			plr.leaderstats.Cash.Value += 20
		else
			plr:Kick("Exploiting")
			warn(plr.Name.." has been kicked for exploiting.")
		end
	else
		plr:Kick("Exploiting")
		warn(plr.Name.." has been kicked for exploiting.")
	end
end)```

I think you could decrease the HP first and then check whether it’s reached zero.
(edited)
My bad, I didn’t see the remote.

I don’t see any part where the ore is actually destroyed?

Also the exploiting stuff is pointless because you can just repeatedly fire with amount 5

I was gonna add scripts to make sure player is within the range of the ore and stuff like that.

It’s done on the client and I want it only to it being shown destroyed for the player and not everyone in the server.

I don’t see it being destroyed in the client script either? What script is destroying it?