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

How do you save and upload data with DataStores?

Asked by 4 years ago

when I load my player when trying to retrieve the data, I do not get an error or a success. on the other hand, the saving does work but randomly.

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("Doesnotd Matterrigh now")

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

    local RetrieveData

    local success, errorMessage = pcall(function()

    end)
    if success then
        if RetrieveData then
            player:WaitForChild("leaderstats").Days.Value = DataStore:GetAsync(player.UserId,player.leaderstats.Days.Value)
            print("Retrived")

        else

        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    print(player," Has Left")
    local days = player.leaderstats.Days.Value

    local success, errorMessage = pcall(function()
        DataStore:SetAsync(player.UserId,days)

    end)
    if success then
        print("All Saved")
    else
        print(errorMessage)
    end
end)
0
I re-wrote my answer, tested it, should work :) Utter_Incompetence 856 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

The whole point of a pcall is that it returns an error or a success in the function. You didn't write any code in the pcall, so there's no error or success to it.

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("Doesnotd Matterrigh now")

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

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

    local days = Instance.new("IntValue")
    days.Parent = leaderstats
    days.Name = "Days"


    local Days_Data

    local success, errorMessage = pcall(function()
        Days_Data = DataStore:GetAsync(player.UserId.. "days") --you should make it a keyword, rather than a value
    end)

    if success then
        days.Value = Days_Data        
        print("Retrived")
    elseif errorMessage then
        warn(errorMessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    print(player," Has Left")
    local days = player.leaderstats.Days.Value

    local success, errorMessage = pcall(function()
        DataStore:SetAsync(player.UserId.. "days", days) --key, keyword, value
    end)
    if success then
        print("All Saved")
    else
        warn(errorMessage) --warn is like print, except in orange text, showing where the warning is
    end
end)

Edit: re-wrote it, tested, works for me

0
The Days data is not even getting saved when the player leaves the game. ffancyaxax12 181 — 4y
1
Bro Thanks :) ffancyaxax12 181 — 4y
0
no problem :) Utter_Incompetence 856 — 4y
Ad

Answer this question