How can i make a datastore for this?

Ok, I few questions:

Do you have this enabled?

image

If so, did you have any errors, what were they? And, did the data simply not save? Did you test it in studio or in a live game?

I had API on and it did not save at all, also i tested it in both live and studio oh and it didnt have any errors

Alright, one final question: Do you still have the script? Or did you delete it

i deleted :ccc let me see if i can get it back give me a min

Found it!!!

I recommend avoiding adding points or auto-saving as it adds requests to the DataStore System

More Requests = More Throttling
I recommend ProfileService as it does saving for you

1 Like

I dont get that :confused:… sorry

Ok,

local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("StatsData")
local players = game:GetService("Players")

local function SerializeLeaderstats(player) : {[string]: number}
   local stats = {}

   -- this loops through the leaderstats folder and sets the correct values in the table
   for _, v in pairs(player.leaderstats:GetChildren()) do
      stats[v.Name] = v.Value
   end

   return stats
end

local function LoadLeaderstatsData(player)
   -- "pcall" stands for protected call which basically helps with errors that may possibly occur
   local success, err = pcall(function()
      -- this returns the player's data
      return dataStore:GetAsync(player.UserId)
   end)

   if success then
      -- this checks if the call was successful
      -- this also checks if they are new/have any data

      if err ~= nil then
         for name, value in pairs(err) do
            -- this loops through the serialized table and sets the values accordingly
            player.leaderstats[name].Value = value
         end
      end
   end

   return err
end

local function OnPlayerExit(player)
   -- does what the other one does except it saves the serialized data
   local success, err = pcall(function()
      dataStore:SetAsync(player.UserId, SerializeLeaderstats(player))
   end)

   if not success then
      -- if it couldn't save, it will send a warning in the output telling you why
      warn("unable to save data:", err)
   end
end

players.PlayerAdded:Connect(function(player)
   -- do stuff (place the default things in here)
   LoadLeaderstatsData(player) -- this calls the loading function so the player's data can load
end)

players.PlayerRemoving:Connect(OnPlayerExit) -- this detects when the player leaves

P.S.

wrap the loop in a coroutine so that the script can fully function

coroutine.wrap(function()
   -- loop in here
end)()

Don’t get what? I don’t understand what you are saying

So i put this under my actual leaderstats script right?

Kind of, place the part of the script that creates the IntValues in the PlayerAdded event, but make sure you follow the comments (they’re important!)

So under this ??

Oh and btw how can i make a comment lua??

wrap the while wait() do in the coroutine and move these
image

in this
image

as you only need 1 event listener

Just put 2 hyphens

-- comment lol

For a multi-line comment, do this:

--[[
  multi line comment
]]

or

--[[
   multi line comment
--]]

Bro do u have discord? … so i can screenshare?

[[ Bro do u have discord? … so i can screenshare? ]]

Can’t you just copy and paste the script so I can show you? lol

