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

I want to make it so that when the power button is clicked, only one coin is awarded.

The problem is, it gives some random amount of coins although the script says
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + addmoney

FULL SCRIPT:

while true do
local T = math.random(1, 15)
wait(T)
script.Parent.Parent.Parent.Parent.Light.Script.Disabled = true
script.Parent.Parent.Parent.Parent.Light.BrickColor = BrickColor.Red()

script.Parent.MouseButton1Click:Connect(function()
local debounce = script.Parent.Parent.Parent.Parent.Light.Script.Disabled

if debounce == true then

debounce = true
			wait(.1)
		addmoney = 1
		currencyname = "Coinn"
		script.Parent.Parent.Parent.Parent.Light.Script.Disabled = true
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats")and v then
			v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + addmoney
	script.Parent.Parent.Parent.Parent.Light.Script.Disabled = false
		debounce = false
		

		end
		end
	end
end)
end


Thanks for reading!

2 Likes

Why is your event listener wrapped in a while loop?

Every T seconds, a new listener is created, so the event is firing to more and more listeners the longer the game is running.

6 Likes

Couldn’t you just make it:

v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + 1?

1 Like
local debounce = script.Parent.Parent.Parent.Parent.Light.Script.Disabled
-- just do this VV
local debounce = false

pretty sure you need to add debounce.Value=true and debounce.Value = false the way you currently have it. But I suggest local debounce = false because it’s just better that way.

you should use variables for your debounces there’s no need to create a pathway you just simply type local debounce = true then set it to false simply by typing debounce = false and debounce = true.

NOTE: Also do not add loops that wrap around functions, because you’re creating infinite functions, which means every time the loop runs it attaches a new function with the same event. This could be why you get more than 1 coin per click.

2 Likes

This is just a suggestion, but you should never trust the client to handle things like this. I’d recommend having the client detect when the player clicks and fires the server to notify it that it needs to update the client’s currency. The server should then handle debounces, and check if there isn’t a debounce. If that checks out, then the server is free to update the currency.

Here’s an example I made real quick.

--client
local debounce = false
local clickEvent = game:GetService("ReplicatedStorage"):WaitForChild("Clicked")

wait(math.random(1, 15))

script.Parent.Parent.Parent.Parent.Light.Script.Disabled = true
script.Parent.Parent.Parent.Parent.Light.BrickColor = BrickColor.Red()

script.Parent.MouseButton1Click:Connect(function()
    if not debounce then
        clickEvent:FireServer()
    end
end)
--server
local currencyName = "Coinn"

local clickEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
clickEvent.Name = "Clicked"
clickEvent.OnServerEvent:Connect(function(player)
    if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild(currencyName) then
        player.leaderstats[currencyName] = player.leaderstats[currencyName] + 1
    end
end)

Edit: I do not know if Coinn is a typo, or it’s just the name of the currency so I’m leaving it be.

3 Likes

@PhoenixRessusection If you want to index something by typing thing[name] you have to create a table for it first.

local stats = v.leaderstats:GetChildren()
local coins = stats["Coins"]
coins.Value=coins.Value+1

What i have to do with that?

@Dracolyx

Ok, @Dracolyx, but if I got rid of the loop, how would I make it so the script would work over and over again?

@Blokav I wrapped the event listener in a while loop so that the script would work over and over again, and wouldn’t just stop once the player clicks the button while the light is red.

@OptimisticSide Coinn was not a typo! I will try this script out that you sent! Thanks!

Actually this can all be re-written to not require an infinite loop at all.

local currencyname = "Coinn"
local addmoney = 1

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
		wait(math.random(1, 15))
		script.Parent.Parent.Parent.Parent.Light.Script.Disabled = true
		script.Parent.Parent.Parent.Parent.Light.BrickColor = BrickColor.Red()
		debounce = true
	end
end)

If you have the opportunity to use an event-driven system over a loop, always go with the event.

2 Likes

Wouldn’t a separate bool value instance not be necessary in this case? You could just create a variable in the code for debounce outside of the connection.

That’s exactly what this code does.

since debounce == Script.Disabled
debounce can be removed

using the changing state of the script itself as the check for coinn gain

Nevermind. I read it wrong. I think he’s made two posts about the same issue, so I was confused. Fixing a script in my game - #7 by bongusbingis

I used the variable to clear up any potential confusion. I’ll redact my earlier code sample.

Ok! If it works, make it a solution so that people know your problem has been solved!

@SS4PPHIRE Yeah, I did add on to that post, but I created a new one because that one was about a different thing on the script. Sorry about that.

1 Like

Your script worked @Blokav ! Thanks so much! Except for one thing: the light only turns red when the player clicks the button at first. I’d like to have it so that it blinks without the player clicking it at first. So then what would I do, because wouldn’t that stop the script from repeating?

I have this so far (I modified @Blokav 's script ). Now how can I make it so it is looping? (Without a while true do loop)

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)

Or should I do this a different way?