Jump to content

fileWrite problem (bad string)


PLEP

Recommended Posts

Basicly, I'm trying to make a small script that would save my positions in a test.txt, but I can't get getElementPosition to print out as text in the file. Any ideas?

function consoleCommandTest ( thePlayer, command )
local newFile = fileCreate("test.txt")             
if (newFile) then
a = getPlayerFromName( "PLEP" )
if a then
	b = getElementPosition (a)
if b then
fileWrite(newFile, b)       
fileClose(newFile)
end
end
end
end
addCommandHandler ( "mypos", consoleCommandTest )

Link to comment
  • Discord Moderators

fileWrite takes string as arguments after theFile.

You are trying to send an integer.

To fix this, use tostring which will convert the integer into a string.

Secondly, getElementPosition returns three floats. You cannot store this in one variable, so you need three or else you will only have your X position printed.

function consoleCommandTest ( thePlayer, command )
local newFile = fileCreate("test.txt")             
if (newFile) then
  a = getPlayerFromName( "PLEP" )
if a then
     x, y, z = getElementPosition (a)
if x and y and z then
fileWrite(newFile, tostring(x), " ", tostring(y), " ", tostring(z))       
fileClose(newFile)
end
end
end
end
addCommandHandler ( "mypos", consoleCommandTest )

Link to comment

Well, yeah. But I'm trying to find out how all this stuff works with files to get atleast some scripting experience. There are no examples about xml in wiki also.

However, now I made another function that would read my file till the end of it and write in another position (Without overwriting the first one). But when I type /mypos in MTA, it causes some excessive error spam in the server window, then I get network troubles. Looks like something's wrong with fileIsEOF and fileRead. I tried everything that would come up in my head, but nothing worked.

function consoleAddPos ( thePlayer, command )
local testFile = fileOpen("Positions.txt")
if (testFile) then
	local buffer
	while not fileIsEOF(testFile) do           
	buffer = fileRead(testFile, 60)
		local c = getPlayerFromName("PLEP")
		if c then
			local x, y, z = getElementPosition (c)
			if x and y and z then
				fileWrite(testFile, "          ", tostring(x), " ", tostring(y), " ", tostring(z), "          ") -- Those "        " in the beginning and ending are supposed to be spacebars between positions.
				fileClose(testFile)
			end
		end
	end
end
end
addCommandHandler ( "mypos", consoleAddPos )

Link to comment
function myPosition(player,command)
local file											-- Just make the file variable local to the function scope. We don't have to assign anything to it though
if fileExists("output.txt") then					-- Check if the file exists
	file = fileOpen("output.txt")					-- If it does, open it with read & write access
else												-- If it doesn't
	file = fileCreate("output.txt")					-- create the file
end
fileSetPos(file,fileGetSize(file))					-- set the read/write position to the end of file. fileGetSize effectively returns the number of bytes from the start of file to the end
local x,y,z = getElementPosition(player)			-- get the position of the player
fileWrite(file,("%s, %s, %s \n"):format(x,y,z))		-- now we write it into the file. Formatting strings is another way to add different strings up. Search for StringLibraryTutorial on lua wiki for more information. \n is a newline. It actually is string.format("%s, %s, %s \n",x,y,z), but it can be shortened to the form which I wrote before
fileClose(file)										-- Close the file. fileClose also automatically saves the file.
end
addCommandHandler("mypos",myPosition)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...