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

My DataStore isn't working and has no error, can anyone help me fix this?

Asked by
imaski4 42
6 years ago

Can you guys tell me what the problem is?

ds = game:GetService("DataStoreService"):GetDataStore("RM")

game.Players.PlayerAdded:connect(function(player)
    local key = "id-" .. player.userId
    local gs = ds:GetAsync(key)
    tbl = {}
    local gui = player.PlayerGui:WaitForChild("Inventory")
    local frame = gui:WaitForChild("Inventry")
    local list1 = frame:WaitForChild("list"):WaitForChild("1")
    table.insert(tbl, list1.Image.Texture.Value)
    table.insert(tbl, list1.Item.Text)
    table.insert(tbl, list1.Qty.qty.Value)
    -- I have 3 table.inserts only because it gave me an error when I put all these in one ok
    if gs then
        tbl[1] = gs[1]
        tbl[2] = gs[2]
        tbl[3] = gs[3]
    else
        ds:SetAsync(key, tbl)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local key = "id-" .. player.userId
    ds:SetAsync(key, tbl)
end)

2 answers

Log in to vote
0
Answered by 6 years ago

The problem is you are setting the table to the datastore if the datastore exists then setting the datastore to the table when the player leaves (setting it to itself). This is what you should do.

ds = game:GetService("DataStoreService"):GetDataStore("RM")

game.Players.PlayerAdded:connect(function(player)
    local key = "id-" .. player.userId
    local gs = ds:GetAsync(key)
    local gui = player.PlayerGui:WaitForChild("Inventory")
    local frame = gui:WaitForChild("Inventry")
    local list1 = frame:WaitForChild("list"):WaitForChild("1")
    if gs then
        list1.Image.Texture.Value = gs[1]
        list1.Item.Text = gs[2]
        list1.Qty.qty.Value = gs[3]
    else
        local tbl = {}
        table.insert(tbl, list1.Image.Texture.Value)
        table.insert(tbl, list1.Item.Text)
        table.insert(tbl, list1.Qty.qty.Value)
        ds:SetAsync(key, tbl)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local key = "id-" .. player.userId
    local gui = player.PlayerGui:WaitForChild("Inventory")
    local frame = gui:WaitForChild("Inventry")
    local list1 = frame:WaitForChild("list"):WaitForChild("1")
    local tbl = {}
    table.insert(tbl, list1.Image.Texture.Value)
    table.insert(tbl, list1.Item.Text)
    table.insert(tbl, list1.Qty.qty.Value)
    ds:SetAsync(key, tbl)
end)

Now when the player joins the datastore values will be loaded into list1 and when they leave list1's value will be saved to the datastore

0
thanks imaski4 42 — 6y
0
No problem. Datastores are hard to get the hang of, but once you do they can be very useful. I use them to save stats, ranks, and ban times. justoboy13 153 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Not sure if this is your problem but in line 8: local frame = gui:WaitForChild("Inventry")

Answer this question