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

Can anyone help fix and simplify this datastore script? [closed]

Asked by 7 years ago
local module = {}
local Storage = game:GetService('DataStoreService'):GetDataStore("StatHolder")


game.Players.PlayerAdded:connect(function(plr)
    local Lvl = Instance.new("NumberValue", plr)
    Lvl.Name = "Lvl"
    Lvl.Value = 1

    local LvlTime = Instance.new("IntValue", plr)
    LvlTime.Name = "LvlTime"

    local Lvl1 = Instance.new("StringValue", plr.LvlTime)
    Lvl1.Name = "Lvl1Time"
    Lvl1.Value = 00.0

    local Lvl2 = Instance.new("StringValue", plr.LvlTime)
    Lvl2.Name = "Lvl2Time"
    Lvl2.Value = 00.0

    local Lvl3 = Instance.new("StringValue", plr.LvlTime)
    Lvl3.Name = "Lvl3Time"
    Lvl3.Value = 00.0

    local Lvl4 = Instance.new("StringValue", plr.LvlTime)
    Lvl4.Name = "Lvl4Time"
    Lvl4.Value = 00.0

    local Lvl5 = Instance.new("StringValue", plr.LvlTime)
    Lvl5.Name = "Lvl5Time"
    Lvl5.Value = 00.0

    local Lvl6 = Instance.new("StringValue", plr.LvlTime)
    Lvl6.Name = "Lvl6Time"
    Lvl6.Value = 00.0

    local Lvl7 = Instance.new("StringValue", plr.LvlTime)
    Lvl7.Name = "Lvl7Time"
    Lvl7.Value = 00.0

    local Lvl8 = Instance.new("StringValue", plr.LvlTime)
    Lvl8.Name = "Lvl8Time"
    Lvl8.Value = 00.0

    local Lvl9 = Instance.new("StringValue", plr.LvlTime)
    Lvl9.Name = "Lvl9Time"
    Lvl9.Value = 00.0

    local Lvl10 = Instance.new("StringValue", plr.LvlTime)
    Lvl10.Name = "Lvl10Time"
    Lvl10.Value = 00.0

    local Lvl11 = Instance.new("StringValue", plr.LvlTime)
    Lvl11.Name = "Lvl11Time"
    Lvl11.Value = 00.0

    local Lvl12 = Instance.new("StringValue", plr.LvlTime)
    Lvl12.Name = "Lvl12Time"
    Lvl12.Value = 00.0

    local SavedLvl = Storage:GetAsync(plr.userId .. "-Lvl")
    if SavedLvl ~= nil then
        Lvl.Value = SavedLvl


    local SavedLvl1Time = Storage:GetAsync(plr.userId .. "-Lvl1Time")
    if SavedLvl1Time ~= nil then
        Lvl.Value = SavedLvl1Time

    local SavedLvl2Time = Storage:GetAsync(plr.userId .. "-Lvl2Time")
    if SavedLvl2Time ~= nil then
        Lvl.Value = SavedLvl2Time

    local SavedLvl3Time = Storage:GetAsync(plr.userId .. "-Lvl3Time")
    if SavedLvl3Time ~= nil then
        Lvl.Value = SavedLvl3Time

    local SavedLvl4Time = Storage:GetAsync(plr.userId .. "-Lvl4Time")
    if SavedLvl4Time ~= nil then
        Lvl.Value = SavedLvl4Time

    local SavedLvl5Time = Storage:GetAsync(plr.userId .. "-Lvl5Time")
    if SavedLvl5Time ~= nil then
        Lvl.Value = SavedLvl5Time

    local SavedLvl6Time = Storage:GetAsync(plr.userId .. "-Lvl6Time")
    if SavedLvl6Time ~= nil then
        Lvl.Value = SavedLvl6Time

    local SavedLvl7Time = Storage:GetAsync(plr.userId .. "-Lvl7Time")
    if SavedLvl7Time ~= nil then
        Lvl.Value = SavedLvl7Time

    local SavedLvl8Time = Storage:GetAsync(plr.userId .. "-Lvl8Time")
    if SavedLvl8Time ~= nil then
        Lvl.Value = SavedLvl8Time

    local SavedLvl9Time = Storage:GetAsync(plr.userId .. "-Lvl9Time")
    if SavedLvl9Time ~= nil then
        Lvl.Value = SavedLvl9Time

    local SavedLvl10Time = Storage:GetAsync(plr.userId .. "-Lvl10Time")
    if SavedLvl10Time ~= nil then
        Lvl.Value = SavedLvl10Time

    local SavedLvl11Time = Storage:GetAsync(plr.userId .. "-Lvl11Time")
    if SavedLvl11Time ~= nil then
        Lvl.Value = SavedLvl11Time

    local SavedLvl12Time = Storage:GetAsync(plr.userId .. "-Lvl12Time")
    if SavedLvl12Time ~= nil then
        Lvl.Value = SavedLvl12Time

    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
end)


game.Players.PlayerRemoving:connect(function(plr)
    local id = plr.userId
    local StatsValue = plr.Lvl.Value
    Storage:SetAsync(id.. "-Lvl", StatsValue)

    local id = plr.userId
    local Stats1Value = plr.LvlTime.Lvl1Time.Value
    Storage:SetAsync(id.. "-Lvl1Time", Stats1Value)

    local id = plr.userId
    local Stats2Value = plr.LvlTime.Lvl2Time.Value
    Storage:SetAsync(id.. "-Lvl2Time", Stats2Value)

    local id = plr.userId
    local Stats3Value = plr.LvlTime.Lvl3Time.Value
    Storage:SetAsync(id.. "-Lvl3Time", Stats3Value)

    local id = plr.userId
    local Stats4Value = plr.LvlTime.Lvl4Time.Value
    Storage:SetAsync(id.. "-Lvl4Time", Stats4Value)

    local id = plr.userId
    local Stats5Value = plr.LvlTime.Lvl5Time.Value
    Storage:SetAsync(id.. "-Lvl5Time", Stats5Value)

    local id = plr.userId
    local Stats6Value = plr.LvlTime.Lvl6Time.Value
    Storage:SetAsync(id.. "-Lvl6Time", Stats6Value)

    local id = plr.userId
    local Stats7Value = plr.LvlTime.Lvl7Time.Value
    Storage:SetAsync(id.. "-Lvl7Time", Stats7Value)

    local id = plr.userId
    local Stats8Value = plr.LvlTime.Lvl8Time.Value
    Storage:SetAsync(id.. "-Lvl8Time", Stats8Value)

    local id = plr.userId
    local Stats9Value = plr.LvlTime.Lvl9Time.Value
    Storage:SetAsync(id.. "-Lvl9Time", Stats9Value)

    local id = plr.userId
    local Stats10Value = plr.LvlTime.Lvl10Time.Value
    Storage:SetAsync(id.. "-Lvl10Time", Stats10Value)

    local id = plr.userId
    local Stats11Value = plr.LvlTime.Lvl11Time.Value
    Storage:SetAsync(id.. "-Lvl11Time", Stats11Value)

    local id = plr.userId
    local Stats12Value = plr.LvlTime.Lvl12Time.Value
    Storage:SetAsync(id.. "-Lvl12Time", Stats12Value)
end)


return module

It doesn't save. Please helpp

Closed as Not Constructive by EzraNehemiah_TF2 and TheHospitalDev

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

It seems like you're attempting to save each 'Level Time' stat to a separate DataStore key. This works, but isn't as efficient as it could be. Here are some fixes you could make:

  • You should save a table of all your data, and then you'll only need one key for your DataStore - as opposed to a separate key for every stat.

  • Another way to increase efficiency in your code would be to use a numerical for loop while setting/getting your stats. This way, you don't have to type out so much.

Note: It looks like you may be using a ModuleScript. You should use a regular Script in ServerScriptStorage.
local Storage = game:GetService('DataStoreService'):GetDataStore("StatHolder")

game.Players.PlayerAdded:connect(function(plr)
    local data = Storage:GetAsync(plr.UserId);
    local Lvl = Instance.new("NumberValue", plr)
    Lvl.Name = "Lvl"
    Lvl.Value = data and data.Level or 1
    local LvlTime = Instance.new("IntValue", plr)
    LvlTime.Name = "LvlTime"
    for i = 1,12 do --numerical for for efficiency
        local this = "Lvl"..i.."Time";
        local lvlTime = Instance.new("StringValue",LvlTime);
        lvlTime.Name = this
        lvlTime.Value = data and data[this] or 0;
    end
end)


game.Players.PlayerRemoving:connect(function(plr)
    local data = {}; --make a table for the stats
    data.Level = plr:FindFirstChild("Lvl").Value --save level
    for i = 1,12 do --numerical for for efficiency
        --save individual values to table
        local this = "Lvl"..i.."Time";
        data[this] = plr.LvlTime:FindFirstChild(this).Value;
    end
    Storage:SetAsync(plr.UserId,data); --save table to datastore
end)
Ad