My Leaderstats doesnt save

  1. What do you want to achieve? Fix my Leaderstats script.

  2. What is the issue? My leaderstats doesn’t save my progress… my script:

local DS = game:GetService("DataStoreService"):GetDataStore("SaveSlaps")

game.Players.PlayerAdded:Connect(function(plr)
	wait(0.5)
	local plrkey = ("id_")..plr.UserId
	local s1 = plr.leaderstats.Slaps
	
	local GetSaved = DS:GetAsync(plrkey)
	
	if GetSaved then
		s1.Value = GetSaved[1]
	else
		local NumForSaving = {
			s1.Value
			
		}
		DS:GetAsync(plrkey,NumForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:GetAsync("id_"..plr.UserId(
		plr.leaderstats.Slaps.Value
		))
end)
  1. What solutions have you tried so far? I tried to disable and enable API services, didn’t worked, i tried to reset my script but also din’t work.

What i did wrong?

On the player removing you are getting the data not editing it

The code for if GetSaved was nil then, you are getting data and not saving it to the DataStore

Hi!
I would recommend that you use DataStore2. It’s a very good module for those who wish to just have a datasave system that works without much hassle!
Here’s the setup that I have for my own games. I’ve simplified it for you!

Have the module downloaded and place it into ServerStorage. Also create a “StringValue” in ServerStorage, that we use to change our DataVersion with, this way we can “wipe” all data in the future, just by changing the value of the DataVersion StringValue in ServerStorage. (I personally write “v1” as the default version. Also create a BoolValue in ServerStorage called “SaveInStudio”, if this boolvalue is false, then it won’t save data while you’re testing in studio.

--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(1) -- 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)

Edit: You can read up on the whole DataStore2 API here: DataStore2

Edit2:

-- If you want to add or take away slaps in a server script, do the following:

local DS_Slaps = dataStore2("Slaps"..dV,Player)
DS_Slaps:Increment(1) -- increases the current value by 1
DS_Slaps:Increment(-1) -- decreases the current value by 1
-- Simple as that!

it dint worked, also it gave me some errors…

You gotta represent the errors, without them I cannot help you. If you have set everything up correctly, and I didn’t make a typo, then everything should work as intended.

Sorry, here are the errors:

infinite yeld on possible 'serverstorage:waitforchild(“DataStoreVersion”)'

Seems like you didn’t follow my instructions correctly.

What the error means is, that the code halted, and is waiting for a object in ServerStorage called “DataStoreVersion”. It will wait forever since you didn’t put a stringvalue called DataStoreVersion into ServerStorage

Ok, the error is gone but theres a new one: Infinite yield possible on ‘ServerStorage:WaitForChild(“DataStore2”)’ Do i have to put another value in ServerStorage?

Here is the instruction that I gave you. Please make sure to read it until you understand it. And feel free to ask for more questions if there’s something I didn’t explain. :slight_smile:

“Have the module downloaded and place it into ServerStorage. Also create a “StringValue” in ServerStorage, that we use to change our DataVersion with, this way we can “wipe” all data in the future, just by changing the value of the DataVersion StringValue in ServerStorage. (I personally write “v1” as the default version. Also create a BoolValue in ServerStorage called “SaveInStudio”, if this boolvalue is false, then it won’t save data while you’re testing in studio.”

You can read up on the whole DataStore2 API here: DataStore2

You forgot one thing, Whats the value of DataVersion2?

local dataStore2 = require(ServerStorage:WaitForChild("DataStore2"))

Since it gave me this error: Infinite yield possible on ‘ServerStorage:WaitForChild(“DataStore2”)’

Hi!

You got to download the DataStore2 module, and place in your game under ServerStorage.

It can be downloaded on the page, that I have linked to my previous message.