Forum

> > CS2D > Scripts > load hats system
Forums overviewCS2D overview Scripts overviewLog in to reply

English load hats system

13 replies
To the start Previous 1 Next To the start

old load hats system

j0kER aR
User Off Offline

Quote
Hello everyone,

someone can help me create a lua so that the hats are saved? something that works with this lua of hats.

to be stored in a folder and when the person enters, have that hat

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
hats = {}
hats.hooks = {"serveraction","menu","startround"}
hats.items = {
     ["Angel"] = {view = "2D", path = "angel.png"}, -- don't forget the commas
     ["Devil"] = {view = "2D", path = "devil.png"},
     ["Gandalf"] = {view = "2D", path = "gandalf_hat.png"},
     ["Graduation"] = {view = "2D", path = "graduation_hat.png"},
     ["Party"] = {view = "2D", path = "party_hat.png"},
     ["Pirate"] = {view = "2D", path = "pirate_hat.png"},
     ["Pumpkin"] = {view = "2D", path = "pumpkin_head.png"},
     ["Santa"] = {view = "2D", path = "santa_hat.png"},
     ["Snowman"] = {view = "2D", path = "snowman.png"},
     ["Spear"] = {view = "2D", path = "spear.png"}
}
--hats.count = #hats.items
hats.counter = 0
hats.menus = {}
hats.player = {}

for k,_ in pairs (hats.items) do
     hats.counter = hats.counter + 1
     hats.items[k].ID = hats.counter
end
hats.count = hats.counter

for _,hook in pairs (hats.hooks) do
     addhook(hook,"h_"..hook)
end 

if hats.count <= 9 then
     hats.menus[1] = "Hats Menu"
     for k,v in pairs (hats.items) do
               hats.menus[1] = hats.menus[1] .. "," .. k .. "|" .. v.view
     end
elseif hats.count > 9 then
     local index = 1
     local counter = 0
     hats.menus[index] = "Hats Menu ["..index.."]"
     for k,v in pairs (hats.items) do
          hats.menus[index] = hats.menus[index] .. "," .. k .. "|" .. v.view
          counter = counter + 1
          if counter == 7 then
               if index == 1 then
                    hats.menus[index] = hats.menus[index] .. ",(Previous Page),Next Page"
               else
                    hats.menus[index] = hats.menus[index] .. ",Previous Page,Next Page"
               end
                    counter = 0
                    index = index + 1
                    hats.menus[index] = "Hats Menu ["..index.."]"
          end
     end
     index = nil
     counter = nil
end

function h_serveraction(id, b)     
     if b == 1 then
          menu(id,hats.menus[1])
     end
end

function findHat(ID)
     for k,v in pairs (hats.items) do
          if v.ID == ID then
               return k
          end
     end
end

function removeHat(id)
     if hats.player[id] then
          freeimage(hats.player[id])
          hats.player[id] = nil
     end
end

function h_menu(id, t, b)
     if t == "Hats Menu" then
          removeHat(id)
          hats.player[id] = image("gfx/hat/"..hats.items[findHat(b)].path,1,1,200+id)
     elseif t:find("Hats Menu") then
          local page = t:sub(12,12)
          if tonumber(t:sub(12,13)) then -- in case of more than single digit page amount
               page = page .. t:sub(12,13)
          end
          page = tonumber(page)
          local ID = page*7+b-7
          if b <= 7 then
               removeHat(id)
               hats.player[id] = image("gfx/hat/"..hats.items[findHat(ID)].path,1,1,200+id)
          elseif b == 8 then
               menu(id,hats.menus[page-1])
          elseif b == 9 then
               menu(id,hats.menus[page+1])
          end
          ID = nil
          page = nil
     end
end

function h_startround()
     for _,id in pairs (player(0,"tableliving")) do
          removeHat(id)
     end
end

hats.count = nil
hats.counter = nil
hats.hooks = nil

this lua was creator by user Rainoth

old Re: load hats system

Gaios
Reviewer Off Offline

Quote
Do you want to automaticaly load hats from folder to script without config file?

old Re: load hats system

j0kER aR
User Off Offline

Quote
what I want is something 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
addhook("leave","savedata")
function savedata(id,reason)
     if player(id,"usgn") ~= 0 then
          local f = assert(io.open("sys/lua/hats/saves/"..player(id,"usgn")..".txt","w"))
          f:write(currenthat[id])
          f:close()
     end
     if hatimg[id] then
          freeimage(hatimg[id])
     end
     hatimg[id] = nil
     currenthat[id] = nil
end

addhook("join","loaddata")
function loaddata(id)
     local loaded = false
     if player(id,"usgn") ~= 0 then
          local f = io.open("sys/lua/hats/saves"..player(id,"usgn")..".txt","r")
          if f ~= nil then
               for line in f:lines() do
                    currenthat[id] = tonumber(line)
                    break
               end
               loaded = true
               f:close()
          end
     end
     if not loaded then
          currenthat[id] = 0
     end
end

and that's from this lua of hats.

hats lua >

old Re: load hats system

