How to calculate number string (Tonumber)

Hello, I’m PLX, and I’m currently making an admin panel using BanAsync, however, I wan’t to convert the duration time that uses seconds into days, so I made this script to do the calculations:

local l_DURATION_1 = tonumber(l_BUTTON_1.Parent.DURATION.Text) * 86400;

But it prints out an error saying this:

Players.YourPlxDev.PlayerGui.ADMIN.Frame.INFO.CONFIG_UI.BAN.LocalScript:10: attempt to perform arithmetic (mul) on string and number

So it would be a pleasure to help!

2 Likes

There doesn’t seem to be any issue with the code you’ve provided, are you sure that’s the part which is giving an error?

1 Like

Here is a function that can convert seconds to days, hours, minutes and seconds if you need it.

function FormatTime(num)
local second = num % 60
local minute = math.floor(num / 60) % 60
local hour = math.floor(num / 3600) % 24
local day = math.floor(num / 86400)

local txt = ""

if day > 1 then
txt = txt .. day .. "d "
end

if hour > 1 then
txt = txt .. hour .. "h "
end

if minute > 1 then
txt = txt .. minute .. "m "
end

if second > 1 then
txt = txt .. second .. "s"
end

return txt
end

And to clarify, are you trying to convert something like “1 day” into seconds?

1 Like

I think he may be entering something like “1 day” or “1 hour” and not a number.

1 Like

yep… It is, I asked AI about it and it wrote the same code, I’m now confused lol

1 Like

Yeah what I thought but, the error message shouldn’t have happened with the line he attached.

2 Likes

yes, I’m trying to convert seconds into days.

1 Like

Maybe you thought you made changes to your code but they never went through?
I’m not sure what is causing it

1 Like

So you should divide it by 86400, not multiply it.

1d = 86400s
1s = (1/86400)d

1 Like

No, I didn’t change the code at the time, so that doesn’t make the reason why it’s broken

1 Like

sorry to ask, but can you give me the line of code?

1 Like

local l_DURATION_1 = tonumber(l_BUTTON_1.Parent.DURATION.Text) / 86400;

I changed the line up, but somehow that line errors. What exactly is the value of DURATION.Text?

1 Like

It’s a textlabel string instance

1 Like

What did you enter originally to cause it to error? Also, labels do not accept inputs.

1 Like

Yes it does, I added this in a client-sided script, I’m also not sure why it’s causing it to say an error…

1 Like

What did you enter originally into the label mate, I need to know if you added smt like “1 day”

And no. It must be a textbox.

1 Like

oh yea, I meant the textlabel by textbox, and what I added into the textbox only numbers without strings.

1 Like
local durationText = l_BUTTON_1.Parent.DURATION.Text --string
local durationNumber = tonumber(durationText) --number
local durationInSeconds = durationNumber * 86400 --math
2 Likes

Whatever you did seems fine ig, can you send the whole scope?

1 Like

Here:
``
– // VARIABLES \ –

local l_TweenService_1 = game:GetService(“TweenService”);

local l_CONTROLS_1 = script.Parent;
local l_INFO_1 = l_CONTROLS_1.Parent.INFO;
local l_CONFIG_UI_1 = l_INFO_1.CONFIG_UI;

local l_BAN_1 = l_CONTROLS_1.BAN;
local l_UNBAN_1 = l_CONTROLS_1.UNBAN;
local l_KICK_1 = l_CONTROLS_1.KICK;

local l_SELECTED_USER_1 = l_CONTROLS_1.Parent.SELECTED_USER;

– // MAIN \ –

l_BAN_1.MouseButton1Click:Connect(function()
if l_SELECTED_USER_1.Value ~= “” then
l_CONFIG_UI_1.TITLE.Text = “Ban “…l_SELECTED_USER_1.Value…”?”
local Tween = l_TweenService_1:Create(l_CONFIG_UI_1.UIScale,TweenInfo.new(0.5,Enum.EasingStyle.Back),{Scale = 1});
Tween:Play()
else
return
end
end)
``

1 Like