So I have had this script since my game was released and its been working like a charm. But I needed to add a “rebirth” stat to the script and this is what I did
local function SaveData(plr)
if plr and plr.leaderstats:FindFirstChild("OOFs") and plr.leaderstats:FindFirstChild("Rebirths") then
data:SetAsync(plr.UserId,plr.leaderstats:FindFirstChild("OOFs").Value)
data:SetAsync(plr.UserId,plr.leaderstats:FindFirstChild("Rebirths").Value)
end
end
game.Players.PlayerAdded:Connect(function(plr)
Instance.new("Folder",plr).Name = "leaderstats"
local stat
local function Getleaderstats()
wait(0.1)
if not plr:FindFirstChild("leaderstats") then
wait(1.5)
Getleaderstats()
else
stat = plr:WaitForChild("leaderstats")
return true;
end
end
Getleaderstats()
local function CreateStat(class,name,parent)
local i = Instance.new(class)
i.Name = tostring(name)
i.Parent = parent
return i
end
local function CreateStat2(class2,name2,parent2)
local ii = Instance.new(class2)
ii.Name = tostring(name2)
ii.Parent = parent2
return ii
end
local money = CreateStat("NumberValue","OOFs",stat)
local money2 = CreateStat2("NumberValue","Rebirths",stat)
wait(0.03)
local SavedLevel = data:GetAsync(plr.UserId,"OOFs")
local SavedLevel2 = data:GetAsync(plr.UserId,"Rebirths")
if SavedLevel then
money.Value = SavedLevel
else
money.Value = 1
wait(0.08)
SaveData(plr,money)
end
if SavedLevel2 then -------------------
money2.Value = SavedLevel2
else
money2.Value = 1
wait(0.08)
SaveData(plr,money2)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("OOFs") and plr.leaderstats:FindFirstChild("Rebirths") then
SaveData(plr,plr.leaderstats["OOFs"])
SaveData(plr,plr.leaderstats["Rebirths"])
end
end)
if not game:GetService("RunService"):IsStudio() then
game:BindToClose(function()
for _,plr in pairs(game:GetService("Players")) do
if plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("OOFs") and plr.leaderstats:FindFirstChild("Rebirths") then
SaveData(plr,plr.leaderstats["OOFs"])
SaveData(plr,plr.leaderstats["Rebirths"])
end
end
wait(10)
end)
end
When I test this in game, it only saves the rebirth stat and then saves the OOFs stat as the same value of the rebirth. In other words, the rebirth value saves in both the rebirth and OOFs value. Can someone tell me what I need to fix for both values to save their OWN values?