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

how to deal with leftover experience?

recently, I have been experimenting with this and haven't found a fix to stop it from repeatedly adding 10 experience since currentXP does not equal the maxXP.

So, how do I deal with this leftover experience I'm getting and how to stop it from exceeding the maxXP limit?

local StarterPlayer = game:GetService("StarterPlayer")

local currentXP = StarterPlayer.plrInfo.currentXP
local currentLevel = StarterPlayer.plrInfo.currentLevel
local maxXP = StarterPlayer.plrInfo.maxXP

touched = true
script.Parent.Touched:Connect(function()
	if touched == true then
	if currentXP.Value == maxXP.Value then
			currentXP.Value =0
			currentLevel.Value = currentLevel.Value + 1
		    maxXP.Value = maxXP.Value*1.5
		    touched = false
		    wait(.5)
		    touched = true
		else
	currentXP.Value = currentXP.Value + 10
	touched = false
	wait(.5)
	touched = true
	end
	end
	end)



example: I need 188 experience to go on to level 3 but I only get experience in increments of 10, how do I deal with the leftover experience I have?

--

bar gui code:

local StarterPlayer = game:GetService("StarterPlayer")

local currentXP = StarterPlayer.plrInfo.currentXP
local currentLevel = StarterPlayer.plrInfo.currentLevel
local maxXP = StarterPlayer.plrInfo.maxXP

local runService = game:GetService("RunService")

local xpBar = script.Parent.bar
local text = script.Parent.xpStatus

runService.Stepped:connect(function()
	text.Text = currentXP.Value.."/"..maxXP.Value.." (lv."..currentLevel.Value..")"
	xpBar.Size = UDim2.new((currentXP.Value / maxXP.Value) * 1,0,1,0)
end)




1 comment

1 answer

✓ Accepted answer

**You can do it like this: **

local StarterPlayer = game:GetService("StarterPlayer")

local currentXP = StarterPlayer.plrInfo.currentXP
local currentLevel = StarterPlayer.plrInfo.currentLevel
local maxXP = StarterPlayer.plrInfo.maxXP

touched = true
script.Parent.Touched:Connect(function()
    if touched == true then
    if currentXP.Value >= maxXP.Value then
            currentXP.Value = currentXP.Value - maxXP.Value
            currentLevel.Value = currentLevel.Value + 1
            maxXP.Value = maxXP.Value*1.5
            touched = false
            wait(.5)
            touched = true
        else
        currentXP.Value = currentXP.Value + 10
        touched = false
        wait(.5)
        touched = true
    end
   end
end)

That's should solve your problem

Edited: I just tested it and works for me....

local StarterPlayer = game:GetService("StarterPlayer")

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

Insert this script to ServerScriptService and you are good to go

12 comments

0

the currentXP values goes into the negatives once I hit a number that isn't divisible by 10

0

Can you send another video cuz the imgur one is blury.

0

https://www.roblox.com/games/4515014353/boss-scenario touch the yellow brick for xp and I'm also using the script you've shown

0

Code of the xp bar?

0

if you keep going till you reach level 3 you'll see what I mean

0

Still works for me at level 3 +

0

All i want is to see the xp bar code cuz that's the issue

0

how do I make it so it's always the server is always checking if the currentXP is more than the maxXP

0

okay I added the bar gui code

0

Doesn't work but thanks for trying. gtg anyways

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