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

I am having trouble with datastore. anything to say? I don't know what to do i tried everything

Asked by 5 years ago

I watched tons of datastore videos and see NOTHING wrong with my code

local DSService = game:GetService("DataStoreService"):GetDataStore("Currency")

game:GetService("Players").PlayerAdded:connect(function(plr)
    local leader = Instance.new("Folder",plr)
    leader.Name = "leaderstats"

    local Credits = Instance.new("IntValue",leader)
    Credits.Name = "Credits"

    local Diamonds = Instance.new("IntValue",leader)
    Diamonds.Name = "Diamonds"

    local key = "player-"..plr.UserId

    local savedData = DSService:GetAsync(key)

    if savedData then
      Credits.Value = savedData[1]
      Diamonds.Value = savedData[2]
    else
      local values = {Credits.Value, Diamonds.Value}
      DSService:SetAsync(key,values)
    end
end)

game:GetService("Players").PlayerRemoving:connect(function(plr)
    local ValuesToSave = {plr.leaderstats.Credits.Value, plr.leaderstats.Diamonds.Value}
    DSService:SetAsync(plr.UserId, ValuesToSave)
end)

while wait(300) do
    for i,v in pairs(game:GetService("Players"):GetChildren()) do
        local key = v.UserId
        DSService:SetAsync(key,v.leaderstats.Credits.Value,v.leaderstats.Diamonds.Value)
    end
end
0
Aren't you supposed to parent those to the player? krustatiion -3 — 5y

1 answer

Log in to vote
0
Answered by
SCP774 191
5 years ago
Edited 5 years ago

First, the GetAsync part is really messed up. The arguments are completely messed up. The correct arguments are:

GetAsync(plr.UserId .. "-NameOfTheScopeHere")

Also, do not use variable to replace the plr.UserId, it'll mess it up. You should use:

local savedData = DSService:GetAsync(plr.UserId .. "-WhateverNameYouWant")

Second, the SetAsync are also really messed up. Once again, the arguments are wrong. The correct arguments should be:

SetAsync(plr.UserId.. "-TheNameShouldBeSameWithTheUpperOneOrDataWillNotLoad", ValueToSave)

In that way your data saving problem should be gone forever.

A little tip: Consider using UpdateAsync, because if you use too much SetAsync, the DataStoreService will overwhelm. SetAsync and UpdateAsync have a different budget. They don't share the same budget. In that way, it's less likely to have data loss reports.

0
can you put the complete code on the page so I can see for myself because it still doesn't work jakebball2014 84 — 5y
Ad

Answer this question