i am currently facing a problem with the script that i have to save the data from the leaderstats of my players.
--//Services
local DataStoreService = game:GetService("DataStoreService") --We need DataStoreService to save our data.
local Players = game:getService("Players")
--//DataStore
local CurrencyData = DataStoreService:GetDataStore("CurrencyData") --This is the name under which the database will be saved. Here we have to give a name to our Database. The name must be unique, because if there is another Database in your game with the same name, it can lead to errors.
--//Configuration
local CurrencyName = "Robux" --This is your currency name. This name will appear on the leaderboard. (Ex: Cash, Money, Coins, Clicks, etc.)
local StartingValue = 1000 --We will set this number to our value when we create it. You can give some Starting Cash to your players.
Players.PlayerAdded:Connect(function(player) --Detects when a new player joins. We also create a variable with the player.
local UserData
local success, errMsg = pcall(function() --We use pcall because sometimes the DataStore go down and we do not want the script to have errors.
UserData = CurrencyData:GetAsync(player.UserId) --We try to get user Data using ":GetAsync()"
end)
if success == false then --In case "success" returns false, it means that DataStores are down again. :/
local doNotSave = Instance.new("Folder")
doNotSave.Name = "DoNotSave"
doNotSave.Parent = player
--We create a folder called DoNotSave. When the player disconnects we will check if this folder exists, and if it does we will NOT save the data.
else
print("Data loaded!") --We let the player know that their data has been loaded successfully.
end
local leaderstats = Instance.new("Folder") --We are creating the leaderstats folder. Here we will insert our Values.
leaderstats.Name = "leaderstats" --If you want to see the currency in the default leaderboard keep the name ,,leaderstats''.
leaderstats.Parent = player
local Currency = Instance.new("IntValue") --Now we are creating an IntValue using Instance.new
Currency.Name = CurrencyName --We set the name of the Value with the name we set above, in the variable "CurrencyName".
Currency.Value = UserData or StartingValue --Now we set the value to what we received from the DataStore. If there is nothing (the player did not play) we set the value to StartingValue.
Currency.Parent = leaderstats --Storing the IntValue inside of leaderstats.
end)
Players.PlayerRemoving:Connect(function(player) --When a player lefts or disconnects from the game we are saving the data
local SavingPath = player.leaderstats:FindFirstChild(CurrencyName) --We are trying to find the IntValue
if player:FindFirstChild("DoNotSave") then --We are trying to find the folder that is created if the data was not received successfully.
warn("Player data was not saved to avoid data loss.")
else
CurrencyData:SetAsync(player.UserId, SavingPath.Value) --If the data was received successfully, then we can save the data without any problems.
end
end)
this code somewhat works, its weird to explain;
if i go shift + f9 and give myself points, then it saves the data, however it doesn’t save my data when i add or multiply scores using scripts. Can someone help? (default is 1000 value)