How do I save the rebirth cost that doubles

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!
    Make a rebirth system that doubles in cost of wins every time you have rebirthed. Example: You rebirthed at 5 wins your new cost will be 10 wins.

  2. What is the issue? Include screenshots / videos if possible!
    The Rebirth system is not able to save when you rebirth and the cost would be the first cost you get before you rebirth. The cost is updated in a screen GUI.
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried getting further support 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!
    ServerScript

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RebirthFunction = ReplicatedStorage:WaitForChild("Rebirth")
local RebirthFrame = game.StarterGui.ScreenGui.RebirthFrame.Cost

local MaxLevel = 5

local Abbreviations = {
	"";
	"K";
	"M";
	"B";
	"T";
	"Q";
	"Qi";
	"Sx";
	"Sp";
	"Oc";
	"N";
	"D";
	"∞";
}

local function formatJumpHeight(jumpHeight)
	for i = 1, #Abbreviations do
		if jumpHeight < 10 ^ (i * 3) then
			if Abbreviations[i] == "∞" then
				return "∞"
			else
				return math.floor(jumpHeight / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. Abbreviations[i]
			end
		elseif tostring(jumpHeight) == "inf" then
			return "∞"
		end
	end
end

-- Function to update player data in DataStore
local function updateDataStore(player)
	local success, errorMessage = pcall(function()
		local leaderstats = player:WaitForChild("leaderstats")
		local jumpHeight = player.Character.Humanoid.JumpHeight
		local winsValue = leaderstats:WaitForChild("Wins")
		local rebirthsValue = leaderstats:WaitForChild("Rebirths")-- Assuming MaxLevel is a global variable
		local rebirthframe = RebirthFrame
		local data = {
			JumpHeight = jumpHeight,
			Wins = winsValue.Value,
			Rebirths = rebirthsValue.Value, 
			Rebirth = tostring(rebirthframe)
		}

		myDataStore:SetAsync(player.UserId, data)
	end)

	if success then
		print("Data is saved for " .. player.Name)
	else
		print("Error when saving data for " .. player.Name .. ": " .. errorMessage)
	end
end


-- Function to load player data from DataStore
local function loadDataStore(player)
	local success, errorMessage = pcall(function()
		local data = myDataStore:GetAsync(player.UserId) or {}

		local jumpHeight = data.JumpHeight
		local wins = data.Wins
		local rebirths = data.Rebirths
		local rebirthcost = data.Rebirth
		if jumpHeight ~= nil then
			player.Character.Humanoid.JumpHeight = jumpHeight
		end

		if wins ~= nil then
			local leaderstats = player:WaitForChild("leaderstats")
			local winsValue = leaderstats:WaitForChild("Wins")
			winsValue.Value = wins
		end

		if rebirths ~= nil then
			local leaderstats = player:WaitForChild("leaderstats")
			local rebirthsValue = leaderstats:WaitForChild("Rebirths")
			rebirthsValue.Value = rebirths
		end
		if rebirthcost ~= nil  then
			rebirthcost = RebirthFrame
		end
	end)

	if not success then
		print("Error when loading data for " .. player.Name .. ": " .. errorMessage)
	end
end

Local Script:

local player = game.Players.LocalPlayer  
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RebirthFunction = ReplicatedStorage:WaitForChild("Rebirth")
local visible = false
local MaxLevel = 5

local OpenrebirthFrame = script.Parent:WaitForChild("RebirthOpen") 
local RebirthFrame = script.Parent:WaitForChild("RebirthFrame") 
local RebirthButton = RebirthFrame:WaitForChild("Rebirthing") 
local leaderstats = player:WaitForChild("leaderstats") 
local Rebirths = leaderstats:WaitForChild("Rebirths") 

OpenrebirthFrame.MouseButton1Up:Connect(function() 
		if not visible then
			RebirthFrame.Visible = true
			RebirthFrame:TweenSizeAndPosition(
				UDim2.new(0.5, 0, 0.5, 0),
				UDim2.new(0.5, 0, 0.5, 0),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Sine,
				0.5,
				false
			)
			wait(0.5)
			visible = true

			RebirthFrame.Exit.MouseButton1Click:Connect(function()
				RebirthFrame:TweenSizeAndPosition(
					UDim2.new(0.01, 0, 0.01, 0),
					UDim2.new(0.5, 0, 1.5, 0),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Sine,
					0.5,
					false
				)
				wait(0.5)
				visible = false
				RebirthFrame.Visible = false
			end)
		end
	local newMultiplier = Rebirths.Value + 2 
	RebirthFrame.Multiplier.Text = "Your new multiplier will be "..newMultiplier  .. "x" 
end) 

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()  
		 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
			RebirthFrame.Cost.Text = "Cost: " .. newCost .. "Wins"
		
	else
		RebirthFrame.Cost.Text = "Not enough wins" 
		wait(3) 
		local newCost = MaxLevel 
			RebirthFrame.Cost.Text = "Cost: " .. newCost .. " Wins"
	end   
	
end)

The New multiplier variable that is in the GUI is able to save because it is connected with the Rebirths value that is saved already in the leader stats. However, the new cost variable is unable to save because it is not connected with anything that has been saved. I am not sure if this is important information but I am trying to give you the most information about my situation as possible.
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.

1 Like

Do you have the code for where the RemoteFunction’s callback is?
You would probably just check if they rebirthed there, then save the rebirths on the server.

