Button spam issue

Currently I have a button for redeeming but the players are able to somehow spam it and redeem the code 80 or less time

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CodeEvent = ReplicatedStorage.Codebase.RemoteEvents.Human.Shop:WaitForChild("Codes")

local textBox = script.Parent:WaitForChild("CodeInput")
local submitButton = script.Parent:WaitForChild("SubmitButton")
local messageLabel = script.Parent:WaitForChild("MessageLabel")
local redeeming = false

submitButton.MouseButton1Click:Connect(function()
	if redeeming then 
		return 
	end

	redeeming = true 
	local code = textBox.Text
	if code ~= "" then
		CodeEvent:FireServer(code)  
		submitButton.Text = "Redeeming..."  
		submitButton.BackgroundColor3 = Color3.fromRGB(200, 200, 200)  
		submitButton.Active = false  
	else
		messageLabel.Text = "Please enter a code!"  
	end

	wait(10) 
	redeeming = false
	submitButton.Text = "Submit"  
	submitButton.BackgroundColor3 = Color3.fromRGB(255, 255, 255)  
	submitButton.Active = true  
end)


script.Parent.TextButton.MouseButton1Click:Connect(function()
	local frameVisible = script.Parent.Frame.Visible
	script.Parent.Frame.Visible = not frameVisible
	script.Parent.MessageLabel.Visible = not frameVisible
	script.Parent.CodeInput.Visible = not frameVisible
	script.Parent.SubmitButton.Visible = not frameVisible
end)


CodeEvent.OnClientEvent:Connect(function(text)
	messageLabel.Text = text
end)

If you know anything wrong or a fix please let me know.

In the first MouseButton1Click event, should it run the code if redeeming is false? it seems it runs it no matter what. This should fix it i believe

submitButton.MouseButton1Click:Connect(function()
	if redeeming then 
		return 
        else
              redeeming = true 
	local code = textBox.Text
	if code ~= "" then
		CodeEvent:FireServer(code)  
		submitButton.Text = "Redeeming..."  
		submitButton.BackgroundColor3 = Color3.fromRGB(200, 200, 200)  
		submitButton.Active = false  
	else
		messageLabel.Text = "Please enter a code!"  
	end

	wait(10) 
	redeeming = false
	submitButton.Text = "Submit"  
	submitButton.BackgroundColor3 = Color3.fromRGB(255, 255, 255)  
	submitButton.Active = true  
	end

	
end)

If I’m not mistaken, all you needed was an else statement to run the code if redeeming wasn’t true.

His code was fine. It’s called a guard clause, which is an inversion tactic on conditionals to reduce indentation. This practice is a component of the never-nesting principle, which you can learn more about through this fantastic video.

1 Like

Shouldn’t the redeeming = true part be under the “if code ~= “” then” statement? I mean, if you don’t enter a code, you should be able to redeem the code in less than 10 seconds.

Also, check to ensure players can’t redeem the same code repeatedly.

You’re correct, but that would require him to reformat the latter half of his code into the else clause. He should continue to use the inversion tatic and convert his code ~= "" conditional into a guard clause:

-- This will reject the submission if, from start to finish, the text is solely a variable degree of whitespace.
if string.find(code, "^%s+$") then
    return
end

See string patterns for more information

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CodeEvent = ReplicatedStorage.Codebase.RemoteEvents.Human.Shop:WaitForChild("Codes")

local textBox = script.Parent:WaitForChild("CodeInput")
local submitButton = script.Parent:WaitForChild("SubmitButton")
local messageLabel = script.Parent:WaitForChild("MessageLabel")
local redeeming = false

submitButton.MouseButton1Click:Connect(function()
	if redeeming or textBox.Text == "" then 
		return 
	end
	
	redeeming = true
	CodeEvent:FireServer(textBox.Text)
	submitButton.Text = "Redeeming..."
	submitButton.BackgroundColor3 = Color3.fromRGB(200, 200, 200)
	submitButton.Active = false

	task.wait(2) -- should be good enough

	redeeming = false
	submitButton.Text = "Submit"
	submitButton.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	submitButton.Active = true
end)

script.Parent.TextButton.MouseButton1Click:Connect(function()
	local frameVisible = script.Parent.Frame.Visible
	script.Parent.Frame.Visible = not frameVisible
	script.Parent.MessageLabel.Visible = not frameVisible
	script.Parent.CodeInput.Visible = not frameVisible
	script.Parent.SubmitButton.Visible = not frameVisible
end)

CodeEvent.OnClientEvent:Connect(function(text)
	messageLabel.Text = text
end)
1 Like

Anyways, I don’t see any abnormalities in this code. Send your server-sided logic. On a side-note, you should use a RemoteFunction to implement a code redemption system, as this can simplify your client code in how you receive the server’s input on the matter. This should invariably control the amount of time it takes to redeem a code as RemoteFunction:InvokeServer is yielding—you don’t need to force clients to wait 10 seconds to redeem another code. False wait times are never good for user experience.

For some reason the thing I marked as solution worked, maybe it was a fluke on my end.

Interesting. Anyways, I encourage you to employ some of the other suggestions made throughout this post

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.