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

Datastore script not loading in data?

Asked by
3F1VE 257 Moderation Voter
3 years ago

Code :

local datastore = game:GetService("DataStoreService"):GetDataStore("Time")
game.Players.PlayerAdded:Connect(function(player)
    local s = Instance.new("Folder", player)
    s.Name = "leaderstats"
    local a = Instance.new("IntValue", s)
    a.Name = "Age"
    a.Value = player.AccountAge
    local b = Instance.new("IntValue", s)
    b.Name = "Time"
    local c = Instance.new("StringValue",s)
    c.Name = "FPS"
    local timee
    local s,e = pcall(function()
        timee= datastore:GetAsync(player.UserId)
    end)
    if s then
        b.Value = timee
    end

    game.Players.PlayerRemoving:Connect(function(plr)
        local timeee = plr.leaderstats.Time.Value
    local s,e = pcall(function()
        datastore:SetAsync(plr.UserId,timeee)
    end)
    if s then
            print("Time", timeee)
        else
            print("Error : ", e)
    end
    end)
end)

When I rejoin the time resets back to zero instead of what I had last time.

0
Take player removing out of player added. make it its own function. (idk if dat dos anything but it no ned be inside that) also are you testing in studio or in game BulletproofVast 1033 — 3y
0
Also, when you set or get data, doesn't it have to be tostring(player.UserId) instead of just player.UserId BulletproofVast 1033 — 3y
0
yes when accesing a datastores value the key has to be a string instead of a number TheBoys810 30 — 3y
0
This worked, consider leaving a answer so others can find out and so I can accept it. 3F1VE 257 — 3y
0
I'd recommend using datastore2 btw. This can be used to prevent dataloss and is far more effective than the roblox datastore. DocGooseYT 110 — 3y

Answer this question