Jump to content

[HELP] FileWrite not writing after first line


Recommended Posts

Posted

I was trying to get a table of every skin and their correspondent voice (because I couldn't find them), but while I was trying to print it, FileWrite only writes the first line.

Technically, the idea is that each second the game changes the player skin (as I couldn't manage to create a ped and retrieve a voice), and gets the skin id, and voice ids, and prints it, giving a nice table with all of them:

addCommandHandler ( "gv",
	function ()
		local localFile = fileCreate("test.csv")
		fileWrite(localFile, "PEDMODEL, VOICETYPE, VOICENAME;")
		local i = 0
		setTimer(function ()
			setElementModel(localPlayer, i)
			local voiceType, voiceName =  getPedVoice(localPlayer)
			fileWrite(localFile, tostring(i)..", "..tostring(voiceType)..", "..tostring(voiceName)..";")
			i = i + 1
		end, 1000, 10)
		fileClose(localFile)
	end
)

 

  • 2 weeks later...
Posted (edited)
can you try this
local pedVoices = {}
local pedIds = {}
local updateTimer
local currentSkinIndex = 0

function configureTable()
   local skinids = getValidPedModels()
   for i=1,#skinids do
      pedIds[skinids[i]] = true
   end
end

function writeTableDataToFile()
   local voiceFile = fileCreate("voices.csv") -- file located mods/deathmatch/resources folder
	fileWrite(voiceFile, "PEDMODEL, VOICETYPE, VOICENAME;\n")
	for _,v in pairs(pedVoices) do
		local formattedData = string.format("%s %s %s;\n", v.model_id, v.voice_type, v.voice_name)
		fileWrite(voiceFile, formattedData)
	end
	fileClose(voiceFile)
	pedVoices = {}
	pedIds = {}
	currentSkinIndex = nil
end

function setPlayerSkinAndAssignTable()
   if pedIds[currentSkinIndex] then
   	setElementModel(localPlayer, currentSkinIndex)
      local voiceType, voiceName = getPedVoice(localPlayer)
      if voiceType and voiceName then
         data = {model_id=currentSkinIndex, voice_type=voiceType, voice_name=voiceName}
         table.insert(pedVoices, data)
         print(data.model_id, data.voice_type, data.voice_name)
      else
      	 data = {model_id=currentSkinIndex, voice_type="none", voice_name="none"}
         table.insert(pedVoices, data)
         print(data.model_id, data.voice_type, data.voice_name)
      end
   end
   if currentSkinIndex == 312 then 
   	if isTimer(updateTimer) then
         killTimer(updateTimer)
         updateTimer = nil
      end
   	print("saving finished now writing file...")
      writeTableDataToFile()
      return
   end
   currentSkinIndex = currentSkinIndex + 1
end

addCommandHandler("gv",
    function()
       if isTimer(updateTimer) then
          killTimer(updateTimer)
          updateTimer = nil
       end
       configureTable()
       updateTimer = setTimer(setPlayerSkinAndAssignTable, 1000, 0)
    end)
Edited by Burak5312
  • Moderators
Posted
On 26/05/2024 at 15:47, DanHR said:

fileClose(localFile)

Just some context why your code is not working as intended.

You are currently closing the file after running the first command. Do this on onClientResourceStop + resourceRoot.

 

Quote
fileWrite(localFile, tostring(i)..", "..tostring(voiceType)..", "..tostring(voiceName)..";")
  • Don't forget to add newlines with \n.
  • After every command run fileFlush, so that the file writes are actually written to the file.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   ?

 

  Useful functions  3x 

  Tutorials  4x 

 

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...