Saving Bool or Int Value

How do you save a integer or bool value? I’ve read a few articles about this and they are all confusing, I just want to save this:

image

Save to Data Store, right?


To get DataStore instance you must use GetDataStore with the name of the data store, you can also add a Scope which is like a folder inside the DataStore

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

to save use SetAsync, you give it a key or unique name and the value to save

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataTest")
DataStore:SetAsync("Test", 20)

and to load use GetAsync, with the previous key or unique name

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataTest")
local Data = DataStore:GetAsync("Test")

if there is no data, GetAsync will return nil.
More information

1 Like

But, how do you access the value through scripts?

literally just path:to:instance.Value?

DataStore can be used both for values related to players and only for the server, here is an example for players

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	-- leaderstats code
	
	local Coins = DataStore:GetAsync("Coins_" .. Player.UserId)
	if Coins then
		print(Player, "has", Coins, "coins")
	else
		print(Player, "has no coins")
	end
end)
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	local Coins = -- player coins
	DataStore:SetAsync("Coins_" .. Player.UserId, Coins)
end)

it will get the player’s previous coins and give them to him, when he leaves it will save them,

pcall

Sometimes due to problems with Roblox or yours DataStore cannot work, pcall is responsible for detecting errors that occur

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	-- leaderstats code
	
	local Success, Coins = pcall(DataStore.GetAsync, DataStore, "Coins_" .. Player.UserId)
	if Success and Coins then
		print(Player, "has", Coins, "coins")
	elseif Success and not Coins then
		print(Player, "has no coins")
	else
		warn("An error has occurred:")
		warn(Coins)
	end
end)
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	local Coins = -- player coins

	local Success, Err = pcall(DataStore.SetAsync, DataStore, "Coins_" .. Player.UserId, Coins)
	if Success then
		print(Coins, "coins of", Player, "have been saved")
	else
		warn("An error has occurred:")
		warn(Coins)
	end
end)

Sources: leaderstats - saving data - pcall

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	-- leaderstats code

	local Coins = DataStore:GetAsync("Coins_" .. Player.UserId)
	if Coins then
		print(Player, "has", Coins, "coins")
	else
		print(Player, "has no coins")
	end
end)
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	local Coins = game.Lighting.PixelBoots
		DataStore:SetAsync("Coins_" .. Player.UserId, Coins)
end)

I’m still confused on how to connect it to the value which is in Lighting.

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

local instance = game.Lighting.PixelBoots
DataStore:SetAsync("PixelBoots", instance.Value)

instance.Changed:Connect(function()
  DataStore:SetAsync("PixelBoots", instance.Value)
)

instance.Value = DataStore:GetAsync("PixelBoots")

this will save the pixelboots value every time its changed and load it once the server starts look at sotr654 first reply to see how it works

I tried this, but it didn’t work. This goes in ServerScriptService, right?

do u want the value to be saved globally or just in the server?

I want it to be saved globally.

so everytime that value changes in one server you want it to be saved and loaded in all servers? dont see what youre trying to do here, its going to be constantly saving and loading

I want it the player to have their saved value every time they join any server.

For some reason, it still isn’t saving the bool value.

bruuuuh you just did a fireevent for all players

Yeah, I have this turned on:
image

oh copy the script again i forgot to pass in the player

Still doesn’t work for some reason, datastores are so confusing.

local DSS = game:GetService("DataStoreService")
local thisDataStore = DSS:GetDataStore("PixelBootsDataStore")
local players = game:GetService("Players")
local runService = game:GetService("RunService")

local defaultData = --whatever you'd like

players.PlayerAdded:Connect(function(plr)
       local pixelBoots = Instance.new("NumberValue")
pixelBoots.Name = "PixelBoots"
       pixelBoots.Parent = plr
      local data
local success, response = pcall(function()
     data = thisDataStore:GetAsync(plr.UserId)
end)
if success then
   if data then
       pixelBoots.Value = data.PixelBoots
   else
      pixelBoots.Value = defaultData
   end
  plr:SetAttribute("HasLoadedDataStore", true)
  end
end)
 players.PlayerRemoving:Connect(function(plr)
       local pixelBoots = plr.PixelBoots
     local data = {pixelBoots .Value}
      local hadLoadedAttribute = plr:GetAttribute("HasLoadedDataStore")
      local success, response = pcall(function()
      if not RunService:IsStudio() and plr:GetAttribute(hasLoaded) == true then
         settingsDS:SetAsync(plr.UserId, data)
      end
   end)
end)

but there are # amounts of local players mate, and it runs for all of them except himself lol

That doesn’t work either. (30 letters)