Redeeming codes script not working?

For some reason, my code just wont change the text box. It’s also not returning any errors.

Heres my code:

local textbox = script.Parent.TextBox
local Codes = {
	{ "RELEASE", Claimed = false},
	{"DtayDa1", Claimed = false}
}
print(Codes[1][1])
local function onButtonPress()
	
textbox.FocusLost:Connect(function(Enter)

	for i = 1,2 do
		print(i)
		print('1')
		
	if Enter then
	
		if Codes[i].Claimed == false and textbox.Text == Codes[i][1] then
			print(Codes[i][1])
				Codes[i].Claimed = true
				
				textbox.Text = "Code Redeemed!"
				wait(1)
				textbox.Text = ""
		else
			if Codes[i].Claimed == true and textbox.Text == Codes[i][1] then
						print(textbox.Text)
				textbox.Text = "Code Already Redeemed!"
				wait(1)
				textbox.Text = ""
			
			else
				if textbox.Text ~= Codes[i][1] then
					print(textbox.Text)
			textbox.Text = "Invalid Code!"
			wait(1)
			textbox.Text = ""
				end
				end
	end
	end
	end
end)
end

script.Parent.Parent.Left.Content.Codes.MouseButton1Click:Connect(onButtonPress)
script.Parent.Parent.Left.Content.Codes.TouchTap:Connect(onButtonPress)

Any help is very appreciated!

4 Likes

there are many more problems in this code, you are checking if there is a code on the client, so if a hacker wants to use the code infinite times, he can simply change the Claimed statement to false and use the code again, you have to create a DataStore just for codes (you probably know what I mean and how to do it)

Local script:

local textbox = script.Parent.TextBox

local CheckCode = remotePath

CheckCode.OnClientEvent:Connect(function(claimed: number)
    if claimed == 2 then
        textbox.Text = "Code Already Redeemed!"
        task.wait(1)
        textbox.Text = ""
        return
    elseif claimed == 1 then
        textbox.Text = "Code Redeemed!"
        task.wait(1)
        textbox.Text = ""
        return
    end

    textbox.Text = "Invalid Code!"
    task.wait(1)
    textbox.Text = ""
end)

local function onButtonPress()

end

textbox.FocusLost:Connect(function(Enter)
    if Enter then
        CheckCode:FireServer(textbox.Text)
    end
end)

script.Parent.Parent.Left.Content.Codes.Activated:Connect(onButtonPress)

Server script:

local CheckCode = remotePath

local Debounces = {

}

local function SaveData()
    --Some lines of code to save codes data
end

CheckCode.OnServerEvent:Connect(function(Player, code: string)
    if Debounces[Player.Name] then
        return
    end
    Debounces[Player.Name] = true

    local Codes = { --think this is the player code datastore
        RELEASE = {
            Claimed = false,
            Reward = {}
        },

        DtayDa1 = {
            Claimed = false,
            Reward = {}
        }
    }

    local Code = Codes[code]
    if Code and not Code.Claimed then
        CheckCode:FireClient(Player, 1) --1 mean the code is being claimed.
        Code.Claimed = true
        SaveData()
    elseif Code and Code.Claimed then
        CheckCode:FireClient(Player, 2) --2 mean the code is already claimed.
    else
        CheckCode:FireClient(Player, 0) --0 mean the code is invalid.
    end


    task.wait(.5)
    Debounces[Player.Name] = nil
end)

(sorry for so many edits in this code, but for every time i as reading it, i was fouding a new error and something to fix to prevent from hackers and bugs)

2 Likes

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