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

Reason for why my math script is dysfunctional(Edited for progress)?

Asked by
Scerzy 85
9 years ago

I'm trying to make a level-up script. I got the leader-board to work, but the problem is that my equation doesn't work. You'll see what I mean

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild('leaderstats')
local level = leaderstats:WaitForChild('Level')
local xp = player:WaitForChild('EXP')

level.Changed:connect(function()
    local n = level.Value
    if xp.Value > 50*(n^2-n+2) then
        level.Value = level.Value+1
    end
end)

So the pattern for how much experience you need to go from level 1 --> 2 --> 3 --> 4 --> 5 --> 6 would be 100, 200, 400, 700, 1100, 1600. (It adds 100 exp from one term to the next) Here's the leader-stats script which is causing me confusion

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new('IntValue', plr)
    stats.Name = 'leaderstats'
    local experience = Instance.new('IntValue', stats)
    experience.Name = 'EXP'
    experience.Value = 0
    local level = Instance.new('IntValue',stats)
    level.Name = 'Level'
    level.Value = 1

    experience.Changed:connect(function()
        local q = experience.Value
        level.Value = math.floor((50*(q^2-q+2)))
    end)
end)

EDIT: I got the script backwards. Now when I have 1 exp, I'm level 100, with 2 exp, I'm level 200, 3 exp I'm level 400 and so on. I can't figure out a way to reverse this even though I know the problem lies in line 13 of the second script

0
I think your < sign should be a > sign, and you should also reset the EXP value to 0 when you increase the level. I'm not entirely sure what your workspace userdata setup is, but I feel like the listening event should be waiting for xp to change, not level. Legojoker 345 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

What you need

What you are looking is inverse of function exp = f(lvl) = 50*(lvl^2-lvl+2).

Getting inverse is hard for some functions if you don't have good enough knowledge in math. But that isn't the problem nowadays when you get everything you need on the Internet.

I will explain how to get the inverse of function if anyone else stumbles upon this problem. TL;DR section will be below it.

How to get inverse of function

What you can do, is use Wolfram Alpha.

http://www.wolframalpha.com/input/?i=inverse+of+f%28n%29+%3D+50*%28n%5E2-n%2B2%29

The result came out as

level = f(exp) = 1 / 2 +- 1 / 10 * sqrt(2 * exp - 175)

Since you are looking for a positive result, we should omit the '-' result.

level = f(exp) = 1 / 2 + 1 / 10 * sqrt(2 * exp - 175)

Now, again Wolfram Alpha will tell us if there is a value that is non-number.

http://www.wolframalpha.com/input/?i=f%28q%29+%3D+1%2F2+%2B+1%2F10%28sqrt%282q-175%29%29

There is an orange curve showing us the imaginary part. We should avoid this. Since imaginary ends at around 100, we don't have to worry since we can just substitute values less than or equal to 100 with level 0.

TL;DR, the actual function

So, here's the function to get the level from exp:

function getLvl(exp)
    if exp < 100 then
        return 0
    else
        return math.floor(1 / 2 + 1 / 10 * math.sqrt(2 * exp - 175))
    end
end

I tested it and it came out perfectly, if I understood what you wanted.

Hope this helped.

Ad

Answer this question