Forum

> > CS2D > Scripts > Save and load money etc.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Save and load money etc.

16 replies
To the start Previous 1 Next To the start

old Save and load money etc.

Mora
User Off Offline

Quote
Hello us
i wanna ask:
can someone give me money/save load <- that i can make it for my script,
ex:

1
2
3
4
5
6
7
8
9
10
11
12
val = {}

unction initArray(m)
local array = {}
for i = 1, m do
array[i]=0
end
return array
end
val.mon=initArray(32)

function val.mon(id)...blaballabba(code i have)
i need save script and load this money, also addons etc...
addons is:

1
2
3
4
5
6
7
abc
def
ghi
jkl
mno
pqr
stu
if you did not understeand pm me...
thanks you so much

old Re: Save and load money etc.

RedizGaming
GAME BANNED Off Offline

Quote
val = {}
local us = player(id,"usgn")

function initArray(m)
local array = {}
for i = 1, m do
array[i]=0
end
return array
end

val.mon=initArray(32)

addhook("join", "val.save")
function val.save(id)
     val.mon[id] = 0
     file=io.open("sys/lua/save/"..us..".txt","w")
     file:write(val.mon[id])
     file:close()
end

addhook("leave","val.load")
function val.load(id)
     val.mon[id] = 0
     file=io.open("sys/lua/savestat/"..us..".txt","r")
     val.mon[id] = file:read("*all")
     file:close()
end

old Re: Save and load money etc.

Mora
User Off Offline

Quote
1
LUA ERROR: sys/lua/4.lua:2: bad argument #1 to 'player' (number expected, got nil)

first 2 lines is

1
2
val = {}
local us = player(id,"usgn")

in your lua has written:
1
2
addhook("join", "val.save")
function val.save(id)
this means when player join money save, how it possible?
1
2
addhook("leave","val.load")
function val.load(id)
this means that when player leave, then money he have load..
Uh??

old Re: Save and load money etc.

Alistaire
User Off Offline

Quote
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
val = {}

function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
end

addhook("join", "val.load")
addhook("leave","val.save")

val.mon=initArray(32)

function val.save(id)
	val.mon[id] = 0
	file=io.open("sys/lua/save/"..player(id, 'usgn')..".txt","w")
	file:write(val.mon[id])
	file:close()
end

function val.load(id)
	val.mon[id] = 0
	file=io.open("sys/lua/savestat/"..player(id, 'usgn')..".txt","r")
	val.mon[id] = file:read("*all")
	file:close()
end

^ Fix.

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Alistaire: my cs2d crush when i start with this script
i see logs and that i see:
1
[10:21:17] LUA ERROR: sys/lua/1.lua:66: attempt to index global 'val' (a nil value)

i rename this to
loadmon, and savemon, but i get this:
1
LUA ERROR: sys/lua/1.lua:76: attempt to index global 'file' (a nil value)

maybe need just make "automake txt. file?"...huh what i talk?
edited 2×, last 03.05.14 08:29:47 am

old Re: Save and load money etc.

Dousea
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
val = {}
val.mon = {}

for id = 1, 32 do
	val.mon[id] = 0
end

addhook ("join", "val.join")
addhook ("leave", "val.leave")

function val.join (id)
	local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".txt", "r")
	
	val.mon[id] = file:read ()
	
	file:close ()
end

function val.leave (id)
	local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".txt", "w+")
	
	file:write (val.mon[id])
	file:close ()
end

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Dousea: thank, is work, but!
When player have an U.S.G.N. then not errors, work fine,
if player join with not usgn then error is
1
LUA ERROR: sys/lua/1.lua:75: attempt to index local 'file' (a nil value)

old Re: Save and load money etc.

Dousea
User Off Offline

Quote
@user Mora:
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
val = {}
val.mon = {}

for id = 1, 32 do
	val.mon[id] = 0
end

addhook ("join", "val.join")
addhook ("leave", "val.leave")

function val.join (id)
	if (player (id, "usgn") > 0) then
		local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".txt", "r")
		
		if (file) then
			val.mon[id] = tonumber (file:read ())
			
			file:close ()
		end
	end
end

function val.leave (id)
	if (player (id, "usgn") > 0) then
		local file = io.open ("sys/lua/save/" .. player  (id, "usgn") .. ".txt", "w+")
		
		file:write (val.mon[id])
		file:close ()
		
		val.mon[id] = 0
	end
end
edited 2×, last 03.05.14 01:20:09 pm

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Dousea: thank you, how it can be customized for addons?
example i have 3:
addon1 = {}
addon2 = {}
addon3 = {}
can this lua save it?
1
2
3
if addon1[id]=true (this mean if i have it) then
 image(id,"gfx blalbabla")
