Forum

> > CS2D > Scripts > Give Money script
Forums overviewCS2D overview Scripts overviewLog in to reply

English Give Money script

17 replies
To the start Previous 1 Next To the start

old Re: Give Money script

TopNotch
User Off Offline

Quote
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

old Re: Give Money script

Talented Doge
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
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

old Re: Give Money script

Dousea
User Off Offline

Quote
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:
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

old Re: Give Money script

GeoB99
Moderator Off Offline

Quote
@user Dousea: 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:
1
parse("setmoney " .. id .. " 16000")

old Re: Give Money script

Gaios
Reviewer Off Offline

Quote
Why do you use always hook? This hook is created for local gameplay.Set money by spawn hook is the best in this problem.

old Re: Give Money script

THEMUD
User Off Offline

Quote
@user Gaios: The reason why @user Dousea 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.

old Re: Give Money script

VADemon
User Off Offline

Quote
@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 Dousea: 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?

old Re: Give Money script

Dousea
User Off Offline

Quote
@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.
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

old Re: Give Money script

GeoB99
Moderator Off Offline

Quote
@user Dousea: Yet Lua triggers a problem with this script because in 12 line you haven't finished the string by completing the last double quote.

old Re: Give Money script

Paulo49
User Off Offline

Quote
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")

old Re: Give Money script

GeoB99
Moderator Off Offline

Quote
@user Paulo49:
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 Dousea's code is quite more efficiently in comparison than yours.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview