How would i go about fixing my coin collector mechanic?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so that when you click a coin, every second it decreases (mines) the coin’s hp by 1 and click it again or click another coin and it cancels the mining

  2. What is the issue? Include screenshots / videos if possible!
    When i tried making that it ended up either saying an error or just straight up saying nothing.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making remote function and the script using it but it ended up saying “attempt to call table value” and i tried other solutions but they didn’t work

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local script

local e = game.ReplicatedStorage.Events.mineCoin
local f = false
local result = e:InvokeServer()

result[2].ClickDetector.MouseClick:Connect(function(m)
	if type(result) == "table" then
		if not f then
			f = true
			result[1](m)
			result[3].Enabled = true
		elseif f then
			f = false
			result[1](m)
			result[3].Enabled = false
		end
	end
end)

server script

local c = script.Parent
local maxhp = c.Parent.mhp
local hp = c.Parent.hp
local bb = c.BillboardGui

local t = {}

local e = game.ReplicatedStorage.Events.mineCoin
local already_mining = script.mining

local function mineCoin(plr)
	local plrmining = plr:FindFirstChild("is_m")
	table.insert(t, c.ClickDetector)
	table.insert(t, c.BillboardGui)

	if already_mining.Value == false and plrmining.Value == false then
		already_mining.Value = true
		plrmining.Value = true
		bb.Enabled = true
		repeat
			if plrmining.Value == false or already_mining.Value == false or hp.Value <= 0 then
				break
			end
			wait(1)
			hp.Value -= 1
		until hp.Value <= 0 or plrmining.Value == false or already_mining.Value == false

		if plrmining.Value == false and already_mining == false then
			already_mining.Value = false
			plrmining.Value = false
			bb.Enabled = false
			return t
		elseif hp.Value == 0 and plrmining.Value == true and already_mining.Value == true then
			plrmining.Value = false
			c.Parent:Destroy()
		end
	else
		plrmining.Value = false
		already_mining.Value = false
		bb.Enabled = false
		return t
	end
	return t
end

e.OnServerInvoke = t

Any help will be welcomed!

1 Like

Here’s a simple system which achieves exactly what you’re trying to achieve.

local coin = script.Parent --Part instance.
local health = coin.Health --IntValue instance.
health.Value = 10 --Set coin's health.
local click = coin.ClickDetector --ClickDetector instance.

click.MouseClick:Connect(function(player) --Connect click detector's "MouseClick" event.
	health.Value -= 1 --Reduce coin's health by 1.
	if health.Value == 0 then
		--Do code here.
	end
end)

I’ve added comments which should hopefully explain the necessary details of the snippet.

image

Oops, i didn’t type the title right. Also what i am trying to achieve is trying to fix my coin collector mechanic. Hold on let me give you the updated version of my script after some research.

local c = script.Parent
local maxhp = c.Parent.mhp
local hp = c.Parent.hp
local bb = c.BillboardGui

local already_mining = false
local isminingthis = false


script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local plrmining = plr:FindFirstChild("is_m")
	isminingthis = true
	
	if already_mining == false then
		already_mining = true
		bb.Enabled = true
		repeat
			if already_mining == false or isminingthis == false or bb.Enabled == false then
				bb.Enabled = false
				isminingthis = false
				already_mining = false
				break
			end
			wait(1)
			if already_mining == false or isminingthis == false or bb.Enabled == false then
				bb.Enabled = false
				isminingthis = false
				already_mining = false
				break
			end
			hp.Value -= 1
		until hp.Value <= 0
		
		if isminingthis == false or bb.Enabled == false then
			already_mining = false
			bb.Enabled = false
		elseif hp.Value <= 0 and already_mining == true or hp.Value <= 0 and already_mining == false then
			c.Parent:Destroy()
		end
	else
		isminingthis = false
		already_mining = false
		bb.Enabled = false
	end
end)