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
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.
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..
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.
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
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.
@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.
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.