I am making a roblox game and i need it to save data but it wont

Viewed 26

it doesnt give error msgs but it wont save i have API services enabled no clue why it isnt working please help!

''' local dataStoreService = game:GetService("DataStoreService") local gameStore = dataStoreService:GetDataStore("TestDataStore")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstat = Instance.new("Folder" , plr)`enter code here`
    leaderstat.Name = "leaderstats"

    local Currency = Instance.new("IntValue" , leaderstat)
    Currency.Name = "Currency"
    Currency.Value = 1

    local CurrencyData

    local success, errorMsg = pcall(function()
        CurrencyData = gameStore:GetAsync(plr.UserId .. "-Currency")
    end)

    if success == true then
        Currency.Value = CurrencyData
    else
        print("Cant recover data because " .. errorMsg)
    end
end)

--Save data when server shut down
game:BindToClose(function()
    for i , plr in pairs(game.Players:GetChildren()) do
        local success , errorMsg = pcall(function()
            gameStore:SetAsync(plr.UserId .. "-level" , plr.leaderstats.Currency.Value)
        end)

        if success then
            print("Data Secured")
        else
            print("Error while saving data because " .. errorMsg)
        end
    end
end)

--Save data when somebody leaves
game.Players.PlayerRemoving:Connect(function(plr)
    for i , plr in pairs(game.Players:GetChildren()) do
        local success , errorMsg = pcall(function()
            gameStore:SetAsync(plr.UserId .. "-level" , plr.leaderstats.Currency.Value)
        end)

        if success then
            print("Data Secured")
        else
            print("Error while saving data because " .. errorMsg)
        end
    end

end)'''

1 Answers

i think you have to write the loading script like this (i haven't stored data in awhile so this may not be it):

local success, errorMsg = pcall(function()
    CurrencyData = gameStore:GetAsync(plr.UserId.Currency)
end)
Related