Gaios
Reviewer Off Offline

Quote
Ahaa.. you want to auto-load a last used hat. Can you put all scripts that you use, here? Don't forget about spoiler tag!

old Re: load hats system

cs2d_is_a_Gem
User Off Offline

Quote
Could someone point out the reason for the error in this script? I for more than I read the script I see everything in order.

old Re: load hats system

cs2d_is_a_Gem
User Off Offline

Quote
Wtf, only unnecessary comments that do not help.
I know that this type of code in particular is a little complicated, any scriptwriter that accepts the challenge?
apparently the code contains no error but there are errors to the next round the hats are deleted.

Edit: @user Yates: why you tag that code to create hats that nobody will use?

old Re: load hats system

j0kER aR
User Off Offline

Quote
user phalenkO, yes :]

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
admins = {142925}

addhook ("serveraction","admin_f2")
function admin_f2(id,action)
for _, usgn in ipairs(admins) do
if player(id,"usgn") == usgn then
if action==1 then
menu(id,"Admin Hats,Hats|>")
end
end

addhook("menu", "admin_hats")
function admin_hats(id,title,button)
if title == "Admin Hats" then 
if button == 1 then callmenu(id, 1) return 1 end end end end end

hattable = {
     {name = "Devil|>", path = "gfx/hat/devil.png"},
     {name = "Skull|>", path = "gfx/hat/skull.png"},
	 {name = "CT|>", path = "gfx/hat/Soldado ct.png"},
	 {name = "TT|>", path = "gfx/hat/Soldado tt.png"},
	 {name = "C.America|>", path = "gfx/hat/captain america hat by spike.png"},
}


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

currenthat = initArray(32)
hatimg = {}

function callmenu(id,page)
     local pages = math.ceil(#hattable/7)
     local output = "Hat Menu Page "..page
     local p = pages * 7
     for a = p-6, p do
          if hattable[a] then
               if (hattable[a].name and hattable[a].path) then
                    if a ~= currenthat[id] then
                         output = output..","..hattable[a].name
                    else
                         output = output..",("..hattable[a].name
                    end
               else
                    output = output..","
               end
          else
               output = output..","
          end
     end
     if page == 1 then
          if currenthat[id] ~= 0 then
               output = output..",No hat"
          else
               output = output..",(No hat"
          end
     else
          output = output..",Back Page |Page "..page-1
     end
     if page == pages then
          if currenthat[id] ~= 0 then
               output = output..",No hat"
          else
               output = output..",(No hat"
          end
     else
          output = output..",Back Page |Page "..page+1
     end
     menu(id,output)
end

addhook("menu","hat_menu")
function hat_menu(id,title,button)
     if string.sub(title,1,14) == "Hat Menu Page " then
          local page = tonumber(string.sub(title,15))
          local pages = math.ceil(#hattable/7)
          if button >= 1 and button <= 7 then
               local pr7 = (page - 1) * 7
               local rsel = pr7 + button
               if currenthat[id] ~= rsel then
                    if hatimg[id] then
                         freeimage(hatimg[id])
                    end
                    currenthat[id] = rsel
                    hatimg[id] = image(hattable[rsel].path,1,1,200+id)
               end
          elseif button == 8 then
               if page == 1 then
                    if hatimg[id] then
                         freeimage(hatimg[id])
                    end
                    hatimg[id] = nil
                    currenthat[id] = 0
               else
                    callmenu(id,page-1)
               end
          elseif button == 9 then
               if page == pages then
                    if hatimg[id] then
                         freeimage(hatimg[id])
                    end
                    hatimg[id] = nil
                    currenthat[id] = 0
               else
                    callmenu(id,page+1)
               end
          end
     end
end

addhook("die","hat_die")
function hat_die(victim)
     if hatimg[victim] then
          freeimage(hatimg[victim])
     end
     hatimg[victim] = nil
end


  
addhook("leave","savedata")
function savedata(id,reason)
     if player(id,"usgn") > 0 then
          local f = assert(io.open("sys/lua/hats/saves/"..player(id,"usgn")..".txt","w"))
          f:write(tostring(currenthat[id]))
          f:close()
     end
     if hatimg[id] then
          freeimage(hatimg[id])
     end
     hatimg[id] = nil
     currenthat[id] = nil
end

addhook("join","loaddata")
function loaddata(id)
     local loaded = false
     if player(id,"usgn") > 0 then
          local f = io.open("sys/lua/hats/saves/"..player(id,"usgn")..".txt","r")
          if f ~= nil then
               for line in f:lines() do
                    if line then
                         currenthat[id] = tonumber(line)
                         break
                    end
               end
               loaded = true
               f:close()
          end
     end
     if not loaded then
          currenthat[id] = 0
     end
end

addhook("spawn","hat_spawn")
function hat_spawn(id)
     timer(1,"hat_spawn_delay",id)
end

function hat_spawn_delay(id)
     id = tonumber(id)
     if currenthat[id] > 0 then
          hatimg[id] = image(hattable[currenthat[id]].path,1,1,200+id)
     end
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview