Take the while true bit out and add a message command.

Then add a citizen.wait at the bottom and show the hud again.

I’m new to lua but id imagine it would look something like this.

AddEventHandler('chatMessage', function(source, name, msg)
    sm = stringsplit(msg, " ");
    if sm[1] == "/hud" then
        CancelEvent()
        Citizen.Wait(0)
        HideHudComponentThisFrame(1)
        HideHudComponentThisFrame(2)
        HideHudComponentThisFrame(3)
        HideHudComponentThisFrame(4)
        HideHudComponentThisFrame(6)
        HideHudComponentThisFrame(7)
        HideHudComponentThisFrame(8)
        HideHudComponentThisFrame(9)
        HideHudComponentThisFrame(13)
        HideHudComponentThisFrame(17)
        HideHudComponentThisFrame(20)
        Citizen.Wait(20)
        ShowHudComponentThisFrame(1)
        ShowHudComponentThisFrame(2)
        ShowHudComponentThisFrame(3)
        ShowHudComponentThisFrame(4)
        ShowHudComponentThisFrame(6)
        ShowHudComponentThisFrame(7)
        ShowHudComponentThisFrame(8)
        ShowHudComponentThisFrame(9)
        ShowHudComponentThisFrame(13)
        ShowHudComponentThisFrame(17)
        ShowHudComponentThisFrame(20)
    end
end)

function stringsplit(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        t[i] = str
        i = i + 1
    end
    return t
end)

That would be the server.lua and then put this in your __resource.lua:

server_scripts {
	"server.lua"
}

Credit to Chat Commands (/me /do etc) as i used their LUA files as a reference.