else msg2(id,"you dant have this addon, loser")

when join:
1
LUA ERROR: sys/lua/1.lua:95: attempt to concatenate field '?' (a nil value)
when leave
1
LUA ERROR: sys/lua/1.lua:88: bad argument #1 to 'write' (string expected, got nil)

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Dousea: yeah,lines is (88 to 95 that get error):

1
2
3
4
5
6
7
8
function val.leave (id)
     if (player (id, "usgn") > 0) then
          local file = io.open ("sys/lua/save/" .. player  (id, "usgn") .. ".txt", "r")
          
          file:write (val.mon[id])
          file:close ()
     end
end

old Re: Save and load money etc.

Dousea
User Off Offline

Quote
@user Mora: It looks fine for me but already fixed the code. About add-ons things.
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
addons = {}

for id = 1, 32 do
	addons[id] = {}
	
	for addon = 1, 3 do
		addons[id][addon] = 0
	end
end

addhook ("leave", "leavehook")
addhook ("join", "joinhook")

function leavehook (id)
	if (player (id, "usgn") > 0) then
		local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".cfg", "w+")
		
		file:write (addons[id][1] .. " " .. addons[id][2] .. " " .. addons[id][3])
		file:close ()
		
		for addon = 1, 3 do
			addons[id][addon] = 0
		end
	end
end

function joinhook (id)
	if (player (id, "usgn") > 0) then
		local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".cfg", "r")
		
		local values = {}
		
		for value in file:read ():gmatch ("[^%s]+") do
			table.insert (values, value)
		end
		
		for addon = 1, 3 do
			addons[id][addon] = tonumber (values[addon])
		end
	end
end
Use 0 for false and 1 for true. Don't use true or false, if you want to make add-on 2 to true for example change its variable's value to 1 then if otherwise (false) use 0. Keep that in mind if you use this code as guide.

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Dousea: nonono, i meant addons is like:
Menu addons,Wings|500k,Terminator|1m,Death|5m.
if i buy one of them, then my file save it, and when i press on it, i got images i set
wings
sethealth 105
Terminator
sethealth 125
Death
sethealth 150,
but not about rename val.mon to addons.
example is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
wing = {}
terminator = {}
death = {}

addhook("menu","men")
function men(id,t,b)
 if t=="addons" then
  if b==1 then
   if wing(id,1) then
    image(gfx bllbabla)
   elseif player val.mon[id]=val.mon[id] > 49999 then
   wing = 1
   else msg2(id,"©255000000You have not enouch Money!")
    end
   end
  end
end
also your code work, but! if file does not exists, example in folder "save" dont have any txt files, then i got error, if someone player join i got error, bcs him usgn txt doest not exists, after moment file.txt added, and money saved.
edited 2×, last 03.05.14 02:52:44 pm

old Re: Save and load money etc.

Dousea
User Off Offline

Quote
@user Mora: I don't really understand you but if you meant like 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
addons = {
	{"Wings", 50000},
	{"Terminator", 100000},
	{"Death", 150000}
}

val.addon = {}

for id = 1, 32 do
	val.addon[id] = {}
	
	for addon = 1, #addons do
		val.addon[id][addon] = 0
	end
end

function loadaddons (id)
	local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".txt", "r")
	
	if (file) then
		local values = {}
		
		for value in file:read ():gmatch ("[^%s]+") do
			table.insert (values, value)
		end
		
		for addon = 1, #addons do
			val.addon[id][addon] = values[addon]
		end
	end
end

function saveaddons (id)
	local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".txt", "w+")
	local content = ""
	
	for addon = 1, #addons do
		content = content .. val.addon[id][addon] .. " "
	end
	
	file:write (content)
	file:close ()
end

function getaddonsmenu (id)
	local field = ""
	
	for key, addon in ipairs (addons) do
		field = field .. addon[1] .. "|$" .. addon[2] .. ","
	end
	
	menu (id, "Add-ons," .. field)
end
It uses the same condition, 0 for false and 1 for true. Use this function in your script. Use loadaddons to load saved add-ons, saveaddons to save current add-ons, getaddonsmenu to show a menu contains add-ons.

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Dousea: know you roleplay mod`s? i mean addons like in.
and one, dont make script before you know this, because it take your own free time.

old Re: Save and load money etc.

Mora
User Off Offline

Quote
@user Dousea: yes. but! dont make script now better i send you code in PM?(full code)
also, your code UP is work now, thank you so much, best))
edited 1×, last 03.05.14 05:09:43 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview