Forum

> > CS2D > Scripts > Load player money
Forums overviewCS2D overview Scripts overviewLog in to reply

English Load player money

3 replies
To the start Previous 1 Next To the start

old Load player money

Ranu
User Off Offline

Quote
Hello, i have a money script, when a player join it should load the player money if the player id health > 0 then.
I didn't want to show you all of the code.

1
2
3
4
5
6
7
8
9
addhook("join","b")
function b(id,money)
if usgn ~= 0 then
money = readfile("sys/lua/money/"..player(id,"usgn")..".txt", 1)-1+1
parse("setmoney "..id.." "..money)
msg2(id,"\169000255000Your money was succesfully loaded!")
return 1
end
end

it doesn't work because if a player join the server, the script will load the money for him/her. in cs2d setmoney only works while the player(id) is alive
edited 1×, last 25.04.19 09:30:55 pm

old Re: Load player money

TrialAndError
User Off Offline

Quote
Join hook doesn't take a second parameter.
The 'usgn' variable doesn't exist (unless you have declared it somewhere else)
Why the '-1+1' at the end of your readfile?
And the return 1 won't affect the join hook.

What I have done in the solution below is to load (when the player first joins) the players data into a table called cache which we can call later in spawn hook to avoid reading a file over and over again. We'll clear the cache when the player leaves.

I assume that the file will be saved such that each new data is split by a space (' '). You could change it by editing the 'file_delim' variable if you have another format.

The data returned is automatically split into a table which contains the content of the file.

So if you had a file with the content:

1
10 20 30 40

The data returned would be
1
2
3
4
5
6
returned data {
	"10",
	"20",
	"30",
	"40"
}
You could get each new data by indexing to the correct spot, e.g. data[1], data[2], ..., data[n]

Note that each data is automatically a string unless you use tonumber to convert it to a number.


Code:
Spoiler >


This is just one example of many different solutions.
edited 2×, last 26.04.19 11:07:35 am

old Re: Load player money

Ranu
User Off Offline

Quote
@user Cebra: Man If i use spawn, then the player can buy something with his money, after he'll respawn again, and his money will back.

@user TrialAndError: usgn = player(id, "usgn'), '-1+1' at the end of my readfile is magic , Well Thanks, it is working
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview