How Would I Save A Player's Number Value

How would I make a datastore script that saves a player’s number value? The script shown below is my old script which doesn’t work, and is inefficient. I feel like the for loop is a waste especially since there is only one item in the folder.

local ds = game:GetService("DataStoreService"):GetDataStore("Upgrade_saving__47859bv9nwvg")
local upgrade_folder = game.ServerStorage.Upgrades -- Change this to where you're storing the tools

game.Players.PlayerAdded:Connect(function(plr)
  local item_fold = Instance.new("Folder")
  item_fold.Name = "upgrades"
  item_fold.Parent = plr

  for _, v in pairs (upgrade_folder:GetChildren()) do -- Creates a new bool value for every tool in the folder
    local upgrade = Instance.new("NumberValue")
    upgrade.Name = v.Name -- Setting the bool value name to the weapon's name
    upgrade.Parent = item_fold
    upgrade.Value = 1
    if ds:GetAsync(v.Name .. plr.UserId) ~= nil then -- Checking to see if the player has ever played the game
      local succ, msg = pcall(function()
        upgrade.Value = ds:GetAsync(v.Name.. plr.UserId) -- setting the value to the saved data
      end)
      if not succ then
        warn("Problem with getting and setting data "..msg)
      end

    end
  end

end)

game.Players.PlayerRemoving:Connect(function(plr)
  local succ, msg = pcall(function()
    for _, v in pairs (plr.items:GetChildren()) do
      ds:SetAsync(v.Name .. plr.UserId, v.Value) -- Saving the values
    end
  end)

  if not succ then
    warn("Problem with saving data ".. msg)
  end
end)

I suggest you research about datastores, there are tons of YouTube tutorials and you can look at the dev hub or dev wiki.

The first thing I noticed was the first line.

You should have the service and getting the datastore as TWO different variables.

Secondly, I agree with @Quwanterz, the Dev Wiki provides tons of information on datastores and how to use various functions. (e.g :GetAsync(), :SetAsync, etc).

We could just give you a script, but what would that help with if you needed to do this in the future?

Check this article out: Data Stores | Documentation - Roblox Creator Hub

Good luck!

1 Like

As you said the loop is a waste since you only want 1 item in the upgrade_folder instead of looping through it just do

--Player Added
--Dont loop through the folder
--Instead of using v.Value just use TheNameofTheBool.Valuecall(function() --You can set the pcall inside a variable if you want
upgrade.Value = ds:GetAsync(v.Name..plr.UserId)
end)

--Player Removing
--Dont loop through the folder
--Instead of using v.Value just use TheNameofTheBool.Value
pcall(function()
 ds:SetAsync(v.Name..plr.UserId,v.Value
1 Like

could I do upgrade.Value = ds:GetAsync(v.Name..plr.UserId) or 1

What do you mean?
If you wanna change the value to the set data then do

upgrade.Value = ds:GetAsync(v.Name..plr.UserId)

What if it’s the player’s first time playing? Would I put it inside an if statement?

if ds:GetAsync(v.Name .. plr.UserId) ~= nil then
upgrade.Value = ds:GetAsync(v.Name .. plr.UserId)
else
upgrade.Value = 1

We simpky store a pcall inside a variable

local success,er = pcall(function()
 upgrade.Value = ds:GetAsync(v.Name..plr.UserId)
end)

if not success then --check if first time playing
   upgrade.Value = 1
end
1 Like