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