Hey guys i got a money system
autorun/client/cl_money.lua
[LUA] local Meta = FindMetaTable("Player")
function Meta:Money(money)
return self:GetNWInt("Money")
end
hook.Add("HUDPaint","DrawMoney",function()
draw.RoundedBox( 2, 25, ScrH() - 100, 130, 23, Color( 128, 255, 128, 255 ) )
draw.DrawText("Money: "..LocalPlayer():Money(), "ScoreboardText", 30, ScrH() - 97, Color(255,255,255,255),0)
end) [/LUA]
Autorun/money.lua
[LUA]
AddCSLuaFile("autorun/client/cl_money.lua")
local Meta = FindMetaTable("Player")
function Meta:Money(money)
return self:GetNWInt("Money")
end
function Meta:Money(money)
return self:GetPData("Money", money)
end
function Meta:SetMoney(money)
self:SetPData("Money", money)
self:SetNWInt("Money", money)
end
function Meta:CanAfford(cost)
return (self:Money() - cost) >= 0
end
hook.Add("PlayerInitialSpawn","DataRetrieving", function(ply)
if !ply:GetPData("Money") then ply:SetPData("Money",0) end
ply:SetNWInt("Money", ply:GetPData("Money") )
end)
function ChangeMoney(ply,cmd,args)
local target, amount = FindPlayer(args[1]),tonumber(args[2])
if !ply:IsSuperAdmin() then ply:ChatPrint("You don't have access to that command!") end
if !target then ply:ChatPrint("No player found with a matching name!") end
if amount < 0 then ply:ChatPrint("You can't use a negative number!") end
target:SetMoney(amount)
end
concommand.Add("setmoney",ChangeMoney)
function GiveMoney(ply,cmd,args)
local target = ply:GetEyeTrace().Entity
if !target:IsPlayer() or !ply:CanAfford(args[1]) then return end
target:SetMoney(target:Money() + args[1])
ply:SetMoney(ply:Money() - args[1])
target:ChatPrint("Received "..tostring(args[1]).."$ from "..ply:Nick().."!")
ply:ChatPrint("Gave "..tostring(args[1]).."$ to "..target:Nick().."!")
end
concommand.Add("givemoney",GiveMoney)
function FindPlayer(info)
local players = player.GetAll()
for k, v in pairs(players) do
if string.find(string.lower(v:Nick()), string.lower(tostring(info))) ~= nil then
return v
end
end
return nil
end
[/LUA]
Ok when i go to do this:
[LUA]
function Tim ( ply )
ply:ChatPrint("Gave" )
ply:SetMoney(ply:Money() + 1000 )
end
concommand.Add( "Tim", Tim )
[/LUA]
It works it saves gave and then when it sets money it has a error.. SetPData return a nil value
please help!
[lua]
function Tim( ply )
for k, v in pairs(ply) do
v:SetMoney(v:Money() + 1000)
end
end
[/lua]
Untested.
so
[LUA]
function Tim( ply )
for k, v in pairs(ply) do
v:SetMoney(v:Money() + 1000)
end
end
concommand.Add ( "Tim, Tim )
[/LUA]
[editline]12:32AM[/editline]
Somehow i think for k, v in pairs(ply) is wrong
[editline]01:28AM[/editline]
Bump help plz!
[editline]01:45AM[/editline]
plz
I would guess that it is because you are trying to use the "SetPData" function in a shared file.
The "SetPData" function is only available on the server; which would be why it is returning nil.
Try adding
[lua]
if CLIENT then return end;
[/lua]
at the start of your "autorun/money.lua" file.
[QUOTE=Trivkz;18249930][lua]
function Tim( ply )
for k, v in pairs(ply) do
v:SetMoney(v:Money() + 1000)
end
end
[/lua]
Untested.[/QUOTE]
That's silly, you'll be giving money to a command and a list of arguments.
The problem is you seem to have an extra Meta:Money() method which both uses GetPData (which you should not be doing) and uses it wrong. So just remove that.
where is the extra meta;moneY?
[editline]04:59AM[/editline]
Found the extra Meta:Money()
[editline]05:00AM[/editline]
So money.lua
[LUA]
AddCSLuaFile("autorun/client/cl_money.lua")
if CLIENT then return end;
local Meta = FindMetaTable("Player")
function Meta:Money(money)
return self:GetNWInt("Money")
end
function Meta:SetMoney(money)
self:SetPData("Money", money)
self:SetNWInt("Money", money)
end
function Meta:CanAfford(cost)
return (self:Money() - cost) >= 0
end
hook.Add("PlayerInitialSpawn","DataRetrieving", function(ply)
if !ply:GetPData("Money") then ply:SetPData("Money",0) end
ply:SetNWInt("Money", ply:GetPData("Money") )
end)
function ChangeMoney(ply,cmd,args)
local target, amount = FindPlayer(args[1]),tonumber(args[2])
if !ply:IsSuperAdmin() then ply:ChatPrint("You don't have access to that command!") end
if !target then ply:ChatPrint("No player found with a matching name!") end
if amount < 0 then ply:ChatPrint("You can't use a negative number!") end
target:SetMoney(amount)
end
concommand.Add("setmoney",ChangeMoney)
function GiveMoney(ply,cmd,args)
local target = ply:GetEyeTrace().Entity
if !target:IsPlayer() or !ply:CanAfford(args[1]) then return end
target:SetMoney(target:Money() + args[1])
ply:SetMoney(ply:Money() - args[1])
target:ChatPrint("Received "..tostring(args[1]).."$ from "..ply:Nick().."!")
ply:ChatPrint("Gave "..tostring(args[1]).."$ to "..target:Nick().."!")
end
concommand.Add("givemoney",GiveMoney)
function FindPlayer(info)
local players = player.GetAll()
for k, v in pairs(players) do
if string.find(string.lower(v:Nick()), string.lower(tostring(info))) ~= nil then
return v
end
end
return nil
end[/LUA]
[editline]05:12AM[/editline]
And would it be
[LUA]
function Tim ( ply )
ply:ChatPrint("Gave" )
ply:SetMoney(ply:Money() + 1000 )
end
concommand.Add( "Tim", Tim )
[/LUA]
Sorry, you need to Log In to post a reply to this thread.