Forum

> > CS2D > General > Saving and loading a SINGLE value based on USGN
Forums overviewCS2D overviewGeneral overviewLog in to reply

English Saving and loading a SINGLE value based on USGN

11 replies
To the start Previous 1 Next To the start

old Re: Saving and loading a SINGLE value based on USGN

CmDark
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
end


credits = initArray(32)


addhook("leave","saved_save")
function saved_save(id)
     local usgn = player(id, "usgn")
     if usgn > 0 then
	 if not player(id,"bot") then
          local save_data = credits[id]..
          file = assert(io.open("sys/lua/saved/"..usgn..".txt","w"))
          file:write(save_data)
          file:close()
		end
	end
end

addhook("join","joinsavnumt")
function joinsavnumt(id)
usgn=player(id,"usgn")
	files = io.open("sys/lua/saved/"..usgn..".txt","r")
	if(files~=nil) then
		for line in io.lines("sys/lua/saved/"..usgn..".txt","r") do
		local parses = toTable(line)
		if (tonumber(parses[1])>0) then
		credits[id] = tonumber(parses[1])
		break
		end
	end
end
end


function toTable(t,match)
local cmd = {}
	if not match then     match = "[^%s]+" end
		for word in string.gmatch(t, match) do
		table.insert(cmd, word)
	end
	return cmd
end

Didn't really test it, try anyway

old Re: Saving and loading a SINGLE value based on USGN

Trotskygrad
User Off Offline

Quote
Geh...

My Code:

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
function script.RP.save(id)
	if (player(id,"usgn") > 0) then
		f = assert(io.open('data/'..player(id,"usgn")..'.txt','w'))
		f:write(script.RP.money[id])
		f:close()
		msg2(id,"©000255000Save File Written!@C")
	else
		msg2(id,"©000255000Save Failed!@C")
	end
end

function script.RP.load(id)
	local usgn = player(id,"usgn")
	if (usgn > 0) then
		msg2(id,'Your U.S.G.N. ID: '..usgn..'@C')
		hudtxt2(id,5,'Logged in: #'..usgn,5,410,0)
		f = io.open('data/'..usgn..'.txt','r')
		if (f ~= nil) then
			msg2(id'Your Save File Found!@C')
			local cash = tonumber(f:read("*all"))
			script.RP.transaction(id,cash)
			f:close()
		else
			msg2(id,'Failed To Load Save!@C')
		end
	else
		msg2(id,'Please check your U.S.G.N. account settings!@C')
	end
end

do you really need to use totable?

old Re: Saving and loading a SINGLE value based on USGN

Homer
User Off Offline

Quote
Trotsky, seeing one little error, that might not be it, if a person saves multiple things and multiple times, wouldn't that create more than one value, thus creating a issue when you read everything in the text file?

old Re: Saving and loading a SINGLE value based on USGN

Admir
User Off Offline

Quote
Trotskygrad has written
No, cause when you !save, it overwrites the old value.


You mean it can save new save's and not overwrite old save? that need you make menu for that, or you can also make new say like !save1, !save2, !save3, but need the different places.

old Re: Saving and loading a SINGLE value based on USGN

Patasuss
User Off Offline

Quote
No.
Example:
Player says !save the script saves his cash: 15340$
Then the text file looks like this:
1
15340$
Then he lose money.Now he has 14000$
Now the text file looks like this:
1
14000$
(sorry for bad english)

old Re: Saving and loading a SINGLE value based on USGN

archmage
User Off Offline

Quote
Trotskygrad has written
Geh...

My Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function script.RP.load(id)
	local usgn = player(id,"usgn")
	if (usgn > 0) then
		msg2(id,'Your U.S.G.N. ID: '..usgn..'@C')
		hudtxt2(id,5,'Logged in: #'..usgn,5,410,0)
		f = io.open('data/'..usgn..'.txt','r')
		if (f ~= nil) then
			msg2(id'Your Save File Found!@C')
		[b]	for l in io.lines('data/'..usgn..'.txt') do
			local cash = string.sub(l, 1, string.len(l))[/b]
			script.RP.transaction(id,cash)
			f:close()
		else
			msg2(id,'Failed To Load Save!@C')
		end
	else
		msg2(id,'Please check your U.S.G.N. account settings!@C')
	end
end

that should work
To the start Previous 1 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview