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

leaderboard not saving data?

Asked by 5 years ago

I made a leaderboard that is supposed to save the player's data but this error keeps popping out and yes I checked the little box to enable API Services.

Output Error: "ServerScriptService.Leaderboard:17: attempt to index global 'data' (a nil value)"

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

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

    local cash = Instance.new("IntValue",leaderboard)
    cash.Name = "Cash"
    cash.Value = 10

    local recieved,failed = pcall(function()
        local data = dataStore:GetAsync("User-"..player.UserId)
    end)

    if recieved then
        cash.Value = data[1]
        print("Data was received")
    else
        print("Data was not received")  
    end

    while true do wait(30)
        dataStore:GetAsync("User-"..player.UserId{cash.Value})
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local leaderboard = player.Leaderboard
    local cash = leaderboard.Cash

    local saved,failed = pcall(function()
        dataStore:SetAsync("User-"..player.UserId{cash.Value})
    end)

    if saved then
        print("Data was saved")
    else
        print("Data was not saved")
    end
end)


Some assistance would be helpful if anyone knows how to fix this kind of problems.

0
you are setting data as '"User-"..player.UserId{cash.Value}' you forgot a "," try to change all SetAsync to SetAsync("User-"..player.UserId, {cash.Value}) yHasteeD 1819 — 5y
0
no doesn't work :c Simpletton 82 — 5y
0
If you're trying to make an autosave, line 33 needs to be SetAsync, also, if you are receiving "Data was not saved", the try printing the return output from pcall given in the 'failed' variable to determine a possible cause. Ziffixture 6913 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Oh I see how it's not working, Line 24 should be "SetAsync" not "GetAsync"

Ad

Answer this question