Scoring numbers in the hundredths place? HELP

I am making a game that will help test and improve gamer skills. I am doing reaction time as my first mini-game/training. I want to be able to score it to the hundredths place but the numbers end up going to a lot of decimal places and the seconds don’t count right. It will only work if I set it to tenths or whole numbers.
Here’s the script below

local scoreIncrement = 0.01

game.ReplicatedStorage.Events.AddScoreRT.OnServerEvent:Connect(function(plr)
	while true do
		wait(0.01)

		-- Check if NSstats and properties exist to avoid nil indexing errors
		if plr.NSstats and plr.NSstats:FindFirstChild("GameStarted") and plr.NSstats:FindFirstChild("GameFinished") then
			print(plr.NSstats.GameStarted.Value)
			print(plr.NSstats.GameFinished.Value)

			if plr.NSstats.GameFinished.Value == true then
				print("GameEnded")
				break
			elseif plr.NSstats.GameStarted.Value == true then
				-- Increment score with rounded value
				local Score = plr.NSstats.Score.Value + scoreIncrement 
				plr.NSstats.Score.Value = Score
				print("ADDED SCORE: " ..Score)
			end
		else
			warn("Something does not exist")
			break
		end
	end
end)

Here’s my decimal shortener that could itself be shortened. I made this for another post but someone else posted a shorter and better solution, I’ll try linking it soon once I find it but here’s mine:

local function convert(num, stopAt)
     local strNum = tostring(num)
     strNum = string.sub(strNum, string.find(strNum, "%."), string.find(strNum, "%.") + stopAt)
     num = string.gsub(tostring(num), "%.%d+", strNum)
     return tonumber(num)
end

print("Final result:")
print(convert(1.5662, 2))
-- stop at hundreths

Will the convert function work on my number value and make sure that its still a number and not a string?

1 Like

Yes because my your (it’s yours now) function converts the number into a string and then reconverts it into a number afterwards

num is the float (decimal number) you wanna convert. And stopAt is the amount of decimal places you want the function to cut at.

Try it out and you’ll see what I mean

local scoreIncrement = 0.01

local function convert(num, stopAt)
	local strNum = tostring(num)
	strNum = string.sub(strNum, string.find(strNum, "%."), string.find(strNum, "%.") + stopAt)
	num = string.gsub(tostring(num), "%.%d+", strNum)
	return tonumber(num)
end

game.ReplicatedStorage.Events.AddScoreRT.OnServerEvent:Connect(function(plr)
	while true do
		wait(0.01)

		-- Check if NSstats and properties exist to avoid nil indexing errors
		if plr.NSstats and plr.NSstats:FindFirstChild("GameStarted") and plr.NSstats:FindFirstChild("GameFinished") then
			print(plr.NSstats.GameStarted.Value)
			print(plr.NSstats.GameFinished.Value)

			if plr.NSstats.GameFinished.Value == true then
				print("GameEnded")
				break
			elseif plr.NSstats.GameStarted.Value == true then
				-- Increment score with rounded value
				local Score = plr.NSstats.Score.Value + scoreIncrement 
				convert(Score, 2)
				plr.NSstats.Score.Value = Score
				print("ADDED SCORE: " ..Score)
			end
		else
			warn("Something does not exist")
			break
		end
	end
end)

is this how its supposed to be used

1 Like

Yes it it, if you’d like to analyze and explain the code; I’d be happy to do so you can understand it

Screenshot 2024-01-11 at 9.39.41 PM

I convert the number into a string.

Then in this line I set the variable that has the string to whatever the string.sub function finds.

For the first argument which is what string it is looking for a pattern in, I just put the string version of the number.

For the second argument I simply just string the decimal point in the string (I realized that I could’ve just replaced that with simply “.” but oh well) NVM YOU DO NEED THE POSITION OF THE DECIMAL POINT NOT AN ACTUAL STRING SO YOU ACTUALLY DO NEED STRING.FIND

For the third and final argument, I once again use the string.find function with the argument for that as ”%.” but I also add the stopAt argument that you provided in the original function (convert) which basically just gets the number of numbers after the decimal point.

Then the string the sub function just isolates those numbers after the decimal point which I save to a new “num” variable.

I then just do this:

Which concatenates the isolated numbers with all the number after the decimal point which just makes it so that true desired amount of numbers after the decimal point is displayed.

While writing I’ve noticed that I could definitely shorten the function a lot and that this is not efficient but well yeah

You’re not setting the Score variable to the convert function like so:

local Score = plr.NSstats.Score.Value + scoreIncrement 

Score = convert(Score, 2)

Didn’t catch this little error, my bad. Now your code should work

Here is my new updated function it’s a bit more shortened but it does the same thing:

local function convert(num, stopAt)
     local strNum = tostring(num)
	 local pos = string.find(strNum, "%.")
     strNum = string.sub(strNum, pos, pos + stopAt)
     return tonumber(string.gsub(tostring(num), "%.%d+", strNum), 10)
end

print("Final result:")
print(convert(1.5662, 2))
-- stop at hundreths

My bad for keeping you on hold earlier, I was stringing up my super explanation

edit even better solution similar to that one I was talking about earlier that was the solution to that post I was talking about:

local function convert(n, decimalPlaces)
	return tonumber(string.format(`%.{decimalPlaces}f`, n))
end

local test = convert("1.739", 4)
print(test)

I think this should work:

math.round(x*100)/100
local x = 3.1415
x = math.round(x*100)/100
print(x)