Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my experience value go into the negatives?

Asked by
Hydrogyn 155
4 years ago
Edited 4 years ago

The >= (greater than or equal to) doesn't work and sometimes miscalculates the currentXP values into the negatives.

Is there any other efficient way to calculate this stuff?

if newvalue >= maxXP.Value then

local StarterPlayer = game:GetService("StarterPlayer")

local currentXP = StarterPlayer:WaitForChild("plrInfo").currentXP
local currentLevel = StarterPlayer:WaitForChild("plrInfo").currentLevel
local maxXP = StarterPlayer:WaitForChild("plrInfo").maxXP

currentXP.Changed:Connect(function(newvalue) 
 if newvalue >= maxXP.Value then
   currentLevel.Value = currentLevel.Value +1
   currentXP.Value = (currentXP.Value - maxXP.Value)
   maxXP.Value = maxXP.Value *1.5
  end
end)

I've tested it and sometimes currentXP is more than maxXP resulting in the exp bar not working anymore. (very rare though)

(goes into negatives): https://imgur.com/a/hhTVk14

you can test it here: https://www.roblox.com/games/4515014353/boss-scenario

1 answer

Log in to vote
1
Answered by 4 years ago

What you have to do is this:

local StarterPlayer = game:GetService("StarterPlayer")

local currentXP = StarterPlayer:WaitForChild("plrInfo").currentXP
local currentLevel = StarterPlayer:WaitForChild("plrInfo").currentLevel
local maxXP = StarterPlayer:WaitForChild("plrInfo").maxXP

currentXP.Changed:Connect(function(newvalue) 
    if newvalue >= maxXP.Value then
        currentLevel.Value = currentLevel.Value +1
        if (currentXP.Value - maxXP.Value) <= 0 then --Checks if it is less then 0
            currentXP.Value = 0
        else
            currentXP.Value = (currentXP.Value - maxXP.Value)
        end
        maxXP.Value = maxXP.Value *1.5
    end
end)

It just simply checks if it is less than or equal to 0.

0
thanks man Hydrogyn 155 — 4y
0
No problem :D firestarroblox123 440 — 4y
Ad

Answer this question