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