How would I adjust this code system?

How would I adjust my code to allow me to add new codes at any time WITHOUT resetting data to give the player a new variable for the new code?

Currently; I’m making codes by storing them inside the users data to say that they have/not used them BUT if I wanted to add a new code I would need to reset the players data.

CODE:

> local CodeRewards = {
>     ['Release'] = {
>         Yen = 2500,
>         StatPoints = 3
>     }
> }
> 
> function Data.AddCodeStat(Player, Stat, Value)
>     local Player_Data = PlayerDataList[Player].Stats
>     
>     Player_Data[Stat] = Player_Data[Stat] + Value
>     
>     RecieveDataEvent:FireClient(Player, PlayerDataList[Player])
> end
> 
> function Data.UseCode(Player, Code)
>     local Player_Data = PlayerDataList[Player].Codes
>     if not CodeRewards[Code] then
>         return
>     end
>     
>     if Player_Data[Code] == true then
>         return
>     elseif Player_Data[Code] == false then
>         Player_Data[Code] = true
>         for i,v in pairs(CodeRewards[Code]) do
>             --i == NAME
>             --v == VALUE
>             --print(i, v)
>             Data.AddCodeStat(Player, i, v)
>         end
>     end
> end

–[[ Player’s Data ]]–

local BaseData = {Stats = {Level = 1,Experience = 0, StatPoints = 0, Strength = 1, Defence = 1}, Quests = {QuestNumber = 0, QuestGoals = 0, QuestFinalGoal = 0, QuestEnemyToKill = ""}, Codes = {['Release'] = false}}

1 Like

Fixing


These lines are just code bloat:

if Player_Data[Code] == true then
    return
elseif Player_Data[Code] == false then
    Player_Data[Code] = true
    for i,v in pairs(CodeRewards[Code]) do
        --i == NAME
        --v == VALUE
        --print(i, v)
        Data.AddCodeStat(Player, i, v)
    end
end

Use this:

if Player_Data[Code] == false then
    Player_Data[Code] = true
    for i,v in pairs(CodeRewards[Code]) do
        --i == NAME
        --v == VALUE
        --print(i, v)
        Data.AddCodeStat(Player, i, v)
    end
end

Next up: You’ll need to write some DataStore code to save the data.

Maybe a different DataStore that you can change live, perhaps it is Google Spreadsheets.