How to check if a player already redeemed a code or not, and also save it (using DataStore)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to learn how to save a number/value and get that.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is i dont know how to do this.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried doing it myself, i tried looking on the devforum, and scripting helpers i didnt find anything.

My script:

local promo = game.ReplicatedStorage.remoteEvents.promocodes.promoCodeEv

local error1
local error2
local success

promo.OnServerEvent:Connect(function(player, input)
	if input.Text == "HELLO" then
		local thisplr = game.Players:findFirstChild(player.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local cash = stats:findFirstChild("Cash")
				local coin = stats:findFirstChild("Coins")
				if (cash~=nil) then
					cash.Value = cash.Value + 5000
					coin.Value = coin.Value + 5000
					success:FireClient(player)
				end
			else
				error1:FireClient(player)
			end
		end
	elseif input.Text == "WOW" then
		local thisplr = game.Players:findFirstChild(player.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local cash = stats:findFirstChild("Cash")
				local coin = stats:findFirstChild("Coins")
				if (cash~=nil) then
					cash.Value = cash.Value + 3000
					coin.Value = coin.Value + 3000
					success:FireClient(player)
				end
			else
				error1:FireClient(player)
			end
		end
	else
		error2:FireClient(player)
	end
end)

the remote events are empty, but i will add them.

You can detect when the code button is pressed or the code is sent to the server, have the server check a “codes redeemed” data store. If it’s not in there then give the player the rewards and add it to there

-- local script
local text = -- texbox or something here
local button = -- button to redeem the code
local remote = -- remote event

button.MouseButton1Click:Connect(function()
   remote:FireServer(text.Text) -- send the server an event
end)
-- server script
local remote = -- remot event here

local valid_codes = { -- table of the codes that are valid (to prevent exploiters)
  ["code 0"] = function(player)
      -- do stuff
  end 
} 

local function getDataStore(name: string, key: string, scope: string?) : { [string]: boolean } | nil
    -- this function would be to receive the data store and returns the values
    -- example: { ["code 0"] = true } or nil
end

remote.OnServerEvent:Connect(function(player, code) -- player who sent the request is automatically passed, code is the code
    local codeStore = getDataStore("data store name", "data store key", "scope (if you used one)")
   
    if codeStore and valid_codes[code] then
        if codeStore[code] then
          -- code already redeemed
        else
          -- code unredeemed
          valid_codes[code](player) -- do things
          -- add it to a data store
        end
    else
      -- data stores are either down, here are no values in the data store, or the code is invalid
    end
end)
6 Likes