Forum

> > CS2D > Scripts > Save/Load points
Forums overviewCS2D overview Scripts overviewLog in to reply

English Save/Load points

18 replies
To the start Previous 1 Next To the start

old Re: Save/Load points

Infinite Rain
Reviewer Off Offline

Quote
I think that I'm the only one who noticed it, but I think that Starkkz join unreal software only to upload his scripts/apps or to brag them in threads like this. I might be wrong tho.

(Don't take it as a troll post)

old Re: Save/Load points

Avo
User Off Offline

Quote
user Infinite Rain has written
I think that I'm the only one who noticed it, but I think that Starkkz join unreal software only to upload his scripts/apps or to brag them in threads like this. I might be wrong tho.


Actually user Starkkz's files are good and there are no doubts (I think), but I don't think that user superpegamento needs so complicated script as user Starkkz posted. In f.a there are lots of easy scripts that user superpegamento would use.

old Re: Save/Load points

Starkkz
Moderator Off Offline

Quote
@user Infinite Rain: Don't be jealous, I make this stuff so the people can solve their problems quickly. Maybe he has more things to save than just points, that's why I share it to him.

On-topic: I would like to know if the file serves or not.

old Re: Save/Load points

superpegamento
User Off Offline

Quote
user Starkkz has written
@user Infinite Rain: Don't be jealous, I make this stuff so the people can solve their problems quickly. Maybe he has more things to save than just points, that's why I share it to him.

On-topic: I would like to know if the file serves or not.


hi Starkkz guide me with your codes , but a problem not load data.. that could be?

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
30
31
32
33
34
35
36
37
38
local points = {}

addhook("join", "point_reset")
addhook("kill","point_increase")
addhook("say" , "points")
addhook("leave","points_save")
addhook("join" , "loadpoints")

function point_reset(id) points[id] = 0 point_update(id) end

function point_increase(id)
     if not points[id] then points[id] = 0 return end
     points[id] = points[id] + 1
     point_update(id)
end
function points(id,msg)
               if msg=="!point" then
              msg2(id, "©252000000"..player(id,"name").." ©255075000Your Points:©000100255 " .. points[id] or 0 .. "")
      end
end

function point_update(id)
end

function points_save(id)
     if (player(id,"usgn")>0) then
          io.output(io.open("sys/lua/saves/ranking/"..player(id,"usgn")..".txt","w+"))
          io.write(points[id])
          io.close()
     end
end

function loadpoints(id)
	if (player(id,"usgn")>0) then
		local filename = "sys/lua/saves/ranking/%s.txt"
		local file = io.open(filename:format(player(id,"usgn"), "r"))
end
end

old Re: Save/Load points

Starkkz
Moderator Off Offline

Quote
On the load function you have to use file:read("*a"), it will return the points as a string, after that you will have to use the tonumber function to convert it into a number.

old Re: Save/Load points

superpegamento
User Off Offline

Quote
user Starkkz has written
On the load function you have to use file:read("*a"), it will return the points as a string, after that you will have to use the tonumber function to convert it into a number.


please code example...

in this way?
1
2
line = file:read("*a"):split()
points[id] = tonumber(line[1]) or 0

old Re: Save/Load points

Starkkz
Moderator Off Offline

Quote
@user superpegamento: You don't need to use the split function in this case, but it will work anyway if you added it to your script. If you didn't, it won't work.

old Re: Save/Load points

Shawni
User Off Offline

Quote
user Infinite Rain has written
I think that I'm the only one who noticed it, but I think that Starkkz join unreal software only to upload his scripts/apps or to brag them in threads like this. I might be wrong tho.

(Don't take it as a troll post)

I think people noticed that but you didn't, Everyone here brag his uploads.

old Re: Save/Load points

superpegamento
User Off Offline

Quote
user Starkkz has written
@user superpegamento: You don't need to use the split function in this case, but it will work anyway if you added it to your script. If you didn't, it won't work.


Ok.
This hook will work then?

1
2
3
4
5
6
7
8
function loadpoints(id)
	if (player(id,"usgn")>0) then
		local filename = "sys/lua/saves/ranking/%s.txt"
		local file = io.open(filename:format(player(id,"usgn"), "r"))
		line = file:read("*a")
		points[id] = tonumber(line[1]) or 0
	end
end

old Re: Save/Load points

Starkkz
Moderator Off Offline

Quote
I suggest you to put local when defining the line variable, change line[1] to line, and finish it puting file:close().

Edit: change this
1
local file = io.open(filename:format(player(id,"usgn"), "r"))

To this:
1
local file = io.open(filename:format(player(id,"usgn")), "r")
edited 1×, last 11.07.13 08:17:15 pm

old Re: Save/Load points

superpegamento
User Off Offline

Quote
user Starkkz has written
I suggest you to put local when defining the line variable, change line[1] to line, and finish it puting file:close().

Edit: change this
1
local file = io.open(filename:format(player(id,"usgn"), "r"))

To this:
1
local file = io.open(filename:format(player(id,"usgn")), "r")


Ok..but still not working

here is the 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 

local points = {}

addhook("join", "point_reset")
addhook("kill","point_increase")
addhook("say" , "puntos")
addhook("leave","save_leave")
addhook("join" , "loadpoints")

function point_reset(id) points[id] = 0 point_update(id) end

function point_increase(id)
     if not points[id] then points[id] = 0 return end
     points[id] = points[id] + 1
     point_update(id)
end
function puntos(id,msg)
               if msg=="!point" then
              msg2(id, "©252000000"..player(id,"name").." ©255075000Your Points:©000100255 " .. points[id] or 0 .. "")
      end
end

function point_update(id)
end

function save_leave(id)
     if (player(id,"usgn")>0) then
          local file = (io.open("sys/lua/saves/ranking/"..player(id,"usgn")..".txt","w+"))
          file:write(points[id])
          file:close()
     end
end

function loadpoints(id)
	if (player(id,"usgn")>0) then
		local filename = "sys/lua/saves/ranking/%s.txt"
		local file = io.open(filename:format(player(id,"usgn")), "r")
		line = file:read("*a")
		points[id] = tonumber(line) or 0
                file:close()
	end
end

old Re: Save/Load points

superpegamento
User Off Offline

Quote
user Starkkz has written
Does it give you errors?

not get errors the problem is that the data is not loaded when you enter the server, for example
1 point for kill a user - is stored in the folder saves/ranking
then exit and enter on server have 0 points - points not charged ... Try in your server

old Re: Save/Load points

Starkkz
Moderator Off Offline

Quote
On line 21 remove return, on line 36 change w+ to w, on line 46 change *a to *l

old thenks

superpegamento
User Off Offline

Quote
user Starkkz has written
On line 21 remove return, on line 36 change w+ to w, on line 46 change *a to *l


thank you very much!
the problem was the hook

1
2
3
4
5
6
7
8
addhook("kill","point_increase")
function point_reset(id) points[id] = 0 point_update(id) end

function point_increase(id)
     if not points[id] then points[id] = 0 return end
     points[id] = points[id] + 1
     point_update(id)
end

is replaced for

1
2
3
4
addhook("kill" , "point_increase")
function point_increase(id)
points[id] = points[id] + 1
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview