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

how do i fix my data store script? that has issue loading after saving for first time

Asked by 5 years ago
Edited 5 years ago

all problems will be listed below open the script and find NOTE!]

local Module = require(game.ReplicatedStorage.ToolsM)
Tools = Module.SimpleNames

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore = DataStoreService:GetDataStore("101CandySimulatorGameStoreDevAlpha")
local DataTable = {}

local AutosaveInterval = 60*5 --the 5 means 5 mins
local AutosaveTick = 0

local PlayerList = Instance.new("Folder")
PlayerList.Name = "PlayerList"
PlayerList.Parent = ReplicatedStorage

game.Players.PlayerAdded:Connect(function(Player)
    -- Getting the key via userid.
    local PlayerKey = Player.UserId

    -- Creating a folder for all the data.
    local PlayerFolder = Instance.new("Folder")
    PlayerFolder.Name = Player.Name
    PlayerFolder.Parent = PlayerList

    -- Checking if they have data.
    local PlayerData = DataStore:GetAsync(PlayerKey)
    if not PlayerData then
        -- If they don't, we create data.
        DataTable[Player] = {
            Level = 1,
            currentEXP = 0,
            Cash = 0,
            AlphaPlayer = 1, --Disable on full version
            CandyCorn = true --[NOTE!]sets candycorn to true
        }
        for i = 1, #Tools do
    print (Tools[i])
            table.insert(DataTable[Player],Tools[i])
            DataTable[Player][Tools[i]] = false
        end
        print (DataTable[Player]["CandyCorn"])
        DataStore:SetAsync(PlayerKey, DataTable[Player])
        wait(2.5)
        Player:Kick("New Data Has Been Made Please Rejoin")
    else
        -- If they do, we set the data.
        DataTable[Player] = PlayerData
    end

    -- Creating variables so other scripts can acess the data.
    local RealLevel = Instance.new("NumberValue")
    RealLevel.Name = "RealLevel"
    RealLevel.Value = DataTable[Player]["Level"]
    RealLevel.Parent = PlayerFolder

    local RealEXP = Instance.new("NumberValue")
    RealEXP.Name = "RealEXP"
    RealEXP.Value = DataTable[Player]["currentEXP"]
    RealEXP.Parent = PlayerFolder

    local MaxEXP = Instance.new("NumberValue")
    MaxEXP.Name = "MaxEXP"
    MaxEXP.Value = RealLevel.Value * 10 + 70
    MaxEXP.Parent = PlayerFolder

    local RealCoins = Instance.new("NumberValue")
    RealCoins.Name = "RealCoins"
    RealCoins.Value = DataTable[Player]["Coins"]
    RealCoins.Parent = PlayerFolder

    --TESTING!!!
    wait(1)
    local Config = Instance.new ("Configuration")
    Config.Name = "ToolsOwned"
    Config.Parent = Player

    local a = Instance.new ("Configuration")
    a.Name = "Hidden"
    a.Parent = Player:WaitForChild("Backpack")

    a = Instance.new ("StringValue")
    a.Name = "CurrentTool"
    a.Parent = Config.Parent

    for i = 1, #Tools do
        a = Instance.new ("BoolValue")--[NOTE!] all of this prints the name in the thing and then nil this is what i am trying to fix
        a.Name = Tools[i]
        a.Parent = Config
        print (Tools[i])
        print (DataTable[Player][Tools[i]])
        a.Value = DataTable[Player][Tools[i]]
    end
    --TESTING!!!

    while wait() do
        MaxEXP.Value = RealLevel.Value * 10 + 70

        -- Leveling part of the loop.
        if RealEXP.Value >= MaxEXP.Value then
            RealEXP.Value = 0
            RealLevel.Value = RealLevel.Value + 1
            MaxEXP.Value = RealLevel.Value * 10 + 70
        end

        -- Doing the autosave for data.
        wait(1)
        AutosaveTick = AutosaveTick + 1
        if AutosaveTick >= AutosaveInterval then
            AutosaveTick = 0

            -- Updating the data of the player.
            DataTable[Player]["Level"] = PlayerFolder:FindFirstChild("RealLevel").Value
            DataTable[Player]["currentEXP"] = PlayerFolder:FindFirstChild("RealEXP").Value
            DataTable[Player]["Coins"] = PlayerFolder:FindFirstChild("RealCoins").Value

            -- Saving the data of the player.
            DataStore:SetAsync(PlayerKey, DataTable[Player])
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    -- Getting the key via userid.
    local PlayerKey = Player.UserId

    -- Finding the data of the player.
    local PlayerFolder = PlayerList:FindFirstChild(Player.Name)
    if PlayerFolder then
        -- Updating the data of the player.
        DataTable[Player]["Level"] = PlayerFolder:FindFirstChild("RealLevel").Value
        DataTable[Player]["currentEXP"] = PlayerFolder:FindFirstChild("RealEXP").Value
        DataTable[Player]["Coins"] = PlayerFolder:FindFirstChild("RealCoins").Value

        -- Saving the data of the player.
        DataStore:SetAsync(PlayerKey, DataTable[Player])

        -- Removing the folder, as we have saved data.
        PlayerFolder:Remove()
    end
end)

Answer this question