How can i make a datastore for this?

For your use case I would recommend simply setting everything to a table and save the table

1 Like

i saw a video on how to do it but when i tested it out it didnt work at all

It was this one but it didnt work, How to Store Multiple Values with Data Store - Roblox Studio Tutorial - YouTube

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? ]]