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

Data Store isn't working. Can someone please help me fix this problem? I can't find a solution.

Asked by 3 years ago
Edited 3 years ago

(found a solution)

For some reason my Data Store script isn't working. I have checked the API Services and HTTP Requests boxes. Also the data store doesn't work not only in the studio but also in the game itself. There are no errors found aswell Please help!

Here is the script:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrkey = "id_"..plr.userId
    local save1 = plr.leaderstats.Bucks
    local save2 = plr.leaderstats.Character
    local save3 = plr.leaderstats.CA

    local GetSaved = ds:GetAsync(plrkey)
    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
        save3.Value = GetSaved[3]
    else
        local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
        ds:GetAsync(plrkey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Bucks.Value, plr.leaderstats.Character.Value, plr.leaderstats.CA.Value})
end)

3 answers

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
3 years ago
Edited 3 years ago

Hello!

In line 16, do you mean :SetAsync?

ds:GetAsync(plrkey, NumbersForSaving)

Also, I'd recommend for you to specify where the values are located in the table, example:

local foobar = {
    [1] = "foo",
    [2] = "bar"
}

Here and here are developer references about datastores. Check them out sometime, to see if you can find your problem.

If you have any questions, ask them below!

I hope this helps!

0
It still doesnt work... One of my values is a string value. Maybe thats why it doesn't work? evil_zebra 4 — 3y
0
So, you're telling me you're trying to save something that isnt a stringvalue nor numbervalue? RAFA1608 543 — 3y
0
What is it, then? RAFA1608 543 — 3y
0
Intvalue and a string value evil_zebra 4 — 3y
View all comments (8 more)
0
It should save if it isnt a object (for example, a model). I dont know what is wrong. RAFA1608 543 — 3y
0
Does it give any errors? RAFA1608 543 — 3y
0
no. thats why i don't know what's happening evil_zebra 4 — 3y
0
maybe theres something else blocking the data from saving but i cant seem to find anything evil_zebra 4 — 3y
0
Have you tried changing player.userId to player.UserId? RAFA1608 543 — 3y
0
yes evil_zebra 4 — 3y
0
Hmm, I have no idea what could be wrong in your script. Sorry. RAFA1608 543 — 3y
0
I checked and only one of the values didn't save. Thank you! Edit: The value didn't save because i did something else wrong. evil_zebra 4 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Change

plr.userId

to

plr.UserId

Also

    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
        save3.Value = GetSaved[3]
    else
        local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
        ds:GetAsync(plrkey, NumbersForSaving)
    end

to

    if GetSaved then
        save1.Value = GetSaved["Bucks"]
        save2.Value = GetSaved["Character"]
        save3.Value = GetSaved["CA"]
    else
        local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
        ds:GetAsync(plrkey, NumbersForSaving)
    end
0
it printed this error: ServerScriptService.SaveData:12: invalid argument #3 (string expected, got nil) evil_zebra 4 — 3y
0
Don't change the last part only plr.userId to plr.UserId SpectacularPavlos 86 — 3y
0
It still doesn't work but now it doesn't print any errors. Maybe theres something wrong with my game? evil_zebra 4 — 3y
0
Yeah there's something wrong with your game SpectacularPavlos 86 — 3y
Log in to vote
0
Answered by 3 years ago

just put the plr parameter to player.

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
        wait()
        local plrkey = "id_"..player.userId
        local save1 = player.leaderstats.Bucks
        local save2 = player.leaderstats.Character
        local save3 = player.leaderstats.CA

        local GetSaved = ds:GetAsync(plrkey)
        if GetSaved then
            save1.Value = GetSaved[1]           
        save2.Value = GetSaved[2]
            save3.Value = GetSaved[3]
        else
            local NumbersForSaving = {save1.Value,save2.Value,save3.Value}
            ds:GetAsync(plrkey, NumbersForSaving)
        end
    end)
game.Players.PlayerRemoving:Connect(function(player)
        ds:SetAsync("id_"..player.userId, {player.leaderstats.Bucks.Value,              player.leaderstats.Character.Value, player.leaderstats.CA.Value})
end)
0
it didn't work evil_zebra 4 — 3y
0
oh... I guess you should do it on client? you should change the a code a little bit too. Finty_james 269 — 3y
0
what should i change? evil_zebra 4 — 3y
0
I think instead of using a event to get the player you do game.Players.LocalPlayer Finty_james 269 — 3y

Answer this question