Writing text to screen from file?

Hi all. I am planning out a SWEP and ran into a bit of trouble. I’d like to have it so that when the user left clicks while holding the swep, it prints text to the center of their screen that was read from a file.

If I use file:Write to write the information to a .txt file, is it possible to print that information to the center of the user’s screen when they left click?

I imagine it has something to do with file:Read and drawText but I’m not quite sure how to put them together. Any help is appreciated.

Early morning bump.

Assign the read file to a variable, then use that variable for reference - although you’ll probably have to send it to the client via the net library

How exactly would you do this?



  if SERVER then 
     --stuff below called on server
     util.AddNetworkString("example") -- you'll need to call util.AddNetworkString("net name") 
     local function SendText(to)
         if to == "all" then
            net.Start("example") -- start the net message
            net.WriteString("This is example text!") -- this writes a string in the message
            net.Broadcast() -- net.Broadcast sends it to all users
        else
            net.Start("example") -- same as above
            net.WriteString("This is example text!") -- same as above
            net.Send(to) -- same as above
       end
    end
    SendText("all") 
    SendText(player.GetAll()[1]) -- will send to the first player received in the loop 
    --serverside code stops here
  else
     -- stuff below called on client
     net.Receive("example", function(ply) -- we need to use net.Receive to tell the client what to do once we receive this net message
        print(net.ReadString()) -- will output '"This is example text!"
    end ) 
  end


to shorten it down a bit:

server:



util.AddNetworkString("example")

local function NetStuff(ply)
    net.Start("example")
    net.Send(ply)
end 


client:



net.Receive("example", function(ply) print("hi") end ) -- what to do when the client receives the message from server?


this is basic server to client. it’s the same client to server but instead of net.Send/net.Broadcast you use net.SendToServer and net.Receive with args (length, player entity)

you don’t always need to make a function just for sending net messages but it’s a lot easier
you’ll need to read from the file using file.Exists to check if the file exists first and if it does file.Read it, save it in a variable, and net.WriteString it

once you perfect this you can move onto sending other stuff in the messages, like tables with net.WriteTable(table)
and also numbers with net.WriteInt(number, bit)/net.WriteUInt(number, bit) - net.WriteUInt can’t hold negative numbers

the reason you need to use file.Read on the server is because you should trust the client as little as possible.

Is there a way to send the text only to the local user who left clicks instead of using broadcast and sending it to everyone?

yes, net.Send(player)