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
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.
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.
@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.
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.
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.
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)