Is there a better way to write this?

I’m using HTTP service to send feedback forms to a data log base.
Is there a better way to ensure that the message is within 400 characters?

function postHTTPData(player, msg)
    if string.len(msg) < 4 then return end
    if debounce[player] == true then return end
    debounce[player] = true
    
    if string.len(msg) > 400 then
        msg = string.sub(msg, 1, 400)
    end
    
    local data = player.Name .. ": " .. msg
    Logger:Ingest(data) -- uses HTTP to post to the url & token
    
    task.wait(60)
    debounce[player] = false
end

Here’s the Ingest:

function Logger:Ingest(message)
	local log = {
		dt = DateTime.now(),
		message = tostring(message)
	}
	
	local response = HTTPService:RequestAsync({
		Url = "https://in.logtail.com", 
		Method = "POST", 
		Headers = {
			["Content-Type"] = "application/json",
			["Authorization"] = "Bearer " .. self.token
		},
		Body = HTTPService:JSONEncode(log)
		
	})
end
1 Like

msg = string.sub(msg, 1, math.min(#msg, 400))