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

How to incorporate square roots into a script?

Asked by
Scerzy 85
9 years ago

I made a previous post but I found the answer myself shortly after. All I have to do is inverse a function of mine. However, this inverse function has a square root in it, and sqrt won't work in my script

NOTE: The problem is on line 14, I'm only adding the rest for context

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
        local p = level.Value
        level.Value = math.floor((1/2) + ((1/10) * sqrt(2*q - 175))

        end)
end)

The first end is underlined in red. If I add another close parenthesis at the end of line 14 then sqrt is underlined in blue. Can't seem to find the problem

0
If sqrt is underlined in blue, it hasn't been indexed. Obviously, then, sqrt is not a proper function. TheDeadlyPanther 2460 — 9y
0
Well then what function is square root? Scerzy 85 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

sqrt() is part of the math library

Where you've put sqrt(), you'll need to replace it with math.sqrt() because of the library it is a part of.

You may find it nice to know that you can get roots by using fractions with exponents (^). As an example, x^(1/2) is the same as math.sqrt(x), and similarly x^(1/3) is a cube root of x. You'll probably learn this as part of maths before or in college, depending on where you live.

0
I completely forgot about raising to a half power. I've learned this a couple years ago but I dunno why it slipped my mind. Scerzy 85 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

replace line 14 with this:

        level.Value = math.floor((1/2) + ((1/10) * math.sqrt(2*q - 175)))

the reason it wont work first of all is that you needed that other parenthesis. Also, sqrt() isn't a function. Usually any functions like that must have "math." before it, like you did with math.floor. So, it has to be math.sqrt().

Answer this question