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

how to change the value of a table thats already made?

Asked by 6 years ago

so i have this script

        DataTable[Player] = {
            Level = 1,
            currentEXP = 0,
            Cash = 0,
            AlphaPlayer = 1, --Disable on full version
        }
        for i = 1, #Tools do
            table.insert(DataTable[Player],tostring(Tools[i]))
        end
        table.insert(DataTable[Player],"CandyCorn")
        DataTable[Player].CandyCorn = true
        DataStore:SetAsync(PlayerKey, DataTable[Player])

but i cant seem to change the value of Candy Corn Before it gets saved any ideas on how to do this?

0
You can't do DataTable[Player], but you could do DataTable["Player"] or DataTable.Player User#22219 20 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

Line 1: DataTable.Player, line 8, 10, 11, 12: DataTable.Player

0
im a bit confused about what u mean by that... ForgotenR4 68 — 6y
0
Change those lines. User#22219 20 — 6y
0
??? ForgotenR4 68 — 6y
0
Change line 1 to DataTable.Player, etc User#22219 20 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
local Module = require(game.ServerStorage.Tools)
Tools = Module.SimpleNames

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

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

local AutosaveInterval = 60
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
        }
        for i = 1, #Tools do
            table.insert(DataTable[Player],tostring(Tools[i]))
        end
        DataTable[Player].CandyCorn = true
        print (DataTable["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.Backpack

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

for i = 1, #Tools do
    a = Instance.new ("BoolValue")
    a.Name = Tools[i]
    a.Parent = Config
    print (DataTable[Player][tostring(Tools[i])])
    a.Value = DataTable[Player][tostring(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