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

[SOLVED] The datastore saves but it still returns nil when i try to load it?

Asked by 2 years ago
Edited by Leamir 2 years ago

This question has been solved by the original poster.
local Players = game.Players
local Datastore = game:GetService("DataStoreService")
local DS = Datastore:GetDataStore("DS")

function LoadFunc(Key)

    local s, e = pcall(function()

        local Data = DS:GetAsync(Key)

        print("Got Data")

        return Data

    end)

    if s then

        print("Succesfully Got Data")

    else

        warn(e)

    end

end

function SaveFunc(WhichData, Key)

    local s, e = pcall(function()

        DS:SetAsync(Key, WhichData)

    end)

    if s then

        print("Saved")

    else

        warn(e)

    end

end

Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local Level = Instance.new("NumberValue")
    Level.Value = 1
    Level.Name = "Level"
    Level.Parent = leaderstats

    local LoadedLevel = LoadFunc("Key-Level"..plr.UserId)

    if LoadedLevel then
        print("Not nil")
        Level.Value = LoadedLevel
        print("Set Level")

    end

end)

game.Players.PlayerRemoving:Connect(function(plr)

    SaveFunc(plr:WaitForChild("leaderstats"):WaitForChild("Level").Value, "Key-Level"..plr.UserId)

end)

So I tried this and it still saves but it doesn't load, it doesn't print "Not nil" or "Set Level". I tried accessing the datastore using Data Editor V2 and it worked, it showed the value which was 2 and it didn't have any extra value, just 2 like intended. But when I load into the game, the Value of level isn't two, rather, it is one meaning it didn't set the value at all. I am suspecting that its because GetData() is returning nil but I do not know why

0
Update: I put else warn("is nil") and it said that, so GetAsync IS returning nil sonichfan 62 — 2y
0
Another update: i put print(Data) in the pcall and it printed 1, which is weird sonichfan 62 — 2y
0
Another update: i put print(Data) in the pcall and it printed 1, which is weird sonichfan 62 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Fixed it! It turns out it was not returning anything, instead I had to make the Data variable outside of the call and then set the data to the Data variable and then check if its nil, if not it returns the data! It works now.

Ad

Answer this question