Saving variables - DataStore problem

So I am making a game called Sweet Empire, and I am currently working on my leaderstats/saving script. I just want to know, how can I save a variable? Like: local variable = 250
This is my script:

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore1 = dataStoreService:GetDataStore("SaveCashDataII")

local xpNeeded = 350

local function Save(plr)
	local key = "plr-"..plr.UserId
	
	local save = {
		
		["Cash"] = plr.leaderstats.Cash.Value,
		["Level"] = plr.leaderstats.Level.Value,
		["XP"] = plr.leaderstats.XP.Value,
		["RequireXP"] = xpNeeded
		
	}
	
	local success, err = pcall(function()
		dataStore1:SetAsync(key, save)
	end)
	
	if not success then
		warn("Failed to overwrite data: "..tostring(err))
	end
end

local function Load(plr)
	local xpNeeded2
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 500
	cash.Parent = leaderstats
	
	local lvl = Instance.new("NumberValue")
	lvl.Name = "Level"
	lvl.Value = 1
	lvl.Parent = leaderstats
	
	local xp = Instance.new("NumberValue")
	xp.Name = "XP"
	xp.Value = 0
	xp.Parent = leaderstats
	
	local key = "plr-"..plr.UserId
	
	local savedData
	
	local success, err = pcall(function()
		savedData = dataStore1:GetAsync(key)
	end)
	
	if not success then
		warn("Failed to save data: "..tostring(err))
		return
	end
	
	if savedData then
		cash.Value = savedData.Cash
		lvl.Value = savedData.Level
		xp.Value = savedData.XP
		xpNeeded2 = savedData.RequireXP
	else
		Save(plr)
	end
end

local function gainXP(plr)
	print(xpNeeded)
	local stats = plr:WaitForChild("leaderstats")
	
	local lvl = stats.Level
	local xp = stats.XP
	
	local xpNeeded = 350
	
	while wait(5) do
		
		xp.Value = xp.Value + 250
		
		if xp.Value >= xpNeeded then
			lvl.Value = lvl.Value + 1
			xp.Value = 0
			
			xpNeeded = xpNeeded * math.sqrt(5 * (math.random(1.5, 2)) ) --do some multiplying
			local divided = xpNeeded / 5
			local rounded = 5 * math.floor(divided)
			xpNeeded = rounded
			
			print(xpNeeded)
		end
	end
end

players.PlayerAdded:Connect(Load)
wait(1)
players.PlayerAdded:Connect(gainXP)

players.PlayerRemoving:Connect(Save)

My original script:
local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore1 = dataStoreService:GetDataStore(“SaveCashDataII”)

local function Save(plr)
	local key = "plr-"..plr.UserId
	
	local save = {
		
		["Cash"] = plr.leaderstats.Cash.Value,
		["Level"] = plr.leaderstats.Level.Value,
		["XP"] = plr.leaderstats.XP.Value
		
	}
	
	local success, err = pcall(function()
		dataStore1:SetAsync(key, save)
	end)
	
	if not success then
		warn("Failed to overwrite data: "..tostring(err))
	end
end

local function Load(plr)
	local xpNeeded2
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 500
	cash.Parent = leaderstats
	
	local lvl = Instance.new("NumberValue")
	lvl.Name = "Level"
	lvl.Value = 1
	lvl.Parent = leaderstats
	
	local xp = Instance.new("NumberValue")
	xp.Name = "XP"
	xp.Value = 0
	xp.Parent = leaderstats
	
	local key = "plr-"..plr.UserId
	
	local savedData
	
	local success, err = pcall(function()
		savedData = dataStore1:GetAsync(key)
	end)
	
	if not success then
		warn("Failed to save data: "..tostring(err))
		return
	end
	
	if savedData then
		cash.Value = savedData.Cash
		lvl.Value = savedData.Level
		xp.Value = savedData.XP
	else
		Save(plr)
	end
end

local function gainXP(plr)
	local stats = plr:WaitForChild("leaderstats")
	
	local lvl = stats.Level
	local xp = stats.XP
	
	local xpNeeded = 350
	
	while wait(5) do
		
		xp.Value = xp.Value + 250
		
		if xp.Value >= xpNeeded then
			lvl.Value = lvl.Value + 1
			xp.Value = 0
			
			xpNeeded = xpNeeded * math.sqrt(5 * (math.random(1.5, 2)) ) --do some multiplying
			local divided = xpNeeded / 5
			local rounded = 5 * math.floor(divided)
			xpNeeded = rounded
			
			print(xpNeeded)
		end
	end
end

