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

How to incorporate square roots into a script?

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

2 comments

0

If sqrt is underlined in blue, it hasn't been indexed. Obviously, then, sqrt is not a proper function.

0

Well then what function is square root?

2 answers

✓ Accepted answer

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.

1 comment

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.

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().

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