SOLVED Leaderstats Doesn't Save Anything

  1. What do you want to achieve? Fix my leaderstats.

  2. What is the issue? My leaderstats doesn’t save my stats, i already tried 2 scripts but nothing work.

  3. What solutions have you tried so far? I tried to look at youtube and API but nothing.

1ST script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Slaps")

game.Players.PlayerAdded:Connect(function(Player)
	local sucess, PlayerData = pcall(function()
		return DataStore:GetAsync(Player.UserId)
	end)
	print(sucess)
	if sucess then
		Player:SetAttribute("Slaps", PlayerData or 0)
		print(PlayerData)
	else
		warn("failed")
	end
end)
--This one didn't work.

2ND Script (From my “solved” Topic)

--Script in ServerScriptService:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

-- Wait for our DataStoreVersion object to spawn in, and get it's value
local dV = ServerStorage:WaitForChild("DataStoreVersion").Value
-- Wait for our DataStore2 module object to spawn in, and require it
local dataStore2 = require(ServerStorage:WaitForChild("DataStore2"))

-- Combine all our DataStores into one
dataStore2.Combine(
	"MasterKey"..dV, -- Dont change this one
	"Slaps"..dV -- Your slaps.
	--"Slaps2"..dV -- Just a example of more data you want to save, could also be named coins ect. (Just remember to add a comma after the dV in the Slaps above
)
-- Define DefaultData for our DataStore's
local Default_Slaps = 0
--local Default_Slaps2 = 0


Players.PlayerAdded:Connect(function(Player)
	-- Make a warn in output, to tell us that the Player's data started loading
	warn("[SERVER]: Started loading [PLAYER: "..Player.Name.."'s] Datastore-data!")
	Player:SetAttribute("DataLoadingFinished", false) -- Can be used by other scripts, to determine if our data is loaded, before we do change it their data
	-- If you want, just change the Folder below to be your leaderstats folder
	-- Create a Folder for our replicated PlayerData, and put it inside our Player
	local Folder_PlayerDataFolder = Instance.new("Folder");Folder_PlayerDataFolder.Name = "PlayerData";Folder_PlayerDataFolder.Parent = Player
	-- Create a IntValue to store the number of Slaps
	local Value_Slaps = Instance.new("IntValue");Value_Slaps.Name = "Slaps";Value_Slaps.Parent = Folder_PlayerDataFolder -- Used to replicate our current data to the player or any player in the server
	-- Create a IntValue to store the number of Slaps
	--local Value_Slaps2 = Instance.new("IntValue");Value_Slaps2.Name = "Slaps2";Value_Slaps2.Parent = Folder_PlayerDataFolder

	local DS_Slaps = dataStore2("Slaps"..dV,Player) -- Define our Slaps Datastore
	--local DS_Slaps2 = dataStore2("Slaps2"..dV,Player)

	local function Update_Slaps(Value) -- We create a function here, to always update the replicated data to be equal value to our datastore data. This function will run everytime the value of Slaps in our datastore updates
		Value_Slaps.Value = Value
		warn("[SERVER]: Update_Slaps ran...")
	end

	--[[local function Update_Slaps2(Value)
		Value_Slaps2.Value = Value
		warn("[SERVER]: Update_Slaps2 ran...")
	end]]

	Update_Slaps(DS_Slaps:Get(Default_Slaps)) -- We run the Update_Slaps function, to initialize the default replicated Slaps value in our PlayerData folder. ":Get()" gets our current data of Slaps. "Default_Slaps" is used if no data was found, hit to why it's the default value.
	--Update_Slaps2(DS_Slaps2:Get(Default_Slaps2))

	DS_Slaps:OnUpdate(Update_Slaps) -- Everytime our Slaps data in our DataStore updates, run the Update_Slaps function
	--DS_Slaps2:OnUpdate(Update_Slaps2)

	-- OnCharacterAdded
	Player.CharacterAdded:Connect(function(Character) -- Since the character does not automatically spawn, we create a function to make it automatically spawn on death, after we've finished loading all the data. It's essentially the same thing the CharacterAutoLoads does.
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			task.wait(5) -- Delay before spawn, change to your liking.
			Player:LoadCharacter()
		end)
	end)

	warn("[SERVER]: Finished loading [PLAYER: "..Player.Name.."'s] Datastore-data!")
	Player:SetAttribute("DataLoadingFinished", true)
	Player:LoadCharacter() -- Also turn off CharacterAUtoLoads under "Players" in Explorer. Normally we wont have the player to spawn before all the data is loaded
end)

Whats wrong on these scripts?

1 Like

What is your issue with 2nd script?

Please read up on the whole thread, where you forked it from. :slight_smile:

1 Like

The 2nd script prints the following:

WARN: Started loading Player’s Datastore-Data!

WARN: Update_slaps ran…

WARN: Finished loading Player’s datastore-data!

Whats wrong?

1 Like

Nothing.

If you read the notes in the script I sent you, you will see that it’s using “warn” in output as a indicator for, that the player’s data started loading, and finished loading.

1 Like

But the strange thing is, my slaps doesn’t appear…

1 Like

Appear where?

Not trying to be rude, but you got to be more informative with your replies :smiley:

In the leaderstats and the display gui.
RobloxScreenShot20220816_130236119

1 Like

Did you change the PlayerData folder name from “PlayerData” to “leaderstats” as mentioned in the notes?

If you meant your “Slaps” stats do not appear on the player list, then you would have to create an “IntValue” instance inside a Folder named “leaderstats” (all lowercase). The folder must be child of the player.

More info at: In-Game Leaderboards

1 Like

Yes, i did everything that you said

Please show me all objects in ServerStorage.

Also please show me the modified script you have ingame.

stdio

Here

1 Like

You forgot to show the modified script within your game. The one that handles the saving/loading of data. :slight_smile:

1 Like

Im using your script, the other one i deleted since it wasn’t working too.

Well, you have to change PlayerData to leaderstats as mentioned, if you use my script directly.

1 Like

For the dataload (Means to the dataload, means DatasaveAndDataload), here’s the script:

local dataload = Data:GetAsync(tostring(plr.UserId))

And for the currency to load, do this:

if dataload then
 .Value = dataload[1] -- Add anything right before .Value
 .Value = dataload[2] -- Add anything right before .Value
end

I have a question for you, how many currencies there is?