Trying to write to a file. but errors happen

error :

Failed to execute thread: server.lua:39: attempt to index a nil value (global 'file')

this is the script.

local os_time = os.time
local os_date = os.date


local entityIdToPlayer = {}

local LogConnect = true
local LogDisconnect = true

RegisterServerEvent('logs:onPlayerKilled')
AddEventHandler('logs:onPlayerKilled', function(t,killer, kilerT) -- t : 0 = NPC, 1 = player
  file = io.open("logs/KillLogs.txt", "w")
  local local_hour = os_date("%I:%M:%S")


  if(t == 1) then
     if(GetPlayerName(killer) ~= nil and GetPlayerName(source) ~= nil)then

       if(kilerT.killerinveh) then
         local model = kilerT.killervehname
         if file then
             print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by  : "..GetPlayerName(killer).." ("..getPlayerID(killer)..") | Vehicle"..model)

             file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by : "..GetPlayerName(killer).." ("..getPlayerID(killer)..") | Vehicle : "..model)
             file:write("\n")
         end

       else
          if file then
             print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by : "..GetPlayerName(killer).." ("..getPlayerID(killer)..")")

             file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by : "..GetPlayerName(killer).." ("..getPlayerID(killer)..")")
             file:write("\n")
         end
       end    
    end
  else
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by a NPC or vehicle")
    file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by a NPC or vehicle")
    file:write("\n")
  end
  file:close() 
end)


RegisterServerEvent("logs:sendPoliceLog")
AddEventHandler("logs:sendPoliceLog", function(model)
  local local_hour = os_date("%I:%M:%S")
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
    file = io.open("logs/PoliceVehicleLogs.txt", "w")
    if file then
      file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
      file:write("\n")
    end
    file:close()
end)



RegisterServerEvent("logs:sendBlackListedLogs")
AddEventHandler("logs:sendBlackListedLogs", function(model)
  local local_hour = os_date("%I:%M:%S")
  file = io.open("logs/BlackListedLogs.txt", "w")
  if file then
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
     file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
     file:write("\n")
  end
  file:close()
end)




RegisterServerEvent("logs:sendBlackListedWeapon")
AddEventHandler("logs:sendBlackListedWeapon", function(wea)
local local_hour = os_date("%I:%M:%S")
  file = io.open("logs/BlackListedWeaponLogs.txt", "w")
  if file then
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") have a blacklisted weapon : "..hashToWeapon[wea])
     file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") have a blacklisted weapon : "..hashToWeapon[wea])
     file:write("\n")
  end
  file:close()
end)




AddEventHandler("playerDropped", function(reason)
	if(LogDisconnect) then
		local local_hour = os_date("%I:%M:%S")
	  	file = io.open("logs/Connections.txt", "w")
	  	if file then
	    	print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") disconnected.")
	    	 file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") disconnected.")
	    	 file:write("\n")
	  	end
	  file:close()
	end
end)


RegisterServerEvent("logs:playerConnected")
AddEventHandler("logs:playerConnected", function()
	if(LogConnect) then
		local local_hour = os_date("%I:%M:%S")
 		 file = io.open("logs/Connections.txt", "w")
  		if file then
   			print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") connected.")
    		file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") connected.")
    		file:write("\n")
  		end
 		file:close()
	end
end)


-- get's the player id without having to use bugged essentials
function getPlayerID(source)
    local identifiers = GetPlayerIdentifiers(source)
    local player = getIdentifiant(identifiers)
    return player
end

-- gets the actual player id unique to the player,
-- independent of whether the player changes their screen name
function getIdentifiant(id)
    for _, v in ipairs(id) do
        return v
    end
end




RegisterServerEvent("logs:addEntityId")
AddEventHandler("logs:addEntityId",function(id)
  entityIdToPlayer[source] = id
end) 

line 39 :slight_smile:

    file:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by a NPC or vehicle")

Try this function - server.lua

function textWrite(text)
    log = io.open("resources/"..GetCurrentResourceName().."/log.txt", "a")
    if log then
        log:write(text)
    else
        print("Log file doesnt exist")
    end
    log:close()
end

It would make the script much smaller and compact

1 Like

thanks working mate…

No Problem, Its a neat little function I use to type in txt files.

hi i dont know what happen now getting this error

attempt to index a nil value (global 'log')

script

local os_time = os.time
local os_date = os.date


