How do I turn nil values into 0

So, I have this script that looks into text boxes and then uses them as a number ofc, im having a problem where if theres nothing in the textbox, it comes out as nil and the multiplication doesnt work, and the whole script breaks. Is there any way I can just use a nil value as 0 when trying to multiply?

heres my script

`local datastoreservice = game:GetService(“DataStoreService”) – These two lines are the datastores
local bandata = datastoreservice:GetDataStore(“TimedBans”)

script.Parent.Parent.SecondFrame.Ban.OnServerEvent:Connect(function(plr,plrtoban,reason,days,hours,minutes,timemsg)
wait(0.1)
local uid = game.Players:GetUserIdFromNameAsync(plrtoban)
local ostime = os.time()
local days = tonumber(days)
local hours = tonumber(hours)
local minutes = tonumber(minutes)
local d = days * 86400
local h = hours * 3600
local m = minutes * 60
local length = d + h + m + ostime
local table2 = {uid,length,reason}
bandata:SetAsync(uid,table2)
if game.Players:FindFirstChild(plrtoban) == nil then
print(“Not in server”)
else
game.Players:FindFirstChild(plrtoban):Kick(“You have been banned\n For: “… reason …” Time “… timemsg …””)
end

if d ~= nil then
	d = 0
end

if h ~= nil then
	h = 0
end

if m ~= nil then
	m = 0
end

end)`

1 Like

You can utilize the usage of the and, and or Statements, for example:

local d = (days or 0) * 84600

if days doesnt exist (days is nil), it will be swapped with 0, otherwise it will be itself, the parenthesis is to make sure it does this check before multiplying (i forgor PEMDAS so that may not be the case)

However I would recommend making sure you are getting the actual data you need instead of doing this.

2 Likes

Thanks it worked, this is just for like a draft, im trying to make a slap battles styled ban UI.So this obviously is not gonna be a permanent solution, which as of rn, I dont need one.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.