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.

3 comments
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})
no doesn't work :c
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.