how to get the players pos and angle

hey! so i want to get the players pos and angle and save it to a text file so it will use the same one every time. so does any one know a tut for reading/writing to text file. and can any one tell me how to get pos/angle?

[editline]25th October 2014[/editline]

i got how to get pos/angle so just mainly the txt file stuff

Could just use PData instead of text file, might be better for you.

[lua]

if SERVER then
local function SavePlayerPos(ply, cmd, args)
local SaveData = {} – Table to keep track of data
SaveData.Pos = ply:GetPos()
SaveData.Ang = ply:GetAngles()

	local JSONData = util.TableToJSON(SaveData) -- Converts Table Data To String
	file.Write("example/".. ply:UniqueID() .. ".txt", JSONData) -- Saves File to Garrysmod/Data/example/<unique id>.txt
	-- Note that the above folder must exist!!!
end
concommand.Add("SavePlayerPos",SavePlayerPos)

local function LoadPlayerPos(ply, cmd, args)
	if 	file.Exists( "example/".. ply:UniqueID() .. ".txt","DATA") then
		Msg("Loading Player Data.

")

		local JSONData = file.Read("example/".. ply:UniqueID() .. ".txt", "DATA")
		local LoadData = util.JSONToTable(JSONData) -- Convert JSON string back to table format

		ply:SetPos(LoadData.Pos)
		ply:SetAngles(LoadData.Ang)
	else
		Msg("No File Found.

")
end
end
concommand.Add(“LoadPlayerPos”,LoadPlayerPos)

end

[/lua]

That should point you in the right direction. You’ll need to create a folder in your garrysmod/data directory before trying to save.

is there code to auto make a folder?

[editline]25th October 2014[/editline]

im making a motd that is basicly a calcview of the map and im making a command that gets the player pos and saves it so they can place the cam anywhere.

[lua]
file.CreateDir( “example” )
[/lua]

That’ll do it :slight_smile:

make sure to check if the file is already created tho or it will create it again.

Nope, it’s perfectly fine to call CreateDir even if it already exists.