Problem with datastoring leaderstats

  1. What do you want to achieve? Keep it simple and clear!
    I wanna make a data stores for leaderstats, but for some reason it prints me what everything not success
  2. What is the issue? Include screenshots / videos if possible!
    Data store don’t work
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes i did.

My code:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leaderstats
	
	local Money = Instance.new("NumberValue")
    Money.Name = "Money"
	Money.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirth"
	Rebirths.Parent = leaderstats
	
	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Clicks.Value = data
		Money.Value = data
		Rebirths.Value = data
	else

		-- Data store is working, but no current data for this player
		Clicks.Value = 0
		Money.Value = 0
		Rebirths.Value = 0
	end
	end
	local function onPlayerExit(player)  --Runs when players exit



		local success, err = pcall(function()

			local playerUserId = "Player_" .. player.UserId

			playerData:SetAsync(playerUserId, player.leaderstats.Clicks.Value) --Saves player data

		end)



		if not success then

			warn('Could not save data!')

		end

	end
	Players.PlayerAdded:Connect(leaderboardSetup)
	Players.PlayerRemoving:Connect(onPlayerExit)
	

Try using BindToClose()
Like this:

game:BindToClose(function()
    wait(1)
end)

Do that under the Connect(functions)

Still nothing

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leaderstats
	
	local Money = Instance.new("NumberValue")
    Money.Name = "Money"
	Money.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirth"
	Rebirths.Parent = leaderstats
	
	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Clicks.Value = data
		Money.Value = data
		Rebirths.Value = data
	else

		-- Data store is working, but no current data for this player
		Clicks.Value = 0
		Money.Value = 0
		Rebirths.Value = 0
	end
	end
	local function onPlayerExit(player)  --Runs when players exit



		local success, err = pcall(function()

			local playerUserId = "Player_" .. player.UserId

			playerData:SetAsync(playerUserId, player.leaderstats.Clicks.Value) --Saves player data

		end)



		if not success then

			warn('Could not save data!')

		end

	end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
	Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(onPlayerExit)
game:BindToClose(function()
	wait(1)
end)

what type of error is occuring?

Even nothing lol, idk why it means success is works, but datastore don’t save anything

Why are you saving only 1 value?
image

I was saving a 3 value by , but that gave me a error print, + money and rebirths are ungettable now

Try this

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function PlayerJOIN(player)
	
	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Clicks.Value = data[1]
		Money.Value = data[2]
		Rebirths.Value = data[3]
	else

		-- Data store is working, but no current data for this player
		Clicks.Value = 0
		Money.Value = 0
		Rebirths.Value = 0
	end
	end
	local function onPlayerExit(player)  --Runs when players exit

		local success, err = pcall(function()

			local playerUserId = "Player_" .. player.UserId
			local leaderstats = player.leaderstats
			local Values = {leaderstats.Click.Value,leaderstats.Money.Value,leaderstats.Rebirth.Value}
			playerData:SetAsync(playerUserId, Values) --Saves player data

		end)



		if not success then

			warn('Could not save data!')

		end

	end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(PlayerJOIN)
Players.PlayerRemoving:Connect(onPlayerExit)

Please put this code in another script

game.Players.PlayerAdded:Connect(function(player()
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leaderstats
	
	local Money = Instance.new("NumberValue")
    Money.Name = "Money"
	Money.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirth"
	Rebirths.Parent = leaderstats
end)

image

Ah ok what the problem is. Remove this

And try replacing with this:

local data
local data1
local data2


local success, err = pcall(function()
    data = playerData:GetAsync(player.UserId.. " Clicks")
    data1 = playerData:GetAsync(player.UserId.. " Money")
    data2 = playerData:GetAsync(player.UserId.. " Rebirths" )
end) 

 if success and data ~= nil and data1 ~= nil and data2 ~= nil then
    Clicks.Value = data
    Money.Value = data1
    Rebirths.Value = data2
else
   warn(err)
end

Remove the if data statement along with the line shown above and replace it with this code ^^^

This script doesn’t work. (30 letters)

Forgot to add data1 and data2, try it again.

Instead as saving them as 3 different values, perhaps save them as a table?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leaderstats
	
	local Money = Instance.new("NumberValue")
    Money.Name = "Money"
	Money.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirth"
	Rebirths.Parent = leaderstats
	
	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Clicks.Value = (data and data[1]) or 0
		Money.Value = (data and data[2]) or 0 
		Rebirths.Value = (data and data[3]) or 0
	end

	local function onPlayerExit(player)  --Runs when players exit



		local success, err = pcall(function()

			local playerUserId = "Player_" .. player.UserId
            local tableToSave = {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value}

			playerData:SetAsync(playerUserId, tableToSave) --Saves player data

		end)



		if not success then

			warn('Could not save data!')

		end

	end
	Players.PlayerAdded:Connect(leaderboardSetup)
	Players.PlayerRemoving:Connect(onPlayerExit)

I was too lazy to wrap the :GetAsync function on a pcall but you could probably do that yourself.

I get that. But the easiest way for me to s ave multiple values is by that.

Ughhh add where??? (30 letters)

local data
local data1
local data2

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

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

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

	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirth"
	Rebirths.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Clicks.Value = data
		Money.Value = data
		Rebirths.Value = data
	else

		-- Data store is working, but no current data for this player
		Clicks.Value = 0
		Money.Value = 0
		Rebirths.Value = 0
	end
end
local function onPlayerExit(player)  --Runs when players exit



	local success, err = pcall(function()
		data = playerData:GetAsync(player.UserId.. " Clicks")
		data1 = playerData:GetAsync(player.UserId.. " Money")
		data2 = playerData:GetAsync(player.UserId.. " Rebirths" )
	end) 

	if success and data ~= nil and data1 ~= nil and data2 ~= nil then
		player.leaderstats.Clicks.Value = data
		player.leaderstats.Money.Value = data1
		player.leaderstats.Rebirths.Value = data2
	else
		warn(err)
	end
	end
Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(onPlayerExit)

maybe i did smth wrong?

Set these to data1, data2, and data3

Data was not saved because when the user is about to leave, you didn’t use :SetAsync or :UpdateAsync,you only placed :GetAsync

Right here

Remove all of this and add my code