Leaderstats save overriding another stat

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?

1 Like

This is because you’re writing to the same data store key, even though you’re trying to use two parameters.

The first parameter in either function (Set or Get) is the key. In this case, you’re using the same key which is the player’s UserId. If you were using two different DataStores, that’d be fine. (Wouldn’t recommend that.)

A simple fix would be to add some sort of string after the UserId of the player, like…

data:SetAsync(plr.UserId.."OOFs", plr.leaderstats.OOFs.Value)
data:SetAsync(plr.UserId.."Rebirths", plr.leaderstats.Rebirths.Value)

And you’d access it the same way:

local rebirths, oofs = data:GetAsync(plr.UserId.."Rebirths"), data:GetAsync(plr.UserId.."OOFs")

Hope that helps. Of course, I’d recommend saving to one key per player using a table, which may help prevent throttling requests, but it’s up to you.

Hope this helps.

1 Like

Works great, thank you for the quick support!

1 Like