Making it so that a player only gets one Coinn per click

You do not need a loop wrapping the entire thing, the event listener in @Blokav’s sample will detect each and every click the user does.

1 Like

Outside of the functions you’ll want to create a DB variable which will always be the same unless changed. If it is put in a loop it’ll just create a new variable with set value like false.

You also may want to look into exploitation stuff to prevent some exploits.

1 Like

@ClanNotGuilds Yeah but the problem is that only when the player clicks the button, the script will start. I want it so that the light will turn red without the player clicking it at first.

Outside of the function and loops, change the Color of the part.

Then it should be the default color when spawned.

@Snoopy1333, like this?

local currencyname = "Coinn"
local addmoney = 1
		wait(math.random(1, 5))
		script.Parent.Parent.Parent.Parent.Light.Script.Disabled = true
		script.Parent.Parent.Parent.Parent.Light.BrickColor = BrickColor.Red()
local debounce = true
script.Parent.MouseButton1Click:Connect(function()
	if (debounce) then
		debounce = false
		script.Parent.Parent.Parent.Parent.Light.Script.Disabled = false
		for _, player in ipairs(game.Players:GetPlayers()) do
			if (player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild(currencyname))then
				player.leaderstats[currencyname].Value = player.leaderstats[currencyname].Value + addmoney
			end
		end

	end
end)

When I do that, the script only works once.

because debounce stays false
You need to add a delayed debounce = true somehow for the next click to work

You don’t really need the “()” in the if statement just “if debounce then” and after everything is completed outside of the for loop make the debounce true again. Or add a wait to it and then make it true for a slight delay.

Also when setting the debounce to false you also want everything to look like it’s clickable again so change what you need after the delay too.

@Snoopy1333, Okay, I will try it.

@ForegoingTyler12943

Aside from what others have correctly mentioned before me :



Since you’re handling UI use a Local Script, Server Scripts won’t work for GUI ,

if you have Filtering Enabled that is and aren’t recommended to be used for it either.

The reason why you’re getting a random amount of coins each time is that you add the coins to the leaderstats within the loop, so you’ll get [1 * amount of players] number of coins each time you click the gui button.

Another thing you’re doing wrong is that you are adding coins to a player’s leaderstats on the Client , do this on the Server instead through an event, so you can save the value as well.

One more thing, ipairs should have been used for faster performance while iterating due to how it works and is different from pairs, even though there was no need for a loop at all because local scripts replicate to each Client themselves.

If you want a simple script that just gives a player one coin then do it without a while loop, the additional things you’re doing (other than anything related to UI or checks) should be handled through another script, they are adding unnecessary lines of code to a simple attempt.

A local script would be used to fire the event, passing the amount :

  event = game:GetService("ReplicatedStorage").event -- the event
  Amount = 1 

  debounce = false -- debounce mechanism
  script.Parent.MouseButton1Click:Connect(function()

  if debounce then
          debounce = true
	            event:FireServer(Amount)     
                wait(5) -- Cooldown 
          debounce = false
      end
  end)

And receive the value on the Server and add the value ,
 local event = game:GetService("ReplicatedStorage").event

 event.OnServerEvent:Connect(function(player,amt)
    
       if type(amt) ~= "Number" then    
          return warn("Value passed must be a number") end

       local currency = "Coinn"
             
             if not player.leaderstats then return end -- leaderstats does not exist
             local coins = player.leaderstats[currency]

       coins.Value = coins.Value + amt

   end

In another script, handle the rest (i.e change the brick color, change different scripts’ Disabled property etc. as you were doing).

This code is a demonstration of how you would only give one player a (1) “Coinn” when they click a GUI button object on the Server.

1 Like

Thanks, @XxELECTROFUSIONxX, but where would I put these scripts?

Would I put this one in the actual textbutton

  event = game:GetService("ReplicatedStorage").event -- the event
  Amount = 1 

  debounce = false -- debounce mechanism
  script.Parent.MouseButton1Click:Connect(function()

  if debounce then
          debounce = true
	            event:FireServer(Amount)     
                wait(5) -- Cooldown 
          debounce = false
      end
  end)

and would I put this one in serverscriptservice

 local event = game:GetService("ReplicatedStorage").event

 event.OnServerEvent:Connect(function(player,amt)
    
       if type(amt) ~= "Number" then    
          return warn("Value passed must be a number") end

       local currency = "Coinn"
             
             if not player.leaderstats then return end -- leaderstats does not exist
             local coins = player.leaderstats[currency]

       coins.Value = coins.Value + amt

   end

(Sorry I’m a bit new to programming I have no idea where to put the scripts)

Yes you would but of course you’d need to set that event in Replicated Storage first
and name it “event” or you would be indexing nil with event.

You will rarely be provided complete scripts on the Forum, I merely did for demonstration.

1 Like

Alright, thanks! This helps a lot!

1 Like

@XxELECTROFUSIONxX Wait but then I’m getting this error message:

the second code block needs an ) at the end

end)

It just means what it says “A closing bracket was left out” (in other words), because I only wrote these scripts as a demonstration, on the Forum .

– Add a ) to the final end in my Server Script.

1 Like

Ok, thanks! I will try it! Hopefully all goes well!

Doing Object.Child is the same exact thing as doing Object[Child], as well as Table.Index and Table[Index]. This is because Roblox’s Instances are just tables and metatables with some handy functions.