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

Datastore won't load data?

Asked by 2 years ago

Code below won't load data, no errors, I have no idea what's going on.

local dataStoreService = game:GetService("DataStoreService")
local cubeDataStore = dataStoreService:GetDataStore("Cubes")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local cubesmade = Instance.new("IntValue",leaderstats)
    cubesmade.Name = "Cubes"
    cubesmade.Value = 0

    local playerUserId = "player_"..player.UserId
    local cubeData
    local success, errormessage = pcall(function()
        cubeData = cubeDataStore:GetAsync(playerUserId)
    end)

    if success then
        cubesmade.Value = cubeData
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "player_"..player.UserId
    local cubeValue = player.leaderstats.Cubes.Value
    local success, errormessage = pcall(function()
        cubeDataStore:SetAsync(playerUserId, cubeValue)
    end)
end)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        local playerUserId = "player_"..player.UserId
        local cubeValue = player.leaderstats.Cubes.Value
        local success, errormessage = pcall(function()
        end)
        cubeDataStore:SetAsync(playerUserId, cubeValue)
    end
end)
0
Looks fine, after you checking if success is true add an else and then print the error message because your code wont print one unless you tell it to from the pcall. manith513 121 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Try this

local DSS = game:GetService("DataStoreService")

local DataStore = DSS:GetDataStore("CubicData")







game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")

    leaderstats.Parent = player



    local Cubes = Instance.new("IntValue")

    Cubes.Name = "Cubes"

    Cubes.Parent = leaderstats



    local data

    local success, errorMessage = pcall(function()

        data = DataStore:GetAsync(player.UserId)

    end)

    if success and data ~= nil then

        print("Data successfully loaded!")

        Cubes.Value = data.Cubes

    else

        warn(errorMessage)

    end

end)



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

    local leaderstats = player.leaderstats

    local data = {

        Cubes = leaderstats.Cubes.Value; 

    }

    local success, errorMessage = pcall(function()

        DataStore:SetAsync(player.UserId,data)

    end)

    if success then

        print("Data successfully saved!")

    else

        warn(errorMessage)

    end

end)

Hope this helps!!

0
It works now, but all it does is print nil and doesn't start the values. MrBucketOfficial 39 — 2y
0
? It works fine to me without any errors? theking66hayday 841 — 2y
Ad

Answer this question