Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

leaderboard not saving data?

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

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})

0

no doesn't work :c

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.

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.