Use 3 back ticks ``` to make a code block

-- this is a code block

is essentially:
```
– this is a code block
```

game.Players.PlayerAdded:Connect(function(Plr)
local stats = Instance.new(“Folder”, Plr)
stats.Name = “Data”
— Level System
local Levels = Instance.new(“IntValue”, stats)
Levels.Name = “Levels”
Levels.Value = 1
local Exp = Instance.new(“IntValue”, stats)
Exp.Name = “Exp”
Exp.Value = 0
local ExpNeed = Instance.new(“IntValue”, stats)
ExpNeed.Name = “ExpNeed”
ExpNeed.Value = 200
— Money System
local Gold = Instance.new(“IntValue”, stats)
Gold.Name = “Gold”
Gold.Value = 1000000000000
— Stats Text
local DefenseP = Instance.new(“IntValue”, stats)
DefenseP.Name = “DefenseP”
DefenseP.Value = 1
local SwordP = Instance.new(“IntValue”, stats)
SwordP.Name = “SwordP”
SwordP.Value = 1
local LuckP = Instance.new(“IntValue”, stats)
LuckP.Name = “LuckP”
LuckP.Value = 1
local DevilFruit = Instance.new(“IntValue”, stats)
DevilFruit.Name = “DevilFruit”
DevilFruit.Value = 1
— Stats System
local Points = Instance.new(“IntValue”, stats)
Points.Name = “Points”
Points.Value = 1000000
local PointsS = Instance.new(“IntValue”, stats)
PointsS.Name = “PointsS”
PointsS.Value = 1
local Defense = Instance.new(“IntValue”, stats)
Defense.Name = “Defense”
Defense.Value = 0
local Sword = Instance.new(“IntValue”, stats)
Sword.Name = “Sword”
Sword.Value = 0
local Luck = Instance.new(“IntValue”, stats)
Luck.Name = “Luck”
Luck.Value = 0
local Special = Instance.new(“IntValue”, stats)
Special.Name = “Special”
Special.Value = 0
end)

game.Players.PlayerAdded:Connect(function(plr)
wait(.1)
local Exp = plr.Data.Exp
local Levels = plr.Data.Levels
local ExpNeed = plr.Data.ExpNeed
local Points = plr.Data.Points

while wait() do
	if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
		Levels.Value = Levels.Value + 1
		Points.Value = Points.Value + 3
		Exp.Value = Exp.Value - ExpNeed.Value
		ExpNeed.Value = ExpNeed.Value + 100
		game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
	end
end

end)

coroutine.wrap(function()
local dss = game:GetService(“DataStoreService”)
local dataStore = dss:GetDataStore(“StatsData”)
local players = game:GetService(“Players”)

local function SerializeLeaderstats(player) : {[string]: number}
local stats = {}

-- this loops through the leaderstats folder and sets the correct values in the table
for _, v in pairs(player.leaderstats:GetChildren()) do
	stats[v.Name] = v.Value
end

return stats

end

local function LoadLeaderstatsData(player)
– “pcall” stands for protected call which basically helps with errors that may possibly occur
local success, err = pcall(function()
– this returns the player’s data
return dataStore:GetAsync(player.UserId)
end)

if success then
	-- this checks if the call was successful
	-- this also checks if they are new/have any data

	if err ~= nil then
		for name, value in pairs(err) do
			-- this loops through the serialized table and sets the values accordingly
			player.leaderstats[name].Value = value
		end
	end
end

return err

end

local function OnPlayerExit(player)
– does what the other one does except it saves the serialized data
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, SerializeLeaderstats(player))
end)

if not success then
	-- if it couldn't save, it will send a warning in the output telling you why
	warn("unable to save data:", err)
end

end

players.PlayerAdded:Connect(function(player)
– do stuff (place the default things in here)
LoadLeaderstatsData(player) – this calls the loading function so the player’s data can load
end)

players.PlayerRemoving:Connect(OnPlayerExit) – this detects when the player leaves
end)()

local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("StatsData")
local players = game:GetService("Players")

local function SerializeLeaderstats(player) : {[string]: number}
	local stats = {}

	-- this loops through the leaderstats folder and sets the correct values in the table
	for _, v in pairs(player.Data:GetChildren()) do
		stats[v.Name] = v.Value
	end

	return stats
end

local function LoadLeaderstatsData(player)
	-- "pcall" stands for protected call which basically helps with errors that may possibly occur
	local success, err = pcall(function()
		-- this returns the player’s data
		return dataStore:GetAsync(player.UserId)
	end)

	if success then
		-- this checks if the call was successful
		-- this also checks if they are new/have any data

		if err ~= nil then
			for name, value in pairs(err) do
				-- this loops through the serialized table and sets the values accordingly
				player.Data[name].Value = value
			end
		end
	end

	return err
end

local function OnPlayerExit(player)
	-- does what the other one does except it saves the serialized data
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, SerializeLeaderstats(player))
	end)

	if not success then
		-- if it couldn't save, it will send a warning in the output telling you why
		warn("unable to save data:", err)
	end
end

players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "Data"
	-- Level System

	local Levels = Instance.new("IntValue", stats)
	Levels.Name = "Levels"
	Levels.Value = 1

	local Exp = Instance.new("IntValue", stats)
	Exp.Name = "Exp"
	Exp.Value = 0

	local ExpNeed = Instance.new("IntValue", stats)
	ExpNeed.Name = "ExpNeed"
	ExpNeed.Value = 200
	-- Money System

	local Gold = Instance.new("IntValue", stats)
	Gold.Name = "Gold"
	Gold.Value = 1000000000000
	-- Stats Text

	local DefenseP = Instance.new("IntValue", stats)
	DefenseP.Name = "DefenseP"
	DefenseP.Value = 1

	local SwordP = Instance.new("IntValue", stats)
	SwordP.Name = "SwordP"
	SwordP.Value = 1

	local LuckP = Instance.new("IntValue", stats)
	LuckP.Name = "LuckP"
	LuckP.Value = 1

	local DevilFruit = Instance.new("IntValue", stats)
	DevilFruit.Name = "DevilFruit"
	DevilFruit.Value = 1

	-- Stats System
	local Points = Instance.new("IntValue", stats)
	Points.Name = "Points"
	Points.Value = 1000000

	local PointsS = Instance.new("IntValue", stats)
	PointsS.Name = "PointsS"
	PointsS.Value = 1

	local Defense = Instance.new("IntValue", stats)
	Defense.Name = "Defense"
	Defense.Value = 0

	local Sword = Instance.new("IntValue", stats)
	Sword.Name = "Sword"
	Sword.Value = 0

	local Luck = Instance.new("IntValue", stats)
	Luck.Name = "Luck"
	Luck.Value = 0

	local Special = Instance.new("IntValue", stats)
	Special.Name = "Special"
	Special.Value = 0

	coroutine.wrap(function()
		while wait() do
			if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
				Levels.Value = Levels.Value + 1
				Points.Value = Points.Value + 3
				Exp.Value = Exp.Value - ExpNeed.Value
				ExpNeed.Value = ExpNeed.Value + 100
				game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
			end
		end
	end)()

	LoadLeaderstatsData(plr) -- this calls the loading function so the player’s data can load
end)

players.PlayerRemoving:Connect(OnPlayerExit) -- this detects when the player leaves

Yo omg it worked but how can i make it save my health??