#Storing Data using DataStoreService
38 messages · Page 1 of 1 (latest)
local DataStore = DataStoreService:GetDataStore("PlayerStats")
local function SaveData(Player, Stat)
local success, errormessage = pcall(function()
local Stat1 = Player.leaderstats[Stat]
local UserKey = Player.UserId
DataStore:SetAsync("Player"..UserKey.."Stat"..Stat, Stat1.Value)
end)
if success then
print("Player "..Player.Name.."'s Data has been successfully saved, "..Player:FindFirstChild("leaderstats"):FindFirstChild(Stat).Value..", "..Stat)
else
print("Player"..Player.Name.."'s Data has not been saved.")
warn(errormessage)
end
end
local function LoadData(Player, Stat)
local StatData
local success, errormessage = pcall(function()
local Stat1 = Player:FindFirstChild("leaderstats"):FindFirstChild(Stat)
local UserKey = Player.UserId
StatData = DataStore:GetAsync("Player"..UserKey.."Stat"..Stat)
Stat1.Value = StatData
end)
if success then
print("Player "..Player.Name.."'s Data has been successfully loaded")
else
warn("Player"..Player.Name.."'s Data has not been loaded.")
warn(errormessage)
end
end
local Players = game.Players
Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = Player
leaderstats.Name = "leaderstats"
local CopiarDinero = script.Valores.Dinero
local Dinero = CopiarDinero:Clone()
Dinero.Parent = leaderstats
LoadData(Player, "Dinero")
end)
Players.PlayerRemoving:Connect(function(Player)
SaveData(Player, "Dinero")
end)```
this is pretty basic it doesn't always save
local DataStoreService = game:GetService("DataStoreService")
local DineroData = DataStoreService:GetDataStore("Dinero")
local function SaveDinero(player, dinero)
DineroData:SetAsync(player.userId, dinero)
end
local function LoadDinero(player)
return DineroData:GetAsync(player.userId) or 0
end
game.Players.PlayerLeaving:Connect(function(player)
local dinero = player.leaderstats.Dinero.Value
SaveDinero(player, dinero)
end)
game.Players.PlayerAdded:Connect(function(player)
local dinero = LoadDinero(player)
player.leaderstats.Dinero.Value = dinero
end)
while true do
wait(45)
SaveDinero()
end```
its only been used once
Create a script using for loops to save all (more) stats inside a table, save the table and load the table
ive tried that but im sending too many requests (45s)
What could you mean with that it doesn't always save
I have to use a loop for backing it up incase if it doesnt save when the player leaves
if the backup is added, there shouldn't be any data loss
** You are now Level 1! **
Really that seems like a great solution but I don't think I would apply that to my game
Thanks though
Looks good
Lol but there can be a some fixes
U know there is much more of a simple way to do that
Well yea, but if he want lets say 3 more stats, he can just add 6 lines (2 for each stat, the instance and the name) and data will load and save the values for these 3 stats
Nope u can create a table to contain a key and a value
What?
Wow u don’t know but I’ll show u
local Data = game:GetService('DataStoreService'):GetDataStore('Save')
local function UpdateData(player)
local ls = Instance.new('Folder',player)
ls.Name = 'leaderstats'
local Saving = Data:GetAsync(player.UserId)
local MainValues = {
{key = 'Coins',Value = 0},
{key = 'Multi',Value = 0},
{key = 'Rebirths',Value = 0}}
for i,v in pairs(MainValues) do
local Number = Instance.new('NumberValue',ls)
Number.Name = v.key
Number.Value = v.Value
if Saving then
Number.Value = Saving[Number.Name]
end
end
end
local function LoadData(player)
local ls = player:FindFirstChild('leaderstats')
local Saving = {}
for i,v in pairs(ls:GetChildren()) do
Saving[v.Name] = v.Value
end
return Saving
end
local Player = game.Players
Player.PlayerAdded:Connect(function(player)
UpdateData(player)
end)
Player.PlayerRemoving:Connect(function(player)
local DidSave = LoadData(player)
Data:SetAsnyc(player.UserId,DidSave)
end)
Amd how is this better?
because u dont need to create like over 100 Instance.new
each time creating a new Value
Um... Yea ig, didnt even think of creating a table to be used for a loop to create instances
But for the OP, I think my script is a little bit easier to understand
how so urs makes it not simple
It just create a normal instance that he prob knows how to use it
well the one i have doenst need to create a lot of Instances
it just needs a Table and a couple of Instances to contain the Table
Yea, I get it, but for someone that is new to Lua, that might be confusing
and plus its not really that confusing
You really wanna argue about it huh?
Ur the one arguing not me since people don’t do the correct thing
From my POV, you are the one who start arguing after I sent the script
then lets see u create a 2k line code to create a BigNum Library with a DataStore to back up the BigNum
Damn... Didn't know you are this mad, sorry
not mad its that u dont know what to do