Coin system not working properly?

Hey there! me (for like the hundredth time) asking for ideas in the forums on how to solve this simple road block, im not too familiar with all api stuff i mean i doubt anyone is but yeah, i don’t know how to solve this one.

--__COINS.DescendantAdded:Connect(function()
	for _, ncoin in pairs(__COINS:GetDescendants()) do
		if ncoin:FindFirstChild("ClickDetector") and ncoin:FindFirstChild("DC") then
			local clickd = ncoin:WaitForChild("ClickDetector")
			local dc = ncoin:WaitForChild("DC")
			local sound = ncoin.ClickOnCoin
			local sound2 = ncoin.breakc

			clickd.MouseClick:Connect(function(plr)
				sound:Play()
				task.wait(.25)
				sound:Stop()	
				if clicks[plr] == 0 then
					if debounce[plr] == false then
						debounce[plr] = true
						clicks[plr] +=1
						coinClicked(plr,dc,ncoin,clickd,sound2)
					end
				else
					clicks[plr] = 0
					cancel[plr] = true
				end
			end)
		end
	end
--end)

as you can see “DescendantAdded” is commented, why? because it literally breaks my script. Now, why would i add this in the first place? well, if i add any other coins, it wont let other coins be clicked. so i tried adding a “while loop”, i clicked a newly added coin and doesn’t work, i remove loop, it works. i try “descendant added” also doesn’t work, maybe script doesn’t work under being called too many times? i doubt it tho, function is only called when you click the coin.

also a question im curious about (yes, related to scripting)

alright, i know im lazy and all with the variables being bad (not sure) but i see free models go “i__run_service” as a run service variable or “v9” as an instance or something, it just feels so hard to read even for the scripter (maybe not i mean they created it), prob used to prevent readability for exploiters specially in actual games.

Thanks!!

2 Likes

Have you considered using ChildAdded instead of DescendantAdded?
The for _, ncoin in pairs(__COINS:GetDescendants()) do part also seems quite useless to me.
As it seems you are trying to check when a coin is added to the __COINS folder (or model or anything) and make it so you can click on it.

Also as per the “weird variable naming” thingy, it’s mostly the developers adding temporary variables with names such as v9 or x or m. As for the weird service naming, it’s probably in context to what the service is being used. Or maybe they just like it that way. There’s no real definitive answer to that

1 Like

Its coins are stored inside more folders inside of __COINS that folder being the area’s name

yup.

1 Like

Are the folders inside __COINS variable being moved/removed/added at any time (ingame)?

1 Like

Looking back, have you tried this?

__COINS.DescendantAdded:Connect(function(ncoin)
	if ncoin:FindFirstChild("ClickDetector") and ncoin:FindFirstChild("DC") then
		local clickd = ncoin:WaitForChild("ClickDetector")
		local dc = ncoin:WaitForChild("DC")
		local sound = ncoin.ClickOnCoin
		local sound2 = ncoin.breakc

		clickd.MouseClick:Connect(function(plr)
			sound:Play()
			task.wait(.25)
			sound:Stop()	
			if clicks[plr] == 0 then
				if debounce[plr] == false then
					debounce[plr] = true
					clicks[plr] +=1
					coinClicked(plr,dc,ncoin,clickd,sound2)
				end
			else
				clicks[plr] = 0
				cancel[plr] = true
			end
		end)
	end
end)

Also where is this script located? (is it a local one, or server one)

1 Like

Actually, no changes to the folders, only coins being added inside them

1 Like

Yeah, thats why descendantAdded is commented (unless theres more additions u did)

Server script service

1 Like

Well before you would listen for a descendant added and then loop all the descendants (which seemed quite useless!) so I mainly removed the unneeded loop and took the exact coin added from the event (ncoin variable)

1 Like

I think the problem is, you are connecting a callback to the MouseClick event for each of the ClickDetectors, multiple times.

This should fix your problem:

local function DescendantAdded(ncoin)
    if ncoin:FindFirstChild("ClickDetector") and ncoin:FindFirstChild("DC") then
			local clickd = ncoin:WaitForChild("ClickDetector")
			local dc = ncoin:WaitForChild("DC")
			local sound = ncoin.ClickOnCoin
			local sound2 = ncoin.breakc

			clickd.MouseClick:Connect(function(plr)
				sound:Play()
				task.wait(.25)
				sound:Stop()	
				if clicks[plr] == 0 then
					if debounce[plr] == false then
						debounce[plr] = true
						clicks[plr] +=1
						coinClicked(plr,dc,ncoin,clickd,sound2)
					end
				else
					clicks[plr] = 0
					cancel[plr] = true
				end
			end)
    end
end

for _, v in ipairs(__COINS:GetDescendants()) do
    DescendantAdded(v)
end

__COINS.DescendantAdded:Connect(DescendantAdded)
2 Likes

When a descendant gets added its also adding all the children from that decendant. I would recommend using childadded
So if i added a part, and in that part is a click detector, DescendantAdded will fire 2 times. Then it will stop the script because its waiting for the click detector and DC. Which is obviously not in the click detector

_COINS.DescendantAdded:Connect(function(Part)
print(Part) --Would print "Coin","ClickDetector"
end)
1 Like

tried this

__COINS.DescendantAdded:Connect(function(ncoin)
	if ncoin:FindFirstChild("ClickDetector") and ncoin:FindFirstChild("DC") then
		local clickd = ncoin:WaitForChild("ClickDetector")
		local dc = ncoin:WaitForChild("DC")
		local sound = ncoin.ClickOnCoin
		local sound2 = ncoin.breakc

		clickd.MouseClick:Connect(function(plr)
			sound:Play()
			task.wait(.25)
			sound:Stop()	
			if clicks[plr] == 0 then
				if debounce[plr] == false then
					debounce[plr] = true
					clicks[plr] +=1
					coinClicked(plr,dc,ncoin,clickd,sound2)
				end
			else
				clicks[plr] = 0
				cancel[plr] = true
			end
		end)
	end
end)

didn’t work :frowning:

@DevSarim’s post does seem to work maybe because of the for loop, though when i removed the loop, i tried it once and it worked and the second time it didn’t, ill wait and see before i mark as a solution.

No actually,

here im checking if its a coin.