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

DataStore is not storing values?

Asked by
Qorm 100
9 years ago

I'm trying to store the player's level and xp for my RPG in DataStorage but something beyond my comprehension is happening

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player.Character
    --#load#--
    local folder=Instance.new("Folder",player) folder.Name="Storage"
    local level=Instance.new("IntValue",folder) level.Name="level" level.Value=1
    local xp=Instance.new("IntValue",folder) xp.Name="xp" xp.Value=0
    --==--

    --#datastore#--
    local DataStore=game:getService("DataStoreService"):GetDataStore("StatStore")
    local key=("player-"..player.userId)
    local savestats=DataStore:GetAsync(key)
    if savestats then
        print("Found "..player.Name.."'s save file! Loading...")
        level.Value=savestats[1]
        xp.Value=savestats[2]
        print("Stats successfuly loaded!")
    else
        print("Making a new save for "..player.Name)
        local statstosave={level.Value,xp.Value}
        DataStore:GetAsync(key,statstosave)
        print("Made "..statstosave[#statstosave].." things to save in DataStorage for "..player.Name)
    end
    --==--

i know, i know, i dont use spacing in my coding- sorry about that. anyway, it says: >"Making a new save for Qorm" then >"Made 0 new things to save in DataStorage for Qorm"

help would be appreciated!!

2 answers

Log in to vote
1
Answered by 9 years ago

I would recommend using "Data persistence" instead.

Your first part for the Folder and Values is good. But saving it is different.

game.Players.PlayerAdded:connect(function(player)
     repeat wait() until player.Character
    local folder=Instance.new("Folder",player) folder.Name="Storage"
    local level=Instance.new("IntValue",folder) level.Name="level" level.Value=1
    local xp=Instance.new("IntValue",folder) xp.Name="xp" xp.Value=0
    xp.Value = player:LoadNumber("xp")
    level.Value = player:LoadNumber("level")
end)

game.Players.PlayerRemoving:connect(function(player)
    if player:WaitForDataReady() then
        player:SaveNumber("xp", ***Where ever you find the XP***)
    player:SaveNumber("level", ***Where ever you find the Level***)
    end   
end)

Source: Click

0
Data persistence isn't reliable -.- FutureWebsiteOwner 270 — 9y
0
It works for me. TypicalModerator 110 — 9y
Ad
Log in to vote
1
Answered by 9 years ago
game.Players.PlayerAdded:connect(function(player)
    --player.CharacterAdded function instead but it isnt necessary in this case
    --#load#--
    local folder=Instance.new("Folder",player) folder.Name="Storage"
    local level=Instance.new("IntValue",folder) level.Name="level" level.Value=1
    local xp=Instance.new("IntValue",folder) xp.Name="xp" xp.Value=0
    --==--

    --#datastore#--
    local DataStore=game:getService("DataStoreService"):GetDataStore("StatStore")
    local key=("player-"..player.userId)
    local savestats=DataStore:GetAsync(key)
    if savestats then
        print("Found "..player.Name.."'s save file! Loading...")
        level.Value=savestats[1]
        xp.Value=savestats[2]
        print("Stats successfuly loaded!")
    else
        print("Making a new save for "..player.Name)
        local statstosave={level.Value,xp.Value}
        DataStore:SetAsync(key,statstosave) --SetAsync, not GetAsync
        print("Made "..#statstosave.." things to save in DataStorage for "..player.Name)
    end
    --==--

Good to see you're learning datastores!

Answer this question