Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it Possible to Shorten My DataStore Code?

Asked by 4 years ago

Okay, so I have a DataStore that is currently 188 lines long. And while it works, I don't believe it's efficient, so I'm wondering if there's a way to shorten it.

Right now, the reason it's so long is because I'm saving every individual value into a table and then saving that table. I was thinking, maybe I could put all the values into a folder outside the DataStore and then save the folder. If this was possible I would be able to significantly cut down on code in my DataStore. It would also allow me to add new values easier and faster than before. Even if this isn't possible, if anyone knows an alternative, I would greatly appreciate it.

This might be a pointless question though, seeing as I have no idea how other people save data and they may have over 1k lines.

This is currently my code, you can see why I have so many lines now:

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("DataStore11")

game.Players.PlayerAdded:Connect(function(player)
    game.Players:WaitForChild(player.Name)
    player:WaitForChild("leaderstats")
    player:WaitForChild("SpeedMulti")
    game.Workspace:WaitForChild(player.Name)

    wait(1)

    local Key = player.UserId

    local Leaderstats = player.leaderstats
    local Speed = player.Speed
    local Rebirths = Leaderstats.Rebirths
    local TimePlayed = player.TPValue
    local Gems = player.Gems
    local RebirthCost = player.RebirthCost
    local SpeedMulti = player.SpeedMulti
    local SpdUpgCost = player.SpdUpgCost
    local JmpUpgCost = player.JmpUpgCost
    local JumpMulti = player.JumpMulti
    local JumpPower = player.JumpPower
    local Cardio = Leaderstats.Cardio
    local CardioMulti = player.CardioMulti
    local TotalSpeed = player.TotalSpeed
    local TotalJumpPower = player.TotalJumpPower
    local TotalCardio = player.TotalCardio
    local TotalGems = player.TotalGems

    local DefaultStats = {
        4, -- 1/Speed
        0, -- 2/Rebirths
        0, -- 3/Time Played
        0, -- 4/Gems
        1000, -- 5/Rebirth Cost
        1, -- 6/Speed Multiplier
        100, -- 7/Speed Upgrade Cost
        100, -- 8/Jump Upgrade Cost
        1, -- 9/Jump Multiplier
        64, -- 10/Jump Power
        68, -- 11/Cardio
        1, -- 12/CardioMulti
        4, -- 13/TotalSpeed
        64, -- 14/TotalJumpPower
        68, -- 15/TotalCardio
        0 -- 16/TotalGems
    }

    local Stats = {
        Speed.Value, -- 1
        Rebirths.Value, -- 2
        TimePlayed.Value, -- 3
        Gems.Value, -- 4
        RebirthCost.Value, -- 5
        SpeedMulti.Value, -- 6
        SpdUpgCost.Value, -- 7
        JmpUpgCost.Value, -- 8
        JumpMulti.Value, -- 9
        JumpPower.Value, -- 10
        Cardio.Value, -- 11
        CardioMulti.Value, -- 12
        TotalSpeed.Value, -- 13
        TotalJumpPower.Value, -- 14
        TotalCardio.Value, -- 15
        TotalGems.Value -- 16
    }

    local LoadStats
    local success, err = pcall(function()
        LoadStats = Data:GetAsync(Key)
    end)

    if LoadStats == nil then
        Speed.Value = DefaultStats[1]
        Rebirths.Value = DefaultStats[2]
        TimePlayed.Value = DefaultStats[3]
        Gems.Value = DefaultStats[4]
        RebirthCost.Value = DefaultStats[5]
        SpeedMulti.Value = DefaultStats[6]
        SpdUpgCost.Value = DefaultStats[7]
        JmpUpgCost.Value = DefaultStats[8]
        JumpMulti.Value = DefaultStats[9]
        JumpPower.Value = DefaultStats[10]
        Cardio.Value = DefaultStats[11]
        CardioMulti.Value = DefaultStats[12]
        TotalSpeed.Value = DefaultStats[13]
        TotalJumpPower.Value = DefaultStats[14]
        TotalCardio.Value = DefaultStats[15]
        TotalGems.Value = DefaultStats[16]

        Data:SetAsync(Key, Stats)
        print("No data to load. Setting data...")
    else
        Speed.Value = LoadStats[1]
        Rebirths.Value = LoadStats[2]
        TimePlayed.Value = LoadStats[3]
        Gems.Value = LoadStats[4]
        RebirthCost.Value = LoadStats[5]
        SpeedMulti.Value = LoadStats[6]
        SpdUpgCost.Value = LoadStats[7]
        JmpUpgCost.Value = LoadStats[8]
        JumpMulti.Value = LoadStats[9]
        JumpPower.Value = LoadStats[10]
        Cardio.Value = LoadStats[11]
        CardioMulti.Value = LoadStats[12]
        TotalSpeed.Value = LoadStats[13]
        TotalJumpPower.Value = LoadStats[14]
        TotalCardio.Value = LoadStats[15]
        TotalGems.Value = LoadStats[16]

        print("Data loaded!")
    end

    while wait(300) do
        local success, err = pcall(function()
            Data:UpdateAsync(Key, function(oldValue)
                return Stats
            end)
        end)

        if success then
            print("Data auto saved!")
        else
            print("Auto save failed.")
            warn(err)
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = player.UserId

    local Leaderstats = player.leaderstats
    local Speed = player.Speed
    local Rebirths = Leaderstats.Rebirths
    local TimePlayed = player.TPValue
    local Gems = player.Gems
    local RebirthCost = player.RebirthCost
    local SpeedMulti = player.SpeedMulti
    local SpdUpgCost = player.SpdUpgCost
    local JmpUpgCost = player.JmpUpgCost
    local JumpMulti = player.JumpMulti
    local JumpPower = player.JumpPower
    local Cardio = Leaderstats.Cardio
    local CardioMulti = player.CardioMulti
    local TotalSpeed = player.TotalSpeed
    local TotalJumpPower = player.TotalJumpPower
    local TotalCardio = player.TotalCardio
    local TotalGems = player.TotalGems

    local Stats = {
        Speed.Value,
        Rebirths.Value,
        TimePlayed.Value,
        Gems.Value,
        RebirthCost.Value,
        SpeedMulti.Value,
        SpdUpgCost.Value,
        JmpUpgCost.Value,
        JumpMulti.Value,
        JumpPower.Value,
        Cardio.Value,
        CardioMulti.Value,
        TotalSpeed.Value,
        TotalJumpPower.Value,
        TotalCardio.Value,
        TotalGems.Value
    }

    local success, err = pcall(function()
        Data:UpdateAsync(Key, function(oldData)
            return Stats
        end)
    end)

    if success then
        print("Data saved!")
    else
        print("Data didn't save.")
        warn(err)
    end
end)

game:BindToClose(function()
    wait(10)
end)

Answer this question