How do I save the MaxLevel variable?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like the Max Level of a rebirth system to save. The MaxLevel value is sent to the client script through a remote function and the max level doubles every time the player rebirths.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is that it will not save and there is nothing in the workspace its just a variable. The MaxLevel variable always resets to 5 when the player leaves.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried getting help from other communities.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
    Server Script:
local MaxLevel = 5 
local function updateDataStore(player)
	local success, errorMessage = pcall(function()
		
		myDataStore:SetAsync(player.UserId .. "JumpHeight", player.Character.Humanoid.JumpHeight)
		
		local leaderstats = player:WaitForChild("leaderstats")
		
		local winsValue = leaderstats:WaitForChild("Wins") 
		
		myDataStore:SetAsync(player.UserId .. "Wins", winsValue.Value)
		
		local RebirthsValue = leaderstats:WaitForChild("Rebirths") 
		
		myDataStore:SetAsync(player.UserId .. "Rebirths", RebirthsValue.Value) 
		
		myDataStore:SetAsync(player.UserId .. "MaxLevel", MaxLevel)
	end)
	if success then
		print("Data is saved for " .. player.Name)
	else
		print("Error when saving data for " .. player.Name .. ": " .. errorMessage)
	end
end
local function loadDataStore(player)
	
	local success, errorMessage = pcall(function()
		
		local jumpHeightData = myDataStore:GetAsync(player.UserId .. "JumpHeight")
		
		local winsData = myDataStore:GetAsync(player.UserId .. "Wins")
		
		local Rebirthdata = myDataStore:GetAsync(player.UserId .. "Rebirths") 
		
		local MaxLevelData = myDataStore:GetAsync(player.UserId .. "MaxLevel")
		
		if jumpHeightData ~= nil then
			player.Character.Humanoid.JumpHeight = jumpHeightData
		end
		if winsData ~= nil then
			
			local leaderstats = player:WaitForChild("leaderstats")
			
			local winsValue = leaderstats:WaitForChild("Wins")
			winsValue.Value = winsData
		end 
		if Rebirthdata ~= nil  then
			local leaderstats = player:WaitForChild("leaderstats") 
			
			local RebirthData = leaderstats:WaitForChild("Rebirths") 
			RebirthData.Value = Rebirthdata
		end
		
		if MaxLevelData ~= nil  then
			MaxLevel = MaxLevelData
		end
	end)
	if not success then
		print("Error when loading data for " .. player.Name .. ": " .. errorMessage)
	end
end

RebirthFunction.OnServerInvoke = function(player) 
		
		if player.leaderstats.Wins.Value >= MaxLevel  then 
			
			player.leaderstats.Rebirths.Value = player.leaderstats.Rebirths.Value + 1 
			
			player.leaderstats.Wins.Value = 0 
			
			player.Character.Humanoid.JumpHeight = 0
		
		end 
		return false
	end

Local Script:

RebirthButton.MouseButton1Up:Connect(function(player)
	local Wins = leaderstats:WaitForChild("Wins")
	print(Wins.Value) 
	
	print("Wins.Value:", Wins.Value)
	print("MaxLevel:", MaxLevel)
	
	if Wins.Value >= MaxLevel then 
		local didPlayerRebirth = RebirthFunction:InvokeServer()  
		print("Boy") 
		 didPlayerRebirth = true  
			print(didPlayerRebirth)
			local newMultiplier = Rebirths.Value + 2 

			RebirthFrame.Multiplier.Text = "Your new multiplier will be "..newMultiplier .. "x"
			MaxLevel = MaxLevel * 2

			local newCost = MaxLevel * 2 

			RebirthFrame.Cost.Text = "Cost: " .. newCost .. "Wins"
		end   

	
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

you cant spam save to roblox datastore

You need to use data store. Save MaxLevel when player leaving and get when joining.
Docs: Saving Data | Roblox Creator Documentation

Sorry about that, I have only been on ROBLOX studio for a few months so I am relatively new to it and my knowledge is limited. I don’t know what you mean by spam save can you clarify that?

1 Like

so, to simplify things out. basically roblox datastore has its own limit 60*smth/minute. so youre limited for saving

Would I just put the variables and a table then and save them from there? Because I saved a lot of things separately and they all saved fine.

by the way as I was trying to fix the spamming datastore thing I was doing I got this error Error when loading data for bigboi_4578: leaderstats is not a valid member of Player “Players.bigboi_4578”

local function loadDataStore(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local success, errorMessage = pcall(function()
		local playerData = myDataStore:GetAsync(plr.UserId)
		if playerData then
			if playerData.JumpHeight then
				plr.Character.Humanoid.JumpHeight = playerData.JumpHeight
			end
			if playerData.Wins then
				plr.leaderstats.Wins.Value = playerData.Wins
			end
			if playerData.Rebirths then
				plr.leaderstats.Rebirths.Value = playerData.Rebirths
			end
			if playerData.MaxLevel then
				MaxLevel = playerData.MaxLevel
			end
		end
	end)
	if not success then
		print("Error when loading data for " .. plr.Name .. ": " .. errorMessage)
	end
end```
can you tell me why this is the error?

local leaderstats = plr:WaitForChild("leaderstats")
maybe you haven’t load the leaderstats folder yet?