Codes system using Datastore2

I’m back with another problem, this time it’s the twitter codes, they’re buggy, they don’t give the levels, money and points to the player once the code is redeemed.

local datastore2 = require(script.Parent.DataStore2)
local codestable = {}
local MainKey = "DataStoreKey"
local proceed = false

local function SetDataTable()
	local DefaultData = {
		Values = {
		["Coins"] = 0,
		["Gems"] = 0,
		["Experience"] = 0,
		["Levels"] = 1,
		["Points"] = 0,
		["Defense"] = 1,
		["Speed"] = 16,
		["Multiplier"] = 1,
		["Rebirths"] = 0,
		["Maps"] = 1,
		},
		
		Achievements = {
		["Alpha"] = 0,
		["Level_10"] = 0,
		["Level_25"] = 0,
		["Level_50"] = 0,
		["Level_100"] = 0,
		}
	}
	return DefaultData
end

game.ReplicatedStorage.Remotes.Code.OnServerEvent:Connect(function(plr, code)
	local codes = datastore2("Codes", plr)
	local userdata = datastore2(MainKey, plr):Get(SetDataTable())
	local Values = datastore2("Values", plr)
	codestable = {}
	for i, v in pairs(codes:Get({})) do
		table.insert(codestable, #codestable + 1, v)
	end
	for i, v in pairs(codes:Get({})) do
		if v ~= code then
			proceed = true
		else
			proceed = false
		end
	end
	if proceed == true then
	proceed = false
	if code == "Alpha" then
		game.ReplicatedStorage.Remotes.Code:FireClient(plr, true)
		table.insert(codestable, #codestable + 1, code)
		userdata.Values.Coins = userdata.Values.Coins + 550
		userdata.Values.Levels = userdata.Values.Levels + 1
		userdata.Values.Points = userdata.Values.Points + 1
	else
	end
	else
	game.ReplicatedStorage.Remotes.Code:FireClient(plr, false)
	end
	Values:Set(userdata.Values)
	codes:Set(codestable)
end)

the money is given at the lines:

userdata.Values.Coins = userdata.Values.Coins + 550
userdata.Values.Levels = userdata.Values.Levels + 1
userdata.Values.Points = userdata.Values.Points + 1

So, your script is messy. In your codes datastore you should only store codes that have been redeemed, example:

local CodesModule = require(place.CodesModule) --// Module containing code info.

local InputtedCode = "whatever"

if not InputtedCode[CodesModule] then
    return -- // checks to see if the code exists.
end

if InputtedCode[PlayerCodeData] == true then
    -- // already redeemed code
else
    PlayerCodeData[InputtedCode] = true
end

I would store the codes in the module like this:

return {
    ["Code"] = function(Player)
        -- // add the thing you want here, example: Player.Coins.Value = 100
   end)
}
local CodesModule = require(CodesModuleLocation)

game:GetService("ReplicatedStorage").Remotes.Code.OnServerEvent:Connect(function(Player, Code)
    local PlayerData = DS2("Codes", Player)
    if CodesModule[tostring(Code)] then
        -- // Code Exists :D
        local RedeemedCodes = PlayerData:Get() -- // assuming this returns a table of items, havent used ds2 in a while lol
        if RedeemedCodes[tostring(Code)] == nil then
            -- // Player doesn't have the code in their datastore, meaning it's unredeemed.
            PlayerData:Set(--[[ // this is where you add in the code with the true value, not sure how it works]])
            CodesModule[tostring(Code)](Player) -- // runs the function that we put into the module
        end
    end
end)

Let me know if you have any issues, I haven’t used DataStore2 in a while.

Another way you can store codes is via HTTPService and Pastebin. This will allow you to update your codes across all live servers and it’s pretty easy to do. I wont go too much into detail but you can create a paste containing your codes in JSON format. This is how I setup mine(You can do a lot more with them)

{
    "CODE1": ["Award"],
    "CODE2": ["Award"],
    "CODE3": ["Award"],
    "CODE4": ["Award"],
    "CODE5": ["Award"]
}

Then, in a module script, you can set up different functions to redeem the code, check if the player input a valid code, and other cool things. To retrieve the codetable you could do something like

local codeTable
local success, errorMessage = pcall(function()
    local response = HttpService:GetAsync("Pastebin RAW Url")
    codeTable = HttpService:JSONDecode(response)
end)

That is just an example but you can look into it more it’s pretty cool :slight_smile: You can still use Datastores but I just thought I give another way to try it if you were not having the best of luck doing it through Datastores.

Being honest, i’m completely confused about pcall functions and http services so i have no clue about that.

1 Like

Nevermind it, it was just a simple mistake i made. it’s working now and it’s fixed