[Help] New Lua User. LoadResourceFile "File not found"

Hello all,

Recently started playing with lua, running into an issue getting a text file I have made to load into my script. Spend a couple of hours searching forums before posting. I couldn’t find anything that specifically helps this issue.

My Code:

My resource .lua clearly showing I am adding my “skins.txt” file into the resource.

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

resource_type 'gametype' { name = 'My awesome game type!' }

files {
    'config/skins.txt'
}


client_script 'mymode_client.lua'


My unfinished client script. I plan on adding random choice of skin. But getting stuck actually loading my “skins.txt” into an array I can start using.

local spawnPos = vector3(-1150.93, -1977.32, 13.16)

local file = LoadResourceFile(GetCurrentResourceName(),"./config/skins.txt")
local spawnskins = {}
local i = 0

if file then
    for line in file:lines() do
     i = i + 1
     spawnskins[i] = line
    end
    file:close()
else
    error('file not found')
end

AddEventHandler('onClientGameTypeStart', function()
    exports.spawnmanager:setAutoSpawnCallback(function()
        exports.spawnmanager:spawnPlayer({
            x = spawnPos.x,
            y = spawnPos.y,
            z = spawnPos.z,
            model = 'a_m_m_skater_01'
        })
    end)

    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end)

It’s now driving me nuts due to how simple im guessing the fix must be, and i simply can’t find the solution.

Picture to show the formatting of skins.txt

Ok…
No Idea what’s the problem but I only understand that you’re stuck on the txt file, but why isn’t explained here. Does it Crash your game, did you get a error back, does whatever should doesn’t show up. Please describe it a little bit more.

The LoadResourceFile doesn’t find my skins.txt

If I try to print “file” from this section in my client script.

local file = LoadResourceFile(GetCurrentResourceName(),"./config/skins.txt")
local spawnskins = {}
local i = 0

It reports back nil.
What am I doing wrong?

For more context. This is what the console spits back out.

Make sure that your txt file is located in the current resource. Do put it in your config folder and make sure that the file is exactly named skins.lua
Also

I don’t understand why there is a . at the beginning.

And I think that

Is Not needed (as I know) try delete and retry then.

For simiplicites sake i’ve taken it out of the folder, as it isn’t needed.

local file = LoadResourceFile(GetCurrentResourceName(),"skins.txt")

No changes, with your suggestions. Same response in console.

From the searching I did before posting, I found that as a quick way to get the reference to the resource im using. As the Native requires it.

or should I say…It should, if it worked.

EDIT
Restarted my FiveM & server, and now finding a different issue.
Here

I’ll go for a search now and try get to the bottom of that.

Any reason why that error has changed with restarting? I thought restarting my script from server cmd was good enough?

You need to run refresh when you make changes to __resource.lua. The error you are hitting now is because LoadResourceFile returns the contents as a string, not a file handle. You want to do this instead.

if file then
    for line in file:gmatch("[^\r\n]+") do
     i = i + 1
     spawnskins[i] = line
    end
else
    error('file not found')
end

Thank you so much. It worked.

Also didn’t know about the refresh thing. Thanks again

Now to go read up on gmatch and regex commands.

Many thanks guys.

Solved

The refresh is always needed If you’re adding new files to the folder and you’ve changed the __resource file.
The __resource file get’s only loaded as long your resource is not already in the cache of the server.
That’s why you need that refresh function.