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