Custom ChatBox?

I accidentally posted in the wrong section at first. I think I’m in the right section now… But here goes.

Okay, I’m trying to make a custom HUD for my favorite server. I know its a little complicated for my second Lua project ever, but I managed to follow tutorials until I got to the chatbox. I’mm completely lost on this. I have the box drawn and I got text to appear while you type, but I’m stumped on how to make the text continue to the next line when it gets too long. More importantly, I’m not sure how to display the messages other players sent in the chatbox. I tried looking at other people’s scripts but I still don’t understand it, and I was only trying because I couldn’t find any tutorials. So I would appreciate it if someone could just tell me 2 things:
A) How to make the text wrap to the next line when its too long
B) Display the chats and make them disappear after a certain amount of time when the chat box isn’t open.

The code I have already is below.


zgui = {}
zgui.typing = " "

function zgui.ProgBar(x,y,w,h,col,per)
    surface.SetDrawColor(Color(255,255,255,255));
	surface.DrawOutlinedRect( x, y, w, h );
    x = x + 1
    y = y + 1
    w = w - 2
    h = h - 2
	surface.SetDrawColor(Color(128,128,128,255));
	surface.DrawRect( x, y, w, h );
	surface.SetDrawColor(col);
	surface.DrawRect( x , y, w * per, h );
    
end

function zgui.Panel(x,y,w,h)
    surface.SetDrawColor(Color(255,255,255,255));
	surface.DrawOutlinedRect( x, y, w, h );
    x = x + 1
    y = y + 1
    w = w - 2
    h = h - 2
	surface.SetDrawColor(Color(64,64,64,255));
	surface.DrawRect( x, y, w, h );
end

function zgui.Delete(name)
    if name == "CHudHealth" then
        return false
    end
end
hook.Add("HUDShouldDraw", "NewHud", zgui.Delete)

function zgui.Status(data)
    zgui.status = data:ReadString()
end
usermessage.Hook("war_status",zgui.Status)

function zgui.StartChat()
    zgui.ChatActive = true
    return true
end
hook.Add("StartChat","EnterMessage",zgui.StartChat)

function zgui.FinishChat()
    zgui.ChatActive = false
    zgui.typing = " "
    return true
end
hook.Add("FinishChat","EndMessage",zgui.FinishChat)

function zgui.Typing(text)
    if zgui.ChatActive then
        zgui.typing = text
    else
        return
    end
end
hook.Add("ChatTextChanged","Typing",zgui.Typing)

function zgui.Create()
    zgui.Panel(0,ScrH()-64,256,64)
    zgui.ProgBar(124,ScrH()-60,128,16,Color(192,32,32,255),LocalPlayer():Health()/100)
    draw.SimpleText("Health: "..LocalPlayer():Health().."%", "ScoreboardText",4,ScrH()-60, Color(255,32,32,255), 0, 0)
    zgui.ProgBar(124,ScrH()-40,128,16,Color(192,192,32,255),LocalPlayer():Armor()/100)
    draw.SimpleText("Battery: "..LocalPlayer():Armor().."%", "ScoreboardText",4,ScrH()-40, Color(255,255,32,255), 0, 0)
    if zgui.status == nil then
        zgui.status = "ERROR: Status unknown"
    end
    draw.SimpleText("War status: "..zgui.status, "ScoreboardText",4,ScrH()-20, Color(255,255,255,255), 0, 0)
    if LocalPlayer():GetActiveWeapon() == NULL then
        return
    else
        zgui.clip = LocalPlayer():GetActiveWeapon():Clip1()
        if zgui.clip >= 0 then
            zgui.Panel(ScrW()-128,ScrH()-64,128,64)
            zgui.Pammo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
            draw.SimpleText("Primary: "..zgui.clip.."/"..zgui.Pammo, "ScoreboardText",ScrW()-124,ScrH()-60, Color(255,255,32,255), 0, 0)
            zgui.Sammo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetSecondaryAmmoType())
            draw.SimpleText("Alternate: "..zgui.Sammo, "ScoreboardText",ScrW()-124,ScrH()-40, Color(255,255,32,255), 0, 0)
            draw.SimpleText("HUD by Zombie", "ScoreboardText",ScrW()-124,ScrH()-20, Color(255,255,32,255), 0, 0)
        end
    end
    
    if zgui.ChatActive == true then
        zgui.Panel(256,ScrH()-64,256,64)
        zgui.Panel(0,ScrH()-320,512,256)
        draw.SimpleText(zgui.typing, "ScoreboardText",270,ScrH()-60, Color(255,255,32,255), 0, 0)
    end
end
hook.Add("HUDPaint", "DrawZHUD", zgui.Create)