local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DatastoreService = game:GetService("DataStoreService")
local Data = DatastoreService:GetDataStore("publicDATA")
local sessionData = {}
function PlayerAdded(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local stats = Instance.new("Folder", player)
stats.Name = "stats"
local prices = Instance.new("Folder", player)
prices.Name = "Prices"
local TotalCoins = Instance.new("NumberValue", stats)
TotalCoins.Name = "TotalCoins"
TotalCoins.Value = 0
local TimePlayed = Instance.new("NumberValue", stats)
TimePlayed.Name = "TimePlayed"
TimePlayed.Value = 0
local SessionTimePlayed = Instance.new("NumberValue", stats)
SessionTimePlayed.Name = "SessionTimePlayed"
SessionTimePlayed.Value = 0
local CoinsPickUp = Instance.new("NumberValue", stats)
CoinsPickUp.Name = "CoinsPickUp"
CoinsPickUp.Value = 0
local coins = Instance.new("NumberValue", leaderstats)
coins.Name = "Coins"
coins.Value = 0
local level = Instance.new("NumberValue", leaderstats)
level.Name = "Level"
level.Value = 0
local Rebirth = Instance.new("NumberValue", stats)
Rebirth.Name = "Rebirth"
Rebirth.Value = 0
local levelUpPrice = Instance.new("NumberValue", prices)
levelUpPrice.Name = "LevelUpPrice"
levelUpPrice.Value = 100
local RebirthPrice = Instance.new("NumberValue", prices)
RebirthPrice.Name = "RebirthPrice"
RebirthPrice.Value = 5
------------------------------------------------------------------------------------------------------------------------------------------------
local success, playerData = pcall(function()
return Data:GetAsync(player.UserId)
end)
if success then
print("Data loaded: " .. player.Name)
if not playerData then
print("New player, giving default data")
playerData = {
["Coins"] = 0,
["TotalCoins"] = 0,
["CoinsPickUp"] = 0,
["Level"] = 0,
["TimePlayed"] = 0,
["Rebirth"] = 0, -- error happens here
}
end
sessionData[player.UserId] = playerData
else
warn("Couldn't load data: " .. player.Name)
player:Kick("Couldn't load your data, rejoin")
end
coins.Value = sessionData[player.UserId].Coins
TotalCoins.Value = sessionData[player.UserId].TotalCoins
CoinsPickUp.Value = sessionData[player.UserId].CoinsPickUp
level.Value = sessionData[player.UserId].Level
TimePlayed.Value = sessionData[player.UserId].TimePlayed
Rebirth.Value = sessionData[player.UserId].Rebirth
coins:GetPropertyChangedSignal("Value"):Connect(function()
sessionData[player.UserId].Coins = coins.Value
sessionData[player.UserId].TotalCoins = TotalCoins.Value
sessionData[player.UserId].CoinsPickUp = CoinsPickUp.Value
sessionData[player.UserId].Level = level.Value
sessionData[player.UserId].TimePlayed = TimePlayed.Value
sessionData[player.UserId].Rebirth = Rebirth.Value
end)
end
Players.PlayerAdded:Connect(PlayerAdded)
function PlayerLeaving(player)
if sessionData[player.UserId] then
local success, errorMsg = pcall(function()
Data:SetAsync(player.UserId, sessionData[player.UserId])
end)
if success then
print("Data saved: " .. player.Name)
else
warn("Can't save: " .. player.Name)
end
end
end
Players.PlayerRemoving:Connect(PlayerLeaving)
function ServerShutdown()
if RunService:IsStudio() then
return
end
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
PlayerLeaving(player)
end)
end
end
game:BindToClose(ServerShutdown)
i think this is happens because i already has saved data before i added rebirth but how can i fix it without reseting data