Hello scripters, I am working on my temp ban system and I am trying to convert the time to show how long is left on the user’s ban. I am having some issues with it.
My error:
“ReplicatedStorage.Common.Commands:96: attempt to perform arithmetic (add) on number and nil”
-- My time converting that I use to set my ban time happened(what time the user got banned)
function convertTime(time)
local day = 86400
local hour = 3600
local minute = 60
local lengthNumber = tonumber(string.match(time, "%d+"))
local time = string.lower(time)
if string.find(time, "s") then
seconds = lengthNumber * 1
elseif string.find(time, "m") then
seconds = lengthNumber * minute
elseif string.find(time, "h") then
seconds = lengthNumber * hour
elseif string.find(time, "d") then
seconds = lengthNumber * day
end
print("Convert time")
return seconds
end
-- The time converter I use to check how long is left on the ban
function Format(Int)
return string.format("%02i", Int)
end
function convertToHMS(Seconds)
local day = 86400
local hour = 3600
local minute = 60
local Minutes = (Seconds - Seconds%60)/60
Seconds = Seconds - Minutes*60
local Hours = (Minutes - Minutes%60)/60
Minutes = Minutes - Hours*60
print(Minutes, Seconds, Hours)
return Format(math.abs(Hours)).."h"..Format(math.abs(Minutes)).."m"..Format(math.abs(Seconds)).."s"
end
and here is the code I am trying to execute but errors.
Errors messages for operations in Luau are non-commutative so it’s easy to find out which data type is which. The first operand is a “number” and the second operand is “nil”.
To OP: What is the value of the variable time in convertTime(time)? It looks like the convertTime function returns nil.
So it takes like “1d” and will split the string looking for the number, if it is d then it does the amount of seconds in a day (86400) times the number provided, then returns it in seconds
function convertTime(time)
local day = 86400
local hour = 3600
local minute = 60
local seconds = 1
local lengthNumber = tonumber(string.match(time, "%d+"))
local time = string.lower(time)
if string.find(time, "s") then
seconds = lengthNumber * 1
elseif string.find(time, "m") then
seconds = lengthNumber * minute
elseif string.find(time, "h") then
seconds = lengthNumber * hour
elseif string.find(time, "d") then
seconds = lengthNumber * day
end
print("Convert time")
return seconds
end
print(convertTime("25d"))