Simple script help

Hi, I’m new to scripting. Currently I want my script to only give 10 coins to leaderstats, however even with my debounce included. It records the mouseclicked event 1-20 times when i’ve just clicked it once, therefore giving the player anywhere from 10 coins to 200.

local spawn1 = game.Workspace.SpawnLocation
–local appleparts = apple:GetChildren()

while true do
task.wait(3)
local apple1 = game.Workspace.AllApples:GetChildren()
for _, v in pairs (apple1) do
if v:IsA(“Model”) then
local apple = v:GetChildren()
local debounce = true
for _, v1 in pairs (apple) do
if v1:IsA(“MeshPart”) and debounce ~= nil then
v.ClickDetector.MouseClick:Connect(function(plr)
local debounce = false
print (“MouseClicked1”)
v1.Anchored = false
v1.AssemblyLinearVelocity = Vector3.new(10,25,0)
local humanoid = plr.Character:WaitForChild(“Humanoid”)
local animate = humanoid:WaitForChild(“Animator”)
local kickanimation = Instance.new(“Animation”)
kickanimation.AnimationId = “rbxassetid://16826458454”
local kickanimationtrack = animate:LoadAnimation(kickanimation)
kickanimationtrack:Play()
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 10

					for _, v in pairs (apple) do
						if v:IsA("MeshPart") then
							task.wait(3)

							v.Parent:Destroy()
							break


						end

					end
					local debounce = false
					task.wait(1)
					
						
					
					
					
					
				end)
				

			end
			
				
		end
	end
end

end

IF it helps, this is the SS

1 Like

The first problem that I see here is that mouseClick is getting connected multiple times, the connection is forever, and it can be broken only with connection:Disconnect(), I recommend instead connecting event, that will check if debounce is true, after give the coins and do the animation that you did, then debounce to false, wait 3 seconds, debounce back to true. So you don’t need while true, you will need it if you have appearing items that are connected through collection service.

1 Like

could i be using a better event than mouseclicked?

1 Like

I’m pretty sure MouseClick is the only “click detecting method” that ClickDetectors have

1 Like

Ok thank you : ) --------------

1 Like

By the way could you put all of this code (copy and paste) it into a code snippet

like this:

CODE

just click the gear icon and click Preformatted Text

that way people can quote your code easily

.MouseClick is only method of ClickDetector, I’ll make a better explain:

  1. Connect MouseClick in which:
  2. if debounce == true then
  3. print(“Click”)
  4. other things, like animation and Coins grant
  5. debounce to false
  6. debounce to true

Hope that helps.