Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my experience value go into the negatives?

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

✓ Accepted answer

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.

2 comments

0

thanks man

0

No problem :D

This discussion is preserved as it was. New answers, comments, and votes are closed.