English Give Money script

17 replies
Goto Page
To the start Previous 1 Next To the start
07.02.16 10:34:03 pm
Up
Jededias
User
Offline Off
If the players money is 0, give 16000 in 3 seconds
07.02.16 10:40:11 pm
Up
Nekomata
User
Offline Off
redacted.
i feel stupid now.
edited 2×, last 07.02.16 10:41:56 pm
07.02.16 10:40:49 pm
Up
TopNotch
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
seconds = 3 -- You can change this if you wish
addhook("second", "_second")
function _second()
     local playerlist = player(0,"tableliving")
     for _, id in pairs(playerlist) do
          if player(id,"money") == 0 then
               timer(seconds*1000, "parse", "setmoney "..id.." 16000")
          end
     end
end

Here you go.
edited 2×, last 07.02.16 10:43:04 pm
07.02.16 10:41:02 pm
Up
Gaios
Security Supporter
Offline Off
Or you wanna this:
> file cs2d Restaurt Money (4)
08.02.16 07:52:06 am
Up
Talented Doge
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
spawn_money = true
spawnmoney = 
{
[1] = game("mp_startmoney"); 
[2] = 16000;
}
spawn_money_type = 2
spawn_money = true

addhook("spawn", "_s")

function _s(id)
     if spawn_money then
          parse("setmoney "..id.." "..spawnmoney[spawn_money_type])
     end
end

--[[ 
1 for set money
2 for 16000 money, you may also set it to any value you like.
If you want normal money, set spawn_money to false.
]]
edited 1×, last 08.02.16 08:22:15 am
Talented Doge
08.02.16 08:42:28 am
Up
Ortimh
User
Offline Off
user TopNotch's code will loop at every second and iterate through living players, if player with given id has 0 on money then cs2d lua cmd timer will be created and will trigger cs2d lua cmd parse after 3 seconds. But cs2d lua cmd timer will be recreated again around 2-3 times (he's using cs2d lua hook second hook) because player with given id still has 0 on money until 3 seconds passed. I can't confirm that my prediction is right. Here's a fix from me:
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
local togive = {}

for id = 1, 32 do
     togive[id] = false
end

function givemoney(id)
     id = tonumber(id)
     togive[id] = false
     
     parse("setmoney " .. id .. " 16000")
end

function alwayshook()
     for index, id in ipairs(player(0, "tableliving")) do
          if (player(id, "money") == 0 and not togive[id]) then
               togive[id] = true
               
               timer(3000, "givemoney", id)
          end
     end
end

addhook("always", "alwayshook")
edited 1×, last 08.02.16 09:31:03 am
I'm awesome ... and I really like cookies.
08.02.16 09:11:39 am
Up
TopNotch
User
Offline Off
@user Ortimh: You are right, but, line 11 will cause a bug.
08.02.16 09:14:47 am
Up
GeoB
Moderator
Offline Off
@user Ortimh: user TopNotch is right at this point. You've forgot another double quote to finish the string. In this case, this should be like this:
Code:
1
parse("setmoney " .. id .. " 16000")
08.02.16 09:50:14 am
Up
Gaios
Security Supporter
Offline Off
Why do you use always hook? This hook is created for local gameplay.Set money by spawn hook is the best in this problem.
08.02.16 10:13:14 am
Up
THEMUD
User
Offline Off
@user Gaios: The reason why @user Ortimh put the always hook is to make it interactive and concurrent.
So by the spawn hook, it will not give 16000 immediately if the player's money is 0, unless he re-spawned again.
I don't know what I'm doing with my life.
08.02.16 10:48:49 am
Up
Gaios
Security Supporter
Offline Off
@user THEMUD: we always can make buy hook with timer.
08.02.16 02:43:19 pm
Up
VADemon
User
Offline Off
@user Gaios: I appreciate your approach, but there are too many ways a player can spend or lose money. Most buggy is the "setmoney" command issued by other scripts.
@user Ortimh: yet I can't justify the use of cs2d lua hook always. If you care so much about ±1s time delay, why don't use cs2d lua hook ms100 instead, where it would be nearly insignificant?
08.02.16 03:24:37 pm
Up
Ortimh
User
Offline Off
@user VADemon: Sure, you can use cs2d lua hook ms100 and even cs2d lua hook second.

This time it will only create cs2d lua cmd timer when using command cs2d cmd setmoney and buying if you care so much about performance impact or anything else. Can't guarantee it will work. Make sure this script is loaded first before any other scripts.
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
51
52
53
54
55
56
57
local togive = {}
local oldparse = parse

for id = 1, 32 do
     togive[id] = false
end

function givemoney(id)
     id = tonumber(id)
     togive[id] = false
     
     parse("setmoney " .. id .. " 16000")
end

function checkmoney(id)
     if (player(id, "money") == 0 and not togive[id]) then
          togive[id] = true
          
          timer(3000, "givemoney", id)
     end
end

function buyhook(id)
     checkmoney(id)
end

function string:split(delimiter)
     local strs = {}
     
     for index, str in string:gmatch("[^" .. delimiter .. "]+") do
          strs[#strs + 1] = str
     end
     
     return strs
end

function parse(commands, stop)
     if (stop == 0 and commands:find(";")) then
          for index, str in ipairs(commands:split(";")) do
               local starts, ends = str:find("setmoney")
               
               if (starts and ends) then
                    checkmoney(tonumber(str:split("%s")[2]))
               end
          end
     else
          local starts, ends = commands:find("setmoney")
          
          if (starts and ends) then
               checkmoney(tonumber(commands:split("%s")[2]))
          end
     end
     
     return oldparse(commands, stop)
end

addhook("buy", "buyhook")
edited 2×, last 09.02.16 07:07:29 am
I'm awesome ... and I really like cookies.
08.02.16 03:34:11 pm
Up
GeoB
Moderator
Offline Off
@user Ortimh: Yet Lua triggers a problem with this script because in 12 line you haven't finished the string by completing the last double quote.
08.02.16 04:05:09 pm
Up
Ortimh
User
Offline Off
@user GeoB: Same error again? Fu*k. Thanks.
I'm awesome ... and I really like cookies.
10.02.16 09:58:24 am
Up
Paulo49
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
valuemoney = 16000
minmoney = 0

function OnSpawn(id)

     if player(id,'money') <= minmoney then

          parse("setmoney "..id.." "..valuemoney.."")

     end
end

addhook("spawn","OnSpawn")
Paulo Henrique
10.02.16 11:36:18 am
Up
GeoB
Moderator
Offline Off
@user Paulo49:
Code:
1
parse("setmoney "..id.." "..valuemoney.."")

On 8 line, even though Lua will not recognize any issue but the last two double quotes are useless because you already defined a certain value of how much money should be given to players by valuemoney character in the string. Also, it'd be appreciated if you could refrain at posting codes without reading thread posts. user Ortimh's code is quite more efficiently in comparison than yours.
10.02.16 07:15:47 pm
Up
Paulo49
User
Offline Off
@user GeoB: custom
Code:
1
"..variable.." == "..variable


is my custom
Paulo Henrique
To the start Previous 1 Next To the start