English Load player money

3 replies
Goto Page
To the start Previous 1 Next To the start
25.04.19 09:15:45 pm
Up
Ranw-X
User
Offline Off
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.

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
VTA Community Discord: https://discord.gg/6rHKWvJ
25.04.19 10:31:55 pm
Up
panteon00
User
Online On
use the cs2d lua hook spawn hook to call cs2d cmd setmoney
loading...
25.04.19 10:45:56 pm
Up
TrialAndError
User
Offline Off
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:

Code:
1
10 20 30 40


The data returned would be
Code:
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
25.04.19 11:31:48 pm
Up
Ranw-X
User
Offline Off
@user panteon00: 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
VTA Community Discord: https://discord.gg/6rHKWvJ
To the start Previous 1 Next To the start