Setting value to 0 isn't working

Im not sure why this is happening, when the Exp is greater then exptolevel it never sets the exp value to 0 or do anything, why is this happening?

if player.leaderstats.Exp.Value >= player.leaderstats.ExpToLevel.Value then
		player.leaderstats.Exp.Value = 0
		expToLevel.Value = expToLevel.Value * 1.25 
	end

Try to use print debug. Or give us any error codes.

The thing is im not getting any errors but when i test the game nothing works

I added prints but that did nothing either

Hello!

If you add print statements correctly, they print at least something. Consider shortening paths. expToLevel inside if-statement doesn’t have player.leaderstats path before it.

local expToLevel = player.leaderstats.ExpToLevel
local exp = player.leaderstats.Exp

print(exp.Value)
print(expToLevel.Value)
if (exp.Value >= expToLevel.Value) then
	exp.Value = 0
	expToLevel.Value *= 1.25 
end

print(exp.Value, expToLevel.Value)

EDIT @NotZylon the above code works perfectly fine, so the issue is related to something else. Are you sure the values don’t change? Print out values after the if-statement block. Are you doing changes from a local script? Such client changes don’t replicate, so you’ll have to do it from server script.

I tried that but it still didn’t work, the exp was greater than exptolevel but it did nothing

Im doing it from a serverscript, im in testing mode and im sure the values don’t change

		expToLevel.Value = expToLevel.Value * 1.25 

You didnt use the expToLevel variable in the conditional, so I’m going to assume that you haven’t actually declared it. Maybe that could be the problem? Although it should have created an error if it were the problem..

1 Like

I tried that but it still doesn’t work, no errors or anything

Can we see the entire script, or just the portions which relate to this problem? Right now, the code you’ve shared doesn’t really tell us anything about what could be going wrong.

1 Like

Add a else statement, and a print() statement inside your conditional check

Other possible thing is that you aren’t replicating the values to the server

Heres the entire script

		local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local ArmorModule = require(game.ReplicatedStorage:WaitForChild("ArmorHandler"))
local newArmor = ArmorModule.chooseRandom():Clone()
local Armor = newArmor
local ArmorNamez = newArmor.Name
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
local template = game.StarterGui.MainGui.InventoryHolder.Inventory.Templates.Template
local newTemplate = template:Clone()
DataStore2.Combine("Data", "Inventory", "Currency", "Level", "Exp", "ExpToLevel")

local defaultLevel = 1
local defaultexp = 0
local DefaultCurrencyAmount = 0
local DefaultHealthAmount = 150
local defaultStrength = 10

local expToLevelUp = function(level)
	return tonumber(100 * math.exp(level))
end