local function updateDataStore(player)
	local success, errorMessage = pcall(function()
		local leaderstats = player:WaitForChild("leaderstats")
		local jumpHeight = player.Character.Humanoid.JumpHeight
		local winsValue = leaderstats:WaitForChild("Wins")
		local rebirthsValue = leaderstats:WaitForChild("Rebirths")-- Assuming MaxLevel is a global variable
		local rebirthframe = RebirthFrame
		local data = {
			JumpHeight = jumpHeight,
			Wins = winsValue.Value,
			Rebirths = rebirthsValue.Value, 
			Rebirth = tostring(rebirthframe)
		}

		myDataStore:SetAsync(player.UserId, data)
	end)

	if success then
		print("Data is saved for " .. player.Name)
	else
		print("Error when saving data for " .. player.Name .. ": " .. errorMessage)
	end
end
RebirthFunction.OnServerInvoke = function(player) 
--code for checking if its a success--
local rebirths = 0 --get the data from the leaderstats--
local multiplier = 1
local wins = 0
local newmultiplier = rebirths + 2
local maxlevel = 5 
--then just do your actual datastore saving--
updateDataStore(player)
end)

You must store the maxlevel in the datastore. It should be set to 5 by default, and then multplied and updated. Then you have the new cost!

This is from your server script.

local RebirthFrame = game.StarterGui.ScreenGui.RebirthFrame.Cost

The Cost part is either a TextLabel, or NumberValue, or something, but you need to add a .Value or .Text because in the code below, you’re trying to save the Frame, TextLabel, or NumberValue object rather than the value inside.

local rebirthframe = RebirthFrame -- References the Frame Above
local data = {
    JumpHeight = jumpHeight,
    Wins = winsValue.Value,
    Rebirths = rebirthsValue.Value, 
    Rebirth = tostring(rebirthframe) -- References the variable above
}

On top of that, you are grabbing the value from the StarterGui not the PlayerGui. Try this and get rid of the local RebirthFrame on line 5.

-- Function to update player data in DataStore
local function updateDataStore(player)
    local success, errorMessage = pcall(function()
        local leaderstats = player:WaitForChild("leaderstats")
        local jumpHeight = player.Character.Humanoid.JumpHeight
        local winsValue = leaderstats:WaitForChild("Wins")
        local rebirthsValue = leaderstats:WaitForChild("Rebirths")-- Assuming MaxLevel is a global variable
        local rebirthframe = player.PlayerGui.StarterGui.RebirthFrame.Cost.Value -- .Text Whichever it is
        local data = {
            JumpHeight = jumpHeight,
            Wins = winsValue.Value,
            Rebirths = rebirthsValue.Value, 
            Rebirth = tostring(rebirthframe)
        }

        myDataStore:SetAsync(player.UserId, data)
    end)

    if success then
        print("Data is saved for " .. player.Name)
    else
        print("Error when saving data for " .. player.Name .. ": " .. errorMessage)
    end
end

The issue is you can’t even access a frame from the server. There is no way to try and save a GUI, that’s only done on the client.

I was making a correction as you said that. Part of the issue is we don’t have the :InvokeServer code and part of this issue is the GUI, which I partly address in my edit above. If they are changing the value of the .RebirthFrame.Cost inside the :InvokeServer then it’s fixed with my code, sort of since they probably are still trying game.StarterGui rather than player.PlayerGui. However, if they are only testing if they are able to rebirth or updating some stats, the :InvokeServer code would probably need to get updated as well.

So you can access a Frame from the Server, but what the OP is trying to do with it isn’t the best way to do it. The best way would be to store it in a data table in code or use values under the player, like other stats they are doing for, and then grabbing those values as needed. I’m not sure why they need this value from a UI in the first place.

There is no value property of a GUI. I got the error: Error when saving data for bigboi_4578: Value is not a valid member of Text Label “Players.bigboi_4578.PlayerGui.ScreenGui.RebirthFrame.Cost”

I feel like maybe the way to do this is to use serialization. Although I don’t know a lot about it, correct me if I am wrong.

What kind of GUI object is .Cost? I used .Value in my example because I had no way of knowing what it is. However, in my comment, and in the code there is a commented text, that says to try .Text in the event that RebirthFrame.Cost is a TextLabel or something else. But without knowing what that object actually is, I had no way of providing the correct code.

.Cost is basically the cost of the rebirth system. It is how it doubles, and it shows you the required cost in order to rebirth. .Cost is a text label, and I am pretty sure there is no value property for it.

Then you need to do .Text.

I’m not sure why you would save the Text of the Label and then save it as a string instead of the number unless you are saving the cost as a number somewhere else. If you’re already saving the rebirth cost as a number as another value, saving the Text value of a TextLabel is 1) redundant and 2) unnecessary as you can just set the TextLabel to cost when the player joins.

Like you mentioned, you do not have the invoke server code. I can provide you with that right now.

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 

This is the OnServerInvoke code, and the InvokeServer is on the local script if you scroll up.

There’s two things you could do. The first is to save the Text from your TextLabel and on join set the Text of the Cost TextLabel to that Text. The other option is to not save the Text, and just save the cost as a NumberValue similar to how you set cost to "Cost: ".. newCost.. " Wins" in your local script.

I’m just confused why you have a variable called MaxLevel that you update when a user rebirths and the next line of code is literally local newCost = MaxLevel. Your cost is your MaxLevel according to your code. Just save that value instead of saving text or trying to serialize a frame that can just be updated when a player joins the game.