players.PlayerAdded:Connect(Load)
wait(1)
players.PlayerAdded:Connect(gainXP)

players.PlayerRemoving:Connect(Save)

What am I supposed to do? Please give feedback.

Um, your save script seems to already save your stats in a table. Do you mean, a general server variable? You can keep that in ServerStorage and it will load for all servers… If you mean saving a player variable, you have already done that. I’m not sure what you’re asking, I feel like I am answering two questions at the same time. Could you elaborate on what you mean?

there is a variable that is called xpNeeded
local xpNeeded = 350

If it is just a constant, why do you need to save it? It can just be stated in a script and it will load when you play the game.

If your xpNeeded is dynamic, then perhaps you have some equation modelling what it should be for certain levels? In that case, you don’t need to save it, you just need to call the function that calculates what xpNeeded should be.

Again, it is hard to answer your question because you are just giving me one-liners without much background info… But hopefully I’ve covered what you are looking for.

OK. So I have an equation that happens to the xpNeeded variable every time you level up. Tho, when I rejoin the game, the variable is set back to 350

When you load your player level, set your xpNeeded to your function that you use when you level up. I.e.,

local function GetXP(level)
    -- your function here for leveling up
end

local level = Player.leaderstats.Level.Value
local xpNeeded = GetXP(level)

Make sense?

So the function is for when you level up? Sorry I am a bad scripter

1 Like

So would it be like this?
local function GetXP(level)
if level <= 1 then
–whatever–
elseif level > 1 then
–blah blah blah–
end
end

Have you tried it to see if it works? You are the one coding not me lol. Try out it :wink:

I will put all my code here once again:
local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore1 = dataStoreService:GetDataStore(“SaveCashDataI”)

local function Save(plr)
	local key = "plr-"..plr.UserId
	
	local save = {
		
		["Cash"] = plr.leaderstats.Cash.Value,
		["Level"] = plr.leaderstats.Level.Value,
		["XP"] = plr.leaderstats.XP.Value
		
	}
	
	local success, err = pcall(function()
		dataStore1:SetAsync(key, save)
	end)
	
	if not success then
		warn("Failed to overwrite data: "..tostring(err))
	end
end

local function Load(plr)
	local xpNeeded2
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 500
	cash.Parent = leaderstats
	
	local lvl = Instance.new("NumberValue")
	lvl.Name = "Level"
	lvl.Value = 1
	lvl.Parent = leaderstats
	
	local xp = Instance.new("NumberValue")
	xp.Name = "XP"
	xp.Value = 0
	xp.Parent = leaderstats
	
	local key = "plr-"..plr.UserId
	
	local savedData
	
	local success, err = pcall(function()
		savedData = dataStore1:GetAsync(key)
	end)
	
	if not success then
		warn("Failed to save data: "..tostring(err))
		return
	end
	
	if savedData then
		cash.Value = savedData.Cash
		lvl.Value = savedData.Level
		xp.Value = savedData.XP
	else
		Save(plr)
	end
end

local function GetXP(level)
	if level <= 1 then
		local RequireXP = 350
		return RequireXP
	elseif level > 1 then
		local RequireXP = 350 * level * math.sqrt(4 * (math.random(1.5, 2)) )
		RequireXP = 5 * math.floor(RequireXP / 5)
		return RequireXP
	end
end

local function gainXP(plr)
	local stats = plr:WaitForChild("leaderstats")
	
	local lvl = stats.Level.Value
	local xp = stats.XP
	
	local xpNeeded = GetXP(lvl)
	print(xpNeeded)
	
	while wait(5) do
		
		xp.Value = xp.Value + 250
		
		if xp.Value >= xpNeeded then
			lvl = lvl + 1
			xp.Value = 0
			
			xpNeeded = GetXP(lvl)
			print(xpNeeded)
		end
	end
end

players.PlayerAdded:Connect(Load)
wait(0.5)
players.PlayerAdded:Connect(gainXP)

players.PlayerRemoving:Connect(Save)

There is something still wrong. I don’t think it saves still.

Hey, I know I’m not answering your question but I have a question about the code as I am new to coding.

Why do you have xpNeeded2 as well as xpNeeded?

Oh… I put that for my first method. I never deleted it when I went to my second method.

Oh, so it’s a typo? Could this be the issue?

There shouldn’t be any issue with that variable cuz it has no purpose. It is not a typo

Oh okay, was not sure what it was for.

1 Like

Lol, I was looking through my old topics and came across this again. I soon realized I would have to make a whole new datastore. I’ve done this before so I shouldn’t have a problem doing it again!

Did you use datastore?

did you save it with a table or did you just save the variable?