local datastoreservice = game:GetService("DataStoreService")
local mydatastore = datastoreservice:GetDataStore("mydatastore")
game.Players.PlayerAdded:Connect(function(player)
local playerid = "Player_" .. player.UserId
local data = mydatastore:GetAsync(playerid)
local ld = Instance.new("Folder", player)
ld.Name = "leaderstats"
local rebirthCount = Instance.new("IntValue", ld)
rebirthCount.Name = "Rebirth"
local damageCount = Instance.new("IntValue", ld)
damageCount.Name = "Damage"
local plusDamage = Instance.new("IntValue", ld)
plusDamage.Name = "PlusDamage"
local wins = Instance.new("IntValue", ld)
wins.Name = "Wins"
if data then
rebirthCount.Value = data["Rebirth"]
damageCount.Value = data["Damage"]
plusDamage.Value = data["PlusDamage"]
wins.Value = data["Wins"]
else
rebirthCount.Value = 0
damageCount.Value = 0
plusDamage.Value = 1
wins.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local datatobesaved = {
rebirth = player.leaderstats.Rebirth.Value,
damage = player.leaderstats.Damage.Value,
plusDamage = player.leaderstats.PlusDamage.Value,
win = player.leaderstats.Wins.Value
}
local playerid = "Player_" .. player.UserId
print("SS", player, playerid)
local success, err = pcall(function()
mydatastore:SetAsync(playerid, datatobesaved)
end)
print(success)
if success then
print("data saved!")
else
print("data faield to save!")
end
end)
Good afternoon, I found a guide on devForum where I found information on how to make good data saving. But for some reason they are not saved, here is the guide if anything: How to To save Data - Tutorial
If anything, in the settings I enabled everything as it should be, and the problem is that the first print is output and the second is not, here is the Output.
Instead of what you have now which is this:
if data then
rebirthCount.Value = data[βRebirthβ]
damageCount.Value = data[βDamageβ]
plusDamage.Value = data[βPlusDamageβ]
wins.Value = data[βWinsβ]
else
rebirthCount.Value = 0
damageCount.Value = 0
plusDamage.Value = 1
wins.Value = 0
end
Try this:
rebirthCount.Value = data[Rebirth]| or 0
damageCount.Value = data[Damage]| or 1
plusDamage.Value = data[PlusDamage]| or 0
wins.Value = data[Wins]| or 0
local datastoreservice = game:GetService("DataStoreService")
local database = datastoreservice:GetDataStore("data")
local SessionData = {}
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local rebirthCount = Instance.new("IntValue", leaderstats)
rebirthCount.Name = "Rebirth"
local damageCount = Instance.new("IntValue", leaderstats)
damageCount.Name = "Damage"
local wins= Instance.new("IntValue", leaderstats)
wins.Name = "Wins"
local plusDamage = Instance.new("IntValue", leaderstats)
plusDamage.Name = "PlusDamage"
local succses = nil
local PlayerData = nil
local attempt = 1
repeat
succses,PlayerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not succses then
warn(PlayerData)
task.wait(3)
end
until succses or attempt == 5
if succses then
print("Connected Data")
if not PlayerData then
print("Default Data Loaded")
PlayerData = {
["Rebirth"] = 0,
["Damage"] = 0,
["PlusDamage"] = 1,
["Wins"] = 0
}
end
SessionData[player.UserId] = PlayerData
else
warn("Unable Get Data For", player.UserId)
player:Kick("Unable To Load Your Data, Try Rejoining")
end
rebirthCount.Value = SessionData[player.UserId].Rebirth
rebirthCount.Changed:Connect(function()
SessionData[player.UserId].Rebirth = rebirthCount.Value
end)
damageCount.Value = SessionData[player.UserId].Damage
damageCount.Changed:Connect(function()
SessionData[player.UserId].Damage = damageCount.Value
end)
plusDamage.Value = SessionData[player.UserId].PlusDamage
plusDamage.Changed:Connect(function()
SessionData[player.UserId].PlusDamage= plusDamage.Value
end)
wins.Value = SessionData[player.UserId].Wins
damageCount.Changed:Connect(function()
SessionData[player.UserId].Wins= wins.Value
end)
end)
function playerLeaving(player)
if SessionData[player.UserId] then
local succses = nil
local errorMsg = nil
local attempt = 1
repeat
succses,errorMsg = pcall(function()
database:SetAsync(player.UserId, SessionData[player.UserId])
end)
attempt += 1
if not succses then
warn(errorMsg)
task.wait(3)
end
until succses or attempt == 5
if succses then
print("Data Saved for ", player.Name)
else
warn("Unable to save for", player.Name)
end
end
end
game.Players.PlayerRemoving:Connect(playerLeaving)
function serverShutdown()
print("Server Shutting Down")
for i,player in ipairs(game.Players:GetPlayers()) do
task.spawn(function()
playerLeaving(player)
end)
end
end
game:BindToClose(serverShutdown)
There can be grammar errors i didnt edited in studio