Hi there, I'm attempting to have a constant updater on how much money is currently on a DarkRP server. I have this so far
[CODE]
if SERVER then
hook.Add("Think", "GetDarkRPMoney", function()
local money
for k, ply in pairs(player.GetAll()) do
money = ply:getDarkRPVar( "money" )
end
print(money)
end)
end[/CODE]
which obviously doesn't combine the values, simply loops through them. I've tried putting into a table, the doing table.concat. I've tried a bunch of things that I don't think were even close. I'm also pretty sure that using think is a very inefficient way, would it be better to put it on like a 1-5 second timer? I don't think I need the information every tick.
Why dont you instead just use the PlayerInitialSpawn Hook, grab their money on spawn. And then afterwards use playerPickedUpMoney (darkrphook) to add to the table when a current player picks more up.
I presume this is more efficient than looping through all players every so often.
Set local money to 0 and do
[code]
money = money + ply:getDarkRPVar( "money" )
[/code]
And yes, a players.GetAll() loop inside a Think hook isnt very efficient, you could use
[URL="http://wiki.darkrp.com/index.php/Hooks/Shared/DarkRPVarChanged"]DarkRPVarChanged[/URL] and check if the money var has been updaten and if so you can recalculate the total amount.
[QUOTE=Benn20002;52416010]Set local money to 0 and do
[code]
money = money + ply:getDarkRPVar( "money" )
[/code]
And yes, a players.GetAll() loop inside a Think hook isnt very efficient, you could use
[URL="http://wiki.darkrp.com/index.php/Hooks/Shared/DarkRPVarChanged"]DarkRPVarChanged[/URL] and check if the money var has been updaten and if so you can recalculate the total amount.[/QUOTE]
(I'll get to the DarkRPVarChanged after I get it working) How do I get it to stop adding the money after its reach all the players money?
[CODE]
if SERVER then
local money = 0
hook.Add("Think", "GetDarkRPMoney", function()
for k, ply in pairs(player.GetAll()) do
money = money + ply:getDarkRPVar( "money" )
end
end)
end[/CODE]
I did what you said, but this just loops endlessly, adding to the money constantly. I tried to fix this, and uhhhh, yea... It didn't work.
[CODE]
if SERVER then
local money = 0
hook.Add("Think", "GetDarkRPMoney", function()
for k, ply in pairs(player.GetAll()) do
for i=0, #player.GetAll() do
money = money + ply:getDarkRPVar( "money" )
end
end
end)
end[/CODE]
Any ideas?
Set money to 0 before the loop and only use one loop, as before.
[code]hook.Add("Think", "GetDarkRPMoney", function()
local money = 0
for _, ply in ipairs(player.GetHumans()) do
money = money + ply:getDarkRPVar("money")
end
print(money)
end)[/code]
[QUOTE=DanDaManGAMEZ;52416102](I'll get to the DarkRPVarChanged after I get it working) How do I get it to stop adding the money after its reach all the players money?
[CODE]
if SERVER then
local money = 0
hook.Add("Think", "GetDarkRPMoney", function()
for k, ply in pairs(player.GetAll()) do
money = money + ply:getDarkRPVar( "money" )
end
end)
end[/CODE]
I did what you said, but this just loops endlessly, adding to the money constantly. I tried to fix this, and uhhhh, yea... It didn't work.
[CODE]
if SERVER then
local money = 0
hook.Add("Think", "GetDarkRPMoney", function()
for k, ply in pairs(player.GetAll()) do
for i=0, #player.GetAll() do
money = money + ply:getDarkRPVar( "money" )
end
end
end)
end[/CODE]
Any ideas?[/QUOTE]
[CODE]
local Money = 0;
hook.Add("Think","Get Money",function()
Money = 0;
for k,v in pairs(player.GetAll()) do
Money = Money + v:getDarkRPVar("money");
end
end)[/CODE]
Sorry, you need to Log In to post a reply to this thread.