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

Why is my ExpBar script incorrectly setting the size?

Asked by 10 years ago

I have an Experience Bar script that I wrote, and it's really been bugging me. For some reason, it sets the bar to size 200 for practically every experience value OTHER than 0. If I set it to 0, the Size is correct, but if I set it to, for example, 50, the size is 200. 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};

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+1] * 100))

        if level < 30 then
            wait(.2)
            ExpBar.Size = UDim2.new(0, 15, 0,  (required * 200))
        elseif level >= 30 then
            wait(.2)
            ExpBar.Size = UDim2.new(0, 15, 0, 200)
        end
end

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



2 answers

Log in to vote
0
Answered by
lomo0987 250 Moderation Voter
10 years ago
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 -- You can't or rather, shouldn't have .Value
    local level = Player.leaderstats.Level -- same here

        if level.Value < 30 then -- edited this
            wait(.2)
            ExpBar.Size = UDim2.new(0, 15, 0,  (math.ceil(xp/(xps[level+1] * 100)) * 200)) -- edited
        elseif level.Value >= 30 then -- edited this
            wait(.2)
            ExpBar.Size = UDim2.new(0, 15, 0, 200)
        end
end

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

I believe this should work, what was happening was the local xp and the local level both had a .Value in them, it can't handle it. (unless it's been updated and it can) But this should work for you now.

0
I used what you gave me, and it gave me an error "Cannot perform arithmetic on userdata value" or something like that, so I changed line 16 to say (xp.Value/(xps[level.Value+1]) * 100) and it just gave me the same problem. SlickPwner 534 — 10y
0
You have to move that to required aswell, Let me update it. -- It's updated lomo0987 250 — 10y
Ad
Log in to vote
0
Answered by
war8989 35
10 years ago
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) * 100 -- This should be Current EXP divided by needed exp times 100

        if level < 30 then
            wait(.2)
            ExpBar.Size = UDim2.new(0, 15, 0,  (required * 2))
        elseif level >= 30 then
            wait(.2)
            ExpBar.Size = UDim2.new(0, 15, 0, 200)
        end
end

Player.Exp.Changed:connect(function()
    hudsize()
end)
0
I tried your script. I set my Exp to 70, and then the bar went all the way past the bottom of the screen. I'm honestly not sure why, based on the math it should be the correct size. SlickPwner 534 — 10y
0
Is your exp bar horizontal or vertical? war8989 35 — 10y

Answer this question