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

What is wrong with my Exp Bar Script?

Asked by 10 years ago

I've made this script to display the player's Experience out of how much they need to level up. It resizes the Exp Bar, but when I change my Experience it doesn't resize the bar at all. What is wrong with it?

wait(2)

local xps = {0, 3, 6, 10, 15, 20, 25, 30, 40, 50,
    65, 80, 95, 110, 125, 140, 155, 180, 210,
    250, 300, 350, 400, 450, 500, 575, 650, 725,
    825, 1000};

function hudsize()
    local Player = game.Players.LocalPlayer
    repeat wait() until Player and Player.leaderstats
    local ExpBar = script.Parent.ExpBar
    local xp = Player.Exp.Value
    local level = Player.leaderstats.Level.Value


        local required = ((xps[level] * 100)/xp)
        wait(.1)
        local size = (required * 200)
        wait(.1)
        ExpBar.Size = UDim2.new(0,15,0,size)

        end

while wait(.5) do
    hudsize()
end

1 answer

Log in to vote
0
Answered by
war8989 35
10 years ago

You should create a ValueChanged event to handle EXP value changes, so you don't have to check for them manually every half second, because that is inefficient. Next, your algorithm for calculating EXP is incorrect, instead, you should be dividing the EXP by the level. That'll give you the correct size and should correct the bug. Finally, either check my code or copy and paste it and test it, it should work.

wait(2)

local xps = {0, 3, 6, 10, 15, 20, 25, 30, 40, 50,
    65, 80, 95, 110, 125, 140, 155, 180, 210,
    250, 300, 350, 400, 450, 500, 575, 650, 725,
    825, 1000};

Player = game.Players.LocalPlayer

function hudsize()
    repeat wait() until Player and Player.leaderstats
    local ExpBar = script.Parent.ExpBar
    local xp = Player.Exp.Value
    local level = Player.leaderstats.Level.Value
    local required = math.ceil(xp / (xps[level] * 100))

    wait(.2)
    ExpBar.Size = UDim2.new(0, 15, 0,  required * 200)
end

Player.Exp.Changed:connect(function()
    hudsize()
end)


0
I tried out your script. Now, the bar just stays the original size, even though my experience is at 1500 (set with admin, for testing.) I think the problem is my LevelUp script, which doesn't actually work. I'm posting a knew question of my Level script right now. SlickPwner 534 — 10y
Ad

Answer this question