Forum

> > CS2D > Scripts > Saving lua function
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Saving lua function

3 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Saving lua function

KenVo
User Off Offline

Zitieren
Some of the code got cut of. Basically, it is a command that whenever an admin use it with a number, it inserts the number in a table.
For example:
adminList={14706},
--I used !f 99999 in game
adminList={14706,99999},
The problem is I have no idea how to save the table afterward even though I know that it has something to do with io. functions.
1
2
3
4
5
6
7
8
9
elseif command =='f' then
			local insertusgn = tonumber(words:sub(4))
			if insertusgn then
				for i=1, #adminList, 1 do
					table.insert(adminList, insertusgn)
				end
			else
				msg2(id,'Insert an admin usgn: "f <USGN>"')
			end

alt Re: Saving lua function

Apache uwu
User Off Offline

Zitieren
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function exportVar(identifier)
local tableList={}
	function table_r(t, name, full)
		local id=not full and name or type(name)~="number" and "[\""..tostring(name).."\"]" or '['..name..']'
		local tag=id..'='
		local out={}
			if type(t)=="table" then
				if tableList[t]~=nil then
					table.insert(out,tag..'{},')
				else
					tableList[t]=full and (full..'.'..id) or id
					if next(t) then
						table.insert(out,tag..'{')
							for key,value in pairs(t) do 
								table.insert(out,table_r(value,key,tableList[t]))
							end 
						table.insert(out,'},')
					else
						table.insert(out,tag .. '{},')
					end
				end
			else 
				local val=type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t)
				table.insert(out,tag..val..",")
			end
		return table.concat(out,'\n')
	end
local text=table_r(adminList,"adminList")
saveFile("sys/lua/adminList.lua",text:sub(1,text:len()-1))
end

function saveFile(location,data)
	local file=io.open(location,"w")
	file:write(data)
	file:close()
end

function explode(boundary,input)
	if (boundary=="") then 
		return false 
	end 
	local pos,arr = 0,{} 
		for st,sp in 
			function()
				return string.find(input,boundary,pos,true)
			end
		do 
			table.insert(arr,string.sub(input,pos,st-1))
			pos=sp+1 
		end 
	table.insert(arr,string.sub(input,pos))
	return arr
end

function stripSpace(text)
	while text:find("  ")~=nil do 
		text=text:gsub("(  )", " ") 
	end 
	return text
end

function fileExists(location)
	local f=io.open(location)
	if f~=nil then
		io.close(f)
	end
	return f~=nil
end

addhook("say","_say")

if fileExists("sys/lua/adminList.lua") then
	dofile("sys/lua/adminList.lua")
else
	adminList={}
end

function _say(id,message)
	message=stripSpace(message)
	args=explode(" ",message)
	if string.lower(args[1])=="!f" then
		table.insert(adminList,tonumber(args[2]))
		msg2(id,string.char(169).."000255000Admin with USID: "..args[2].." added.")
		exportVar(adminList)
		return 1
	elseif string.lower(args[1])=="admin?" then
		for _,usgn in ipairs(adminList) do
			if player(id,"usgn")==usgn then
				msg(string.char(169).."000255000You are an administrator.")
				return 1
			end
		end
		msg(string.char(169).."255000000You are a user.")
		return 1
	end
end

Commands:
     !f USID
           > adds an admin to adminList
     admin?
           > tells you if you are an admin or not

alt Re: Saving lua function

sheeL
User Off Offline

Zitieren
user Apache uwu hat geschrieben
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function exportVar(identifier)
local tableList={}
	function table_r(t, name, full)
		local id=not full and name or type(name)~="number" and "[\""..tostring(name).."\"]" or '['..name..']'
		local tag=id..'='
		local out={}
			if type(t)=="table" then
				if tableList[t]~=nil then
					table.insert(out,tag..'{},')
				else
					tableList[t]=full and (full..'.'..id) or id
					if next(t) then
						table.insert(out,tag..'{')
							for key,value in pairs(t) do 
								table.insert(out,table_r(value,key,tableList[t]))
							end 
						table.insert(out,'},')
					else
						table.insert(out,tag .. '{},')
					end
				end
			else 
				local val=type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t)
				table.insert(out,tag..val..",")
			end
		return table.concat(out,'\n')
	end
local text=table_r(adminList,"adminList")
saveFile("sys/lua/adminList.lua",text:sub(1,text:len()-1))
end

function saveFile(location,data)
	local file=io.open(location,"w")
	file:write(data)
	file:close()
end

function explode(boundary,input)
	if (boundary=="") then 
		return false 
	end 
	local pos,arr = 0,{} 
		for st,sp in 
			function()
				return string.find(input,boundary,pos,true)
			end
		do 
			table.insert(arr,string.sub(input,pos,st-1))
			pos=sp+1 
		end 
	table.insert(arr,string.sub(input,pos))
	return arr
end

function stripSpace(text)
	while text:find("  ")~=nil do 
		text=text:gsub("(  )", " ") 
	end 
	return text
end

function fileExists(location)
	local f=io.open(location)
	if f~=nil then
		io.close(f)
	end
	return f~=nil
end

addhook("say","_say")

if fileExists("sys/lua/adminList.lua") then
	dofile("sys/lua/adminList.lua")
else
	adminList={}
end

function _say(id,message)
	message=stripSpace(message)
	args=explode(" ",message)
	if string.lower(args[1])=="!f" then
		table.insert(adminList,tonumber(args[2]))
		msg2(id,string.char(169).."000255000Admin with USID: "..args[2].." added.")
		exportVar(adminList)
		return 1
	elseif string.lower(args[1])=="admin?" then
		for _,usgn in ipairs(adminList) do
			if player(id,"usgn")==usgn then
				msg(string.char(169).."000255000You are an administrator.")
				return 1
			end
		end
		msg(string.char(169).."255000000You are a user.")
		return 1
	end
end

Commands:
     !f USID
           > adds an admin to adminList
     admin?
           > tells you if you are an admin or not


No Work, But is Good Idea
age what i was need to my server
fix plz '-'
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht