i am new to lua. actually this script use for game server but i need help for getting the value from my store .txt file. so my store system is each players will be save on one file.
Store File
my syntax data from text file
[name]:[points]:[which var point earned on index]
my example data (this var point actually the written data from lua file that sended to text file)
jack:60:4
ronald:40:1
jack:30:1
so when i got from above is the list of
local player = {{(name),(points),(which var point earned on index)},
{(name),(points),(which var point earned on index)},...};
My own code
local line_data = {}
local point_file = io.open("points.txt", "r")
for line in point_file:lines() do
local playername, playerpoint, playervarpointname = line:match("(%d+):(%d+):(%d+)")
local player_data = {playername, playerpoint, playervarpointname}
for i = 1, #line do
player_data = [#player_data + 1] = line[i];
end
line_data[#line_data + 1] = player_data
end
is this the correct code ?
local line_data = {}
local point_file = io.open("points.txt", "r")
for line in point_file:lines() do
local playername, playerpoint, playervarpointname = line:match("([^:]+):(%d+):(%d+)")
local player_data = {playername, playerpoint, playervarpointname}
table.insert(line_data, player_data)
end
Or , imho, '[a-zA-Z0-9]+ ' , may be used instead of '[^:]+'
Related
So basically, the script is supposed to load text after the intro screen disappears, not sure what's wrong with it because it worked back in 2014 :/
The Code:
if true then
script.Parent.slowsleigh:Play()
fadegui({intro.load},{0},{1},30)
wait(1)
local titles = intro.titles
local text1 = titles.title1.Text
local text2 = titles.title2.Text
local text3 = titles.title3.Text
local text4 = titles.title4.Text
if GameData.entered == 1 then
text1 = "And you came back."
text2 = "You can't change the past."
text3 = "Pathetic."
end
titles.title1.Text = ""
titles.title2.Text = ""
titles.title3.Text = ""
titles.title4.Text = ""
--[
titles.title1.Visible = true
for i = 1, #text1 do
titles.title1.Text = string.sub(text1,1,i)
wait(0.05 + math.random(-5,10)/200)
end
And heres the script inside of ServerScriptServices
local data = game:GetService("DataStoreService"):GetGlobalDataStore()
local dataVersion = 2
local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
local store = data:GetAsync(tostring(player.UserId))
return store
end
local set = Instance.new("RemoteEvent",workspace)
set.Name = "SetData"
set.OnServerEvent:connect(function(player,newData)
local store = data:GetAsync(tostring(player.UserId))
for i,v in pairs(newData) do
store[i] = v
end
data:SetAsync(player.UserId, store)
end)
The Error:
17:48:57.908 Players.Trl0_P90.PlayerGui.maingui.mainscript:758: attempt to index nil with 'entered' - Client - mainscript:758
The error is telling you that you are trying to access GameData.entered, when GameData is nil.
From the code you have shared, your issue appears to be that you are accessing data from the data store and not accounting for the fact that it might not exist. Take a look at the docs for GlobalDataStore:GetAsync() :
This function returns the latest value of the provided key and a DataStoreKeyInfo instance. If the key does not exist or if the latest version has been marked as deleted, both return values will be nil.
This could be caused by a new player joining, and there being no default data. So, one thing you could do is set up some default data in case there's a new player:
local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
local store = data:GetAsync(tostring(player.UserId))
-- if there is no data, it might be a new player!
-- provide some new player data
if not store then
store = {
entered = 1,
foo = true,
bar = "blah",
}
end
return store
end
This way, the function will always return the correctly shaped data.
I'm trying to get the name of all the file saved in two folders, the name are saved as :
1.lua 2.lua 3.lua 4.lua and so on
the folders name are :
first folder : "/const/"
second folder: "/virt/"
what I'm trying to do is only get the number of the files and this works but not in the right order, when I get the 17 file for example I get the 17th delivered from the function before the 15 and this causes for me a problem here the code of the function that I'm using :
local virt_path = "/virt/"
local const_path = "/const"
local fs = require "lfs"
local const = {}
for num = 1, (numberoffile)do -- numberoffile is predfined and can't be change
const[num] = assert(
dofile (const_path .. mkfilename(num)),
"Failed to load constant ".. num ..".")
end
local function file_number() --this is the function that causes me a headach
local ci, co, num = ipairs(const)
local vi, vo, _ = fs.dir(virt_path)
local function vix(o)
local file = vi(o)
if file == nil then return nil end
local number = file:match("^(%d+).lua$")
if number == nil then return vix(o) end
return tonumber(number)
end
local function iter(o, num)
return ci(o.co, num) or vix(o.vo, num)
end
return iter, {co=co, vo=vo}, num
end
As I said the function delive the need return values but not the right Arithmetic order.
any idea what I'm doing wrong here ?
I use my path[1] library.
1 We fill table with filenames
local t = {}
for f in path.each("./*.lua", "n") do
t[#t + 1] = tonumber((path.splitext(f)))
end
table.sort(t)
for _, i in ipairs(t) do
-- do work
end
2 We check if files exists
for i = 1, math.huge do
local p = "./" .. i .. ".lua"
if not path.exists(p) then break end
-- do work
end
[1] https://github.com/moteus/lua-path
I have a lot of variables that seem to lend themselves to be referenced a lot easier, but I'm not sure how to do it; the variable names are related to entries on a coordinate system, and where they are written to is also coordinate based. I have tried searching google, I've gone through the Lua documentation, but as I'm not sure what I'm looking for I think that is hindering my search. Here is how the variables look in an excerpt of my code:
-- Bank 1
local Object111 = sItem.getTargetDetails("-4,3,-4")
local Object112 = sItem.getTargetDetails("-5,3,-4")
local Object113 = sItem.getTargetDetails("-6,3,-4")
local Object114 = sItem.getTargetDetails("-7,3,-4")
local Object115 = sItem.getTargetDetails("-8,3,-4")
local Object121 = sItem.getTargetDetails("-4,4,-4")
local Object122 = sItem.getTargetDetails("-5,4,-4")
local Object123 = sItem.getTargetDetails("-6,4,-4")
local Object124 = sItem.getTargetDetails("-7,4,-4")
local Object125 = sItem.getTargetDetails("-8,4,-4")
local Object131 = sItem.getTargetDetails("-4,5,-4")
local Object132 = sItem.getTargetDetails("-5,5,-4")
local Object133 = sItem.getTargetDetails("-6,5,-4")
local Object134 = sItem.getTargetDetails("-7,5,-4")
local Object135 = sItem.getTargetDetails("-8,5,-4")
-- Bank 2
local Object211 = sItem.getTargetDetails("-4,3,1")
local Object212 = sItem.getTargetDetails("-5,3,1")
local Object213 = sItem.getTargetDetails("-6,3,1")
local Object214 = sItem.getTargetDetails("-7,3,1")
local Object215 = sItem.getTargetDetails("-8,3,1")
local Object221 = sItem.getTargetDetails("-4,4,1")
local Object222 = sItem.getTargetDetails("-5,4,1")
local Object223 = sItem.getTargetDetails("-6,4,1")
local Object224 = sItem.getTargetDetails("-7,4,1")
local Object225 = sItem.getTargetDetails("-8,4,1")
local Object231 = sItem.getTargetDetails("-4,5,1")
local Object232 = sItem.getTargetDetails("-5,5,1")
local Object233 = sItem.getTargetDetails("-6,5,1")
local Object234 = sItem.getTargetDetails("-7,5,1")
local Object235 = sItem.getTargetDetails("-8,5,1")
I would then proceed to call each of these variables in a function, where the position on screen is a function of the numbers in the name, the first two numbers relating to x coords and the last number to the y coords:
mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
etc....
I can see that this should be able to be generated on the fly, but I don't know where to start without calling it out manually; I would prefer it if my code was cleaner. At this stage I really just need to be taught how to fish; if anyone can point me to documents that explain how to do what I'm trying to I would be most grateful.
Here's my final solution for anyone trying to achieve same:
local Banks = 2
local Rows = 3
local Columns = 5
function Objectstatus(bank,row,column)
Id = bank .. row .. column
worldx = "-" .. column+3
worldy = row+2
if bank == 1 then worldz = -4; end
if bank == 2 then worldz = 1; end
Object = {}
Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz)
xcursor = (column*7)+3
if bank == 1 then
ycursor = (row*4)+8
end
if bank == 2 then
ycursor = (row*4)+24
end
mon.setCursorPos(xcursor,ycursor)
powertext(Object[Id].StoredPercentage) -- A function that sets Texts settings based on result
mon.write(Object[Id].StoredPercentage) -- Actually writes the result
end
for BankTemp = 1, Banks do
for ColumnTemp = 1, Columns do
for RowTemp = 1, Rows do
Objectstatus(BankTemp,RowTemp,ColumnTemp)
end
end
end
Json parsing is not working on devices while works perfect on simulator, that scene could not be open where I read or write some data.
I have made a class for Json parsing
Here it is,
module(..., package.seeall)
local json=require ("json")
function saveTable(t,filename)
local path = system.pathForFile(filename, system.DocumentsDirectory)
local file = io.open(path,"w")
if file then
local contents = json.encode(t)
file:write(contents)
io.close(file)
return true
else
return false
end
end
function loadTable(filename)
local path = system.pathForFile(filename, system.DocumentsDirectory)
local contents = ""
local myTable = {}
local file = io.open(path,"r")
if file then
local contents = file:read("*a")
myTable = json.decode(contents)
io.close(file)
return myTable
end
return nil
end
I actually want an alternative of NSUserDefaults on Corona.
Any help?
Yea, I got it, I used to loadTable first, without saving table, so it would be a problem, never call loadTable before save table :)
I have 2 functions in Lua which create a dictionary table and allow to check if a word exists:
local dictTable = {}
local dictTableSize = 0
function buildDictionary()
local path = system.pathForFile("wordlist.txt")
local file = io.open( path, "r")
if file then
for line in file:lines() do
dictTable[line] = true
dictTableSize = dictTableSize + 1
end
io.close(file)
end
end
function checkWord(word)
if dictTable[word] then
return(true)
else
return(false)
end
end
Now I want to be able to generate a couple of random words. But since the words are the keys, how can I pick some, given the dictTableSize.
Thanks
Just add a numerical index for each word to the dictionary while loading it:
function buildDictionary()
local path = system.pathForFile("wordlist.txt")
local file = io.open( path, "r")
if file then
local index = 1
for line in file:lines() do
dictTable[line] = true
dictTable[index] = line
index = index + 1
end
io.close(file)
end
end
Now you can get a random word like this:
function randomWord()
return dictTable[math.random(1,#dictTable)]
end
Side note: nil evaluates to false in Lua conditionals, so you could write checkWord like this:
function checkWord(word)
return dictTable[word]
end
Another side note, you'll get less polution of the global namespace if you wrap the dictionary functionality into an object:
local dictionary = { words = {} }
function dictionary:load()
local path = system.pathForFile('wordlist.txt')
local file = io.open( path, 'r')
if file then
local index = 1
for line in file:lines() do
self.words[line] = true
self.words[index] = line
index = index + 1
end
io.close(file)
end
end
function dictionary:checkWord(word)
return self.words[word]
end
function dictionary:randomWord()
return self.words[math.random(1,#self.words)]
end
Then you can say:
dictionary:load()
dictionary:checkWord('foobar')
dictionary:randomWord()
Probably two ways: you can keep the array with words and just do words[math.random(#words)] when you need to pick a random word (just make sure that the second one is different from the first).
The other way is to use next the number of times you need:
function findNth(t, n)
local val = next(t)
for i = 2, n do val = next(t, val) end
return val
end
This will return b for findNth({a = true, b = true, c = true}, 3) (the order is undefined).
You can avoid repetitive scanning by memoizing the results (at this point you will be better off using the first way).
this is a trade off that you have for using the word table the way you are. i would invert the word table once you load it, so that you can get references to words by index as well if you have to. something like this:
-- mimic your dictionary structure
local t = {
["asdf"] = true, ["wer"] = true, ["iweir"] = true, ["erer"] = true
}
-- function to invert your word table
function invert(tbl)
local t = {}
for k,_ in pairs(tbl) do
table.insert(t, k)
end
return t
end
-- now the code to grab random words
local idx1, idx2 = math.random(dictTableSize), math.random(dictTableSize)
local new_t = invert(t)
local word1, word2 = new_t[idx1], new_t[idx2]
-- word1 and word2 now have random words from your 'dictTable'