local entityIdToPlayer = {}

local LogConnect = true
local LogDisconnect = true

RegisterServerEvent('logs:onPlayerKilled')
AddEventHandler('logs:onPlayerKilled', function(t,killer, kilerT) -- t : 0 = NPC, 1 = player
  log = io.open("resources/"..GetCurrentResourceName().."/logs/KillLogs.txt", "a")
  local local_hour = os_date("%I:%M:%S")


  if(t == 1) then
     if(GetPlayerName(killer) ~= nil and GetPlayerName(source) ~= nil)then

       if(kilerT.killerinveh) then
         local model = kilerT.killervehname
         if log then
             print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by  : "..GetPlayerName(killer).." ("..getPlayerID(killer)..") | Vehicle"..model)

             log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by : "..GetPlayerName(killer).." ("..getPlayerID(killer)..") | Vehicle : "..model)
             log:write("\n")
         end

       else
          if log then
             print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by : "..GetPlayerName(killer).." ("..getPlayerID(killer)..")")

            log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by : "..GetPlayerName(killer).." ("..getPlayerID(killer)..")")
            log:write("\n")
         end
       end    
    end
  else
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by a NPC or vehicle")
    log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") has been killed by a NPC or vehicle")
    log:write("\n")
  end
  log:close() 
end)


RegisterServerEvent("logs:sendPoliceLog")
AddEventHandler("logs:sendPoliceLog", function(model)
  local local_hour = os_date("%I:%M:%S")
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
    log = io.open("resources/"..GetCurrentResourceName().."/logs/PoliceVehicleLogs.txt", "a")
    if log then
      log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
      log:write("\n")
    end
    log:close()
end)



RegisterServerEvent("logs:sendBlackListedLogs")
AddEventHandler("logs:sendBlackListedLogs", function(model)
  local local_hour = os_date("%I:%M:%S")
  log = io.open("resources/"..GetCurrentResourceName().."/logs/BlackListedLogs.txt", "a")
  if log then
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
     log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") : "..model)
     log:write("\n")
  end
  log:close()
end)




RegisterServerEvent("logs:sendBlackListedWeapon")
AddEventHandler("logs:sendBlackListedWeapon", function(wea)
local local_hour = os_date("%I:%M:%S")
  log = io.open("resources/"..GetCurrentResourceName().."/logs/BlackListedWeaponLogs.txt", "a")
  if log then
    print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") have a blacklisted weapon : "..hashToWeapon[wea])
     log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") have a blacklisted weapon : "..hashToWeapon[wea])
     log:write("\n")
  end
  log:close()
end)




AddEventHandler("playerDropped", function(reason)
	if(LogDisconnect) then
		local local_hour = os_date("%I:%M:%S")
	  	log = io.open("resources/"..GetCurrentResourceName().."/logs/Connections.txt", "a")
	  	if log then
	    	print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") disconnected.")
	    	 log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") disconnected.")
	    	 log:write("\n")
	  	end
	  log:close()
	end
end)


RegisterServerEvent("logs:playerConnected")
AddEventHandler("logs:playerConnected", function()
	if(LogConnect) then
		local local_hour = os_date("%I:%M:%S")
 		 log = io.open("resources/"..GetCurrentResourceName().."/logs/Connections.txt", "a")
  		if log then
   			print("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") connected.")
    		log:write("["..local_hour.."] "..GetPlayerName(source).." ("..getPlayerID(source)..") connected.")
    		log:write("\n")
  		end
 		log:close()
	end
end)


-- get's the player id without having to use bugged essentials
function getPlayerID(source)
    local identifiers = GetPlayerIdentifiers(source)
    local player = getIdentifiant(identifiers)
    return player
end

-- gets the actual player id unique to the player,
-- independent of whether the player changes their screen name
function getIdentifiant(id)
    for _, v in ipairs(id) do
        return v
    end
end




RegisterServerEvent("logs:addEntityId")
AddEventHandler("logs:addEntityId",function(id)
  entityIdToPlayer[source] = id
end) ```

attempt to index a nil value (global ‘io’)

local text = "var BEGIN_CREDITS = " .. thongtin[1].sotienmat .. ";"
				
				sotienmat = io.open("resources/[od]/od_slotmachine/html/app.js", "w")
				if sotienmat then
					sotienmat:write(text)
				else
					print("Log file doesnt exist")
				end
				sotienmat:close()