game.Players.PlayerAdded:Connect(function(player)

	--All the DataStores in here--
	local InventoryStore = DataStore2("Inventory", player)
	local CurrencyStore = DataStore2("Currency", player)
	local ExpStore = DataStore2("Exp", player)
	local LevelStore = DataStore2("Level", player)
	local replicatedDataFolder = Instance.new("Folder")
	replicatedDataFolder.Parent = replicatedStorage.ReplicatedData
	replicatedDataFolder.Name = player.UserId


	---------------------------------------------
	--STARTING THE INVENTORY FUNCTION--
	local inventoryString = Instance.new("StringValue")
	inventoryString.Parent = replicatedDataFolder
	inventoryString.Name = "Inventory"

	--Leaderstats--
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local LevelValue = Instance.new("NumberValue", leaderstats)
	LevelValue.Name = "Level"

	local expValue = Instance.new("NumberValue", leaderstats)
	expValue.Name = "Exp"
	
	local expToLevel = Instance.new("NumberValue", leaderstats)
	expToLevel.Name = "ExpToLevel"
	expToLevel.Value = 25
	
	local Warrior = Instance.new("IntValue",leaderstats)
	Warrior.Name = "War"	
	Warrior.Value = defaultStrength
	
	local expToLevel = player.leaderstats.ExpToLevel
	local exp = player.leaderstats.Exp

	print(exp.Value)
	print(expToLevel.Value)
	if (exp.Value >= expToLevel.Value) then
		exp.Value = 0
		expToLevel.Value = expToLevel.Value * 1.25 
	end
	
	----------------------------------------------


	--Functions--

	local function updateLevel(amount)
		player.leaderstats.Level.Value = amount
		replicatedStorage.Events.UpdateClientLevel:FireClient(player, amount)
	end

	local function updateEXP(exp)
		print(expValue.Value)
		print(expToLevel.Value)
		if exp >= expToLevelUp(LevelStore:Get(defaultLevel)) then
			ExpStore:Increment(expToLevelUp(LevelStore:Get(defaultLevel)) * -1)
			LevelStore:Increment(1)
			game.Workspace["Level Up"]:Play()
			player.leaderstats.Exp.Value = 0
			player.PlayerGui.Level.LevelUP .Visible = true
			wait(1) 
			player.PlayerGui.Level.LevelUP.Visible = false
			expToLevel.Value = expToLevel.Value * 1.25 
		else
			player.leaderstats.Exp.Value = exp
		end
		
		local expAtNextLevel = expToLevelUp(LevelStore:Get(defaultLevel))

		local currentlyHave = ExpStore:Get(defaultexp)

		player.leaderstats.ExpToLevel.Value = expAtNextLevel - currentlyHave
	
	end
	
	replicatedStorage.Events.UpdateExp.OnServerEvent:Connect(function(plr, exp)
		if player.leaderstats.Exp.Value >= expToLevel.Value then
			player.leaderstats.Exp.Value = 0
			player.PlayerGui.Level.LevelUP .Visible = true
			wait(1) 
			player.PlayerGui.Level.LevelUP.Visible = false
			expToLevel.Value = expToLevel.Value * 1.25 
		else
			player.leaderstats.Exp.Value = exp
		end
	end)
	local inventoryData = InventoryStore:Get({})
	inventoryString.Value = HttpService:JSONEncode(inventoryData)

	InventoryStore:OnUpdate(function(decodedData, player)
			if not decodedData then
			decodedData = ""
		end
		inventoryString.Value =  tostring(decodedData)
		newTemplate.ArmorName.Value =  tostring(decodedData)
		print(decodedData.ClassName)
	end)
	

	local function updateClientCurrency(amount)
		replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
	end

	updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))

	CurrencyStore:OnUpdate(updateClientCurrency)
	--call functions right away one time
	updateLevel(LevelStore:Get(defaultLevel))
	updateEXP(ExpStore:Get(defaultexp))

	LevelStore:OnUpdate(updateLevel)
	ExpStore:OnUpdate(updateEXP)
	

end)

Since you’ve said above that adding prints don’t do anything, it might be that the :WaitForChild()s at the top are yielding indefinitely, wait around 30 seconds for them to timeout by default and see if you get any warnings (yellow text)

Also, you don’t need to redeclare the variables here, just use the ones you declared above (expToLevel and expValue)

	local expToLevel = player.leaderstats.ExpToLevel
	local exp = player.leaderstats.Exp
1 Like

I added

else
print("Not Working") 

And when i went in game it printed not working

Oh, I think I see the problem now. You intialised expValue with a value of 0, and expToLevel with a value of 25. The if statement you provided in the original post checks for the values BEFORE they actually get set, so it’s always going to be checking if 0 >= 25, which will always return false.

2 Likes

@NotZylon you are comparing values at the start only. Your values are never changed, and the block of code runs only when player joins the game. If you want to track real-time changes, add .Changed event.

	expValue.Changed:Connect(function(value)
		if (value >= expToLevel.Value) then
			expValue.Value = 0
			expToLevel.Value *= 1.25
		end
	end)

EDIT

When .Changed event happens, it returns the new value.

By the way, rather not sure game.(service here), because it’s better practice to use game:GetService(), and you already have services defined, so use Players.PlayerAdded instead of game.Players.PlayerAdded.

The above script supposes you are changing values throughout the gameplay.

EDIT (2) @NotZylon unfortunately, I don’t have enough time to do a code review, but you should paste it in #help-and-feedback:code-review in order to get some detailed feedback. There are a couple of things that can be improved, like function declaring and some concept improvements. The leaderboard should be updated without the need of .Changed function.

@wc3u is providing some good insight.

2 Likes

Why are you using value If I may ask? I don’t really understand parameters

It still prints not working

So what do i do?

Using value is not required, but it can simplify things since it is available. I wouldn’t worry about it until you’ve worked out your current problem.