For some reason, the Prestige's Value is returning 0 (the error has been created by myself)

Hello I have another issue, this time considering to my Data to the game Button Simulator.

No matter what I’ve tried, putting waits, putting another folder, nope, nothing.

Prestige will be a new currency will be added to the version 2.0 that’ll multiply Ultra Rebirth.

My 73-lines ServerScript in ServerScriptService:

local Data = game:GetService("DataStoreService"):GetDataStore("randomthingloldataxd")
local mps = game:GetService("MarketplaceService") -- Coming soon

game.Players.PlayerAdded:Connect(function(plr)
	local ls = Instance.new("Folder", plr)
	ls.Name = "leaderstats"
	
	task.wait(0.1)
	
	local cash = Instance.new("NumberValue",ls)
	cash.Name = "Cash"
	cash.Value = 0

	task.wait(0.1)

	local multi = Instance.new("NumberValue",ls)
	multi.Name = "Multiplier"
	multi.Value = 1

	task.wait(0.1)

	local reb = Instance.new("NumberValue",ls)
	reb.Name = "Rebirth"
	reb.Value = 1

	task.wait(0.1)

	local ureb = Instance.new("NumberValue",ls)
	ureb.Name = "Ultra Rebirth"
	ureb.Value = 1
	
	local AvailableStatsButCantBeInTheLeaderstats = Instance.new("Folder",plr)
	AvailableStatsButCantBeInTheLeaderstats.Name = "leaderstats #2"

	task.wait(0.1)

	local pres = Instance.new("NumberValue", AvailableStatsButCantBeInTheLeaderstats)
	pres.Value = 1
	pres.Name = "Prestige"

	local dataload = Data:GetAsync(tostring(plr.UserId))
	
	if dataload then
		print("The leaderstats has successively loaded.")
		cash.Value = dataload[1]
		multi.Value = dataload[2]
		reb.Value = dataload[3]
		ureb.Value = dataload[4]
		pres.Value = dataload[5]
	elseif not dataload then
		error("Sorry, the leaderstats has failed to load.")
	end
	
	if pres.Value == 0 then
		error("Error On The Currency"..'"Prestige".')
		return
	end
	
	while task.wait(1/30) do
		cash.Value = cash.Value + (1*(multi.Value+0))
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	Data:SetAsync(tostring(plr.UserId),{
		plr.leaderstats.Cash.Value,
		plr.leaderstats.Multiplier.Value,
		plr.leaderstats.Rebirth.Value,
		plr.leaderstats["Ultra Rebirth"].Value,
		plr["leaderstats #2"].Prestige.Value
	})
end)

Without the extra line, at the end, it’s 72.

Video:

To be exact, here’s the Images:

The Cyan one is Prestige.

bandicam 2023-02-23 11-11-06-268

bandicam 2023-02-23 11-10-05-542

I mean I guess that makes sense why it errors.

I don’t know how this does not works, just tell me how.
When I remove the Prestige value, the Gui will not work, that’s for sure, because if nothing’s related to the Prestige it’ll simply error it’s because 7 Currencies, meant by 7 TextLabels related to them.
I have 5 Currencies, then 5 TextLabels related to them.

Here’s the LocalFile:
Random name 2 (can’t think of a name).rbxl (140.7 KB)

So yeah guys, any help is appreciated, also do not change my 73-lines script to a something like 25 lines script, to told me to put a folder inside of the Data Script.

Change this to

local dataload = Data:GetAsync(tonumber(plr.UserId))

This might work

Nope, still the same amount which is 0.

tostring is the must used after :GetAsync and :SetAsync.

tonumber is not so much used compared to tostring after :GetAsync and :SetAsync, but it’s usable.

So I prefer using tostring.

The prestige value could be saved as 0 in the data

Which is weird, I wrote “1” and the Data thinks that it’s 0.

Maybe my Data can’t handle 5+ currencies.

This variable is too long, you should simplify it to HiddenStats or HiddenData. Or just Data.

Why do you have an error when prestige is 0? Wouldn’t you start at 0 anyway?

No. Every currencies except the currency Cash starts with 1. Cash starts with 0.

Because this is what it constantly returns.

And it’s from:

if pres.Value == 0 then
	error("Error On The Currency "..'"Prestige".')
	return
end

Okay, changed it to HiddenStat because my second “leaderstats” folder has Stats on it.

Also, I can’t put 2 Data’s because it’ll not know what to choose.

Try using this?

if dataload then
    cash.Value = dataload[1]
    multi.Value = math.clamp(dataload[2], 1, math.huge)
    reb.Value = math.clamp(dataload[3], 1, math.huge)
    ureb.Value = math.clamp(dataload[4], 1, math.huge)
    pres.Value = math.clamp(dataload[5], 1, math.huge)
else
    error("Data failed to load.")
end
1 Like

Well, yes, it worked, thank you.

@bluebxrrybot Thanks for the help.
@ZerroxShiot Thanks to try to help me.
@D0GMAN_454 Thanks to try to help me.
Also, for whose tried to help me cannot anymore, since it’s solved.

I dont see why you dont do this:

pres.Value = math.max(1, dataload[2])

There’s no indication that you’re safely saving/loading the data which results in more debugging then you originally should be dealing with, you should be encasing your DataStore (GetAsync/SetAsync) methods within a pcall to ensure that the data is properly being taken care of

You don’t have to worry about that exceeding the data limit since key values handle up to 4,194,304 characters & a max size of 4 MB, you’re nowhere near that limit


And please, do not use the 2nd parameter of Instance.new(“Instance”, Parent) as it’s considered bad practice as stated in this post:

Make sure that what you’re trying to save is within the correct order as well, & include your DataStore methods in pcall functions

local Data = game:GetService("DataStoreService"):GetDataStore("randomthingloldataxd")
local mps = game:GetService("MarketplaceService") -- Coming soon

game.Players.PlayerAdded:Connect(function(plr)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr
	
	local hiddenstats = Instance.new("Folder")
	hiddenstats.Name = "leaderstats #2"
	hiddenstats.Parent = plr
	
	local cash = Instance.new("NumberValue")
	cash.Name = "Cash"
	cash.Parent = ls

	local multi = Instance.new("NumberValue")
	multi.Name = "Multiplier"
	multi.Value = 1
	multi.Parent = ls

	local reb = Instance.new("NumberValue")
	reb.Name = "Rebirth"
	reb.Value = 1
	reb.Parent = ls

	local ureb = Instance.new("NumberValue")
	ureb.Name = "Ultra Rebirth"
	ureb.Value = 1
	ureb.Parent = ls
	
	local pres = Instance.new("NumberValue")
	pres.Value = 1
	pres.Name = "Prestige"
	pres.Parent = hiddenstats

    local dataload
    local success, oops = pcall(function()
        dataload = Data:GetAsync(plr.UserId)
    end)

    if success then
        if dataload ~= nil then
            print("Data has loaded successfully to: ", plr)

            cash.Value = dataload[1]
	    	multi.Value = dataload[2]
		    reb.Value = dataload[3]
		    ureb.Value = dataload[4]
	    	pres.Value = dataload[5]
        else
            print("No data found, assuming it's a new player.") 
        end
    else
        warn("An error has occured loading the data.", oops)
    end

	if pres.Value == 0 then
		warn("Error On Prestige.")
		return
	end
	
	while true do
		cash.Value = cash.Value + (1*(multi.Value+0))
		task.wait(1 / 30)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local hiddenstats = plr:WaitForChild("leaderstats #2")
    
    local DataTable = {
        leaderstats.Cash.Value,
        leaderstats.Multiplier.Value,
        leaderstats.Rebirth.Value,
        leaderstats["Ultra Rebirth"].Value,
        hiddenstats.Prestige.Value
    }
    
    local success, oops = pcall(function()
        Data:SetAsync(plr.UserId, DataTable)
    end)
    
    if success then
        print("Successfully saved data for", plr)
    else
        warn("An error occured trying to save the data:", oops)
    end

end)

@bluebxrrybot That’s only a workaround to a simple situation, that may work sometimes but that doesn’t instruct the user on how exactly it’s supposed to fix the situation

It’s better to explain how to efficiently handle the more important aspects of data handling & how to properly use it within the future by explaining the better approaches & modern recommendations, rather than just posting code that can be topped off with a band-aid

That Argument can be used against you here as you are telling the OP to do something without actually explaining what it does, in this case you are saying pcall() to Handle Data:

  • What does it do?

  • How does it affect the Code?

  • How is it better than the Current Method?

pcall() doesnt ensure Anything, It Prevents Errors so you can Handle them easier.