Ulysses Stuff > Suggestions

Cash System?

<< < (6/7) > >>

Golden-Death:
I don't really know what the problem is. It seems to be thinking one of my variables is nil, but that same variable works fine with everything else, if you are correct in saying everything else works. I don't know why the HUD wouldn't be showing.

See if you have the clientside file (golden_cl.lua) in your lua folder, and check to make sure that garrysmod/data/Data Cash/(your steam id)_pref.txt says "true" inside. Hmmm.

TomatoSoup:
Maybe my CustomHud overwrites the existing hud?

And just before it calls the variable, do a

variable = variable or 0, I do that whenever I get an error, and it tends to eliminate the error, yet continue to function perfectly.

TomatoSoup:
Theres nothing in data/datacash. I awarded myself 100 just as a test, and it didn't save that?

Maybe it's returning nil because it doesn't have anything to check? In that case, still do the variable = variable or 0 so we don't loose the hook.

Golden-Death:
I'll add some more debug messages so I can see where it's hanging up for you.

Alright try this, and connect to your server. Then try out all the stuff and give me the full console log so I know what's up with it.

If this doesn't work, I could also try running a listen server to test it, if my router isn't messed up.

The best thing to do, if you still want it, would be for me to recode it. It's a bit hacky, which is probably why it has so many issues.


Replace goldencash.lua

--- Code: -----Goldencash System By Golden-Death. Duuuh. --

AddCSLuaFile( "golden_cl.lua" )
include("golden_cl.lua")


GCashDefaultStartMoney = 200


PlyCash = {}
PlayID = {}

function GCashInit(ply)
local id = ply:SteamID()
PlayID[ply] = id
if PlayID[ply] == nil then
Msg("ERROR: Player: "..ply:Nick().."'s ID was nil!\n")
else
Msg("No problems! Stored "..ply:Nick().."'s ID as "..PlayID[ply].."\n")
end
if (not file.IsDir("datacash")) then
  file.CreateDir("datacash")
  Msg("Data directory not found. Creating one now.\n")
end

 if not file.Exists("datacash/"..PlayID[ply]..".txt") then
  file.Write("datacash/"..PlayID[ply]..".txt",GCashDefaultStartMoney)
  Msg("Data for "..PlayID[ply].." not found. Creating one now.\n")
 end
  if file.Exists("datacash/"..PlayID[ply]..".txt") then
  Msg("Data for "..PlayID[ply].." found. Loading.\n")
  local gdata = file.Read("datacash/"..PlayID[ply]..".txt")
  GCashLoad(ply, PlayID[ply], gdata)
  Msg("Loaded data.\n")
 end
local rp = RecipientFilter() // Grab a RecipientFilter object
rp = rp:AddPlayer(ply)
umsg.Start("updatecash", rp)
umsg.Long(PlyCash[PlayID[ply]])
umsg.End()
GCashPrefLoad(ply)
Msg("Now sending cash to client and updating prefs\n")
end

function GCashPrefLoad(ply)
 if not file.Exists("datacash/"..PlayID[ply].."_prefs.txt") then
  file.Write("datacash/"..PlayID[ply].."_prefs.txt", "true")
  Msg("Preferences for "..PlayID[ply].." not found. Creating one now.\n")
 end
  if file.Exists("datacash/"..PlayID[ply].."_prefs.txt") then
  Msg("Preferences for "..PlayID[ply].." found. Loading.\n")
  local gp = file.Read("datacash/"..PlayID[ply].."_prefs.txt")
  if gp == "false" then
  ply:PrintMessage(HUD_PRINTTALK, "[GCash] Your HUD for GCash is hidden. Type !gcash hud show to bring it back.")
  end
local rp = RecipientFilter() // Grab a RecipientFilter object
rp = rp:AddPlayer(ply)
umsg.Start("updateprefs", rp)
umsg.String(gp)
umsg.End()
Msg("Successfully sent preferences to client\n")
 end
end



function GCashLoad(ply, id, cash)
Msg("Recieved player: "..id.."\nCash: "..cash.."\n")
PlyCash[id] = cash
end

function GCashAdd(ply, rec, id, cash)
PlyCash[id] = PlyCash[id] + math.Round(cash)
rec:PrintMessage(HUD_PRINTTALK, "[GCash] "..ply:Nick().." has awarded you "..math.Round(cash).." dollars.\n")
GCashStore(rec)
end
--(ply, v, PlayID[v], PlayID[ply], stt[4])
function GCashTrade(ply, rec, recid, plyid, cash)
PlyCash[recid] = PlyCash[recid] + math.Round(cash)
PlyCash[plyid] = PlyCash[plyid] - math.Round(cash)
rec:PrintMessage(HUD_PRINTTALK, "[GCash] "..ply:Nick().." has given you "..math.Round(cash).." dollars.\n")
GCashStore(rec)
end

function GCashSub(ply, rec, id, cash)
PlyCash[id] = PlyCash[id] - math.Round(cash)
rec:PrintMessage(HUD_PRINTTALK, "[GCash] "..ply:Nick().." has taken "..math.Round(cash).." dollars from you.\n")
GCashStore(rec)
end

function GCashStore(ply)
--for k,v in pairs(player.GetAll()) do
Msg("Saving cash...\n")
file.Write("datacash/"..PlayID[ply]..".txt", PlyCash[PlayID[ply]])
local rp = RecipientFilter() // Grab a RecipientFilter object
rp = rp:AddPlayer(ply)
umsg.Start("updatecash", rp)
umsg.Long(PlyCash[PlayID[ply]])
umsg.End()
Msg("Saved data for "..PlayID[ply].."\nSaved "..PlyCash[PlayID[ply]].."\n")
end
--end

function GCashPrefSave(ply, opt)
if not file.Exists("datacash/"..PlayID[ply].."_prefs.txt") then
  file.Write("datacash/"..PlayID[ply].."_prefs.txt", opt)
  Msg("Preferences for "..PlayID[ply].." not found. Creating one now.\n")
 end
  if file.Exists("datacash/"..PlayID[ply].."_prefs.txt") then
  Msg("Preferences for "..PlayID[ply].." found. Loading.\n")
file.Write("datacash/"..PlayID[ply].."_prefs.txt", opt)
 end
end


function GCCommand( ply, cmd )
    --Award Command--
if (string.find(cmd, "!gcash award")) then
if not ply:IsSuperAdmin( ) then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Only admins can use this command.")
return "";end
local stt = string.Explode( " ", cmd )
if stt[3] == nil or stt[4] == nil then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Not enough arguments were passed! (!gcash give player amount)")
return "";end
if tonumber(stt[4]) == nil then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Error: Something was wrong with the amount you tried to award.")
return "";end
for k,v in pairs(player.GetAll()) do
local ns = string.lower(v:GetName())
if (string.find(ns, stt[3])) then
GCashAdd(ply, v, PlayID[v],stt[4])
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Awarding "..math.Round(stt[4]).." dollars to "..v:Nick()..".")
end
return ""
end
end

--Give Command--
if (string.find(cmd, "!gcash give")) then
local stt = string.Explode( " ", cmd )
if stt[3] == nil or stt[4] == nil then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Not enough arguments were passed! (!gcash give player amount)")
return "";end
if tonumber(stt[4]) == nil then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Error: Something was wrong with the amount you tried to give.")
return "";end
for k,v in pairs(player.GetAll()) do
local ns = string.lower(v:GetName())
if (string.find(ns, stt[3])) then
if tonumber(PlyCash[PlayID[ply]]) < tonumber(stt[4]) then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Sorry "..ply:Nick()..", but you only have "..PlyCash[PlayID[ply]].." dollars. You cannot give "..v:Nick().." "..math.Round(stt[4]).." dollars.")
return "";end
if tonumber(stt[4]) < 1 then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Sorry "..ply:Nick()..", but you cannot give players negative cash. Duh.")
return "";end
GCashTrade(ply, v, PlayID[v], PlayID[ply], stt[4])
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Giving "..math.Round(stt[4]).." of your dollars to "..v:Nick()..".")
end
return ""
end
end

--Save Command--
if (string.find(cmd, "!gcash save")) then
GCashStore(ply)
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Your cash has been saved. ("..PlyCash[PlayID[ply]]..")")
return ""
end

--Check Command--
if (string.find(cmd, "!gcash check")) then
PlyCash[PlayID[ply]] = PlyCash[PlayID[ply]] or 0
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Your cash is: "..PlyCash[PlayID[ply]])
return ""
end

--Pref Command-
if (string.find(cmd, "!gcash hud")) then
local stt = string.Explode( " ", cmd )
if stt[3] == nil then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Not enough arguments were passed! (!gcash hud show or hide)")
return "";end
if stt[3] == "show" then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] HUD on.")
local rp = RecipientFilter() // Grab a RecipientFilter object
rp = rp:AddPlayer(ply)
umsg.Start("updateprefs", rp)
umsg.String("true")
umsg.End()
GCashPrefSave(ply, "true")
Msg("Successfully sent pref to client to show\n")
end
if stt[3] == "hide" then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] HUD off.")
local rp = RecipientFilter() // Grab a RecipientFilter object
rp = rp:AddPlayer(ply)
umsg.Start("updateprefs", rp)
umsg.String("false")
umsg.End()
GCashPrefSave(ply, "false")
Msg("Successfully sent pref to client to hide\n")
end
return ""
end

--Help Command--
if (string.find(cmd, "!gcash help")) then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Help - Say the following for the desired effect:\n!gcash award (player) (amount) - Awards a player money*")
ply:PrintMessage(HUD_PRINTTALK, "!gcash take (player) (amount) - Removes money from a player*\n!gcash list - Lists player's money")
ply:PrintMessage(HUD_PRINTTALK, "!gcash give (player) (amount) - Gives some of your money to another player\n!gcash check - Shows your money")
ply:PrintMessage(HUD_PRINTTALK, "!gcash hud (hide or show) - Toggle the HUD. Your choice is saved for later logins.\n!gcash help - Displays this help")
ply:PrintMessage(HUD_PRINTTALK, "--GCash System made by Golden-Death--   (* Admin only)")
return ""
end

--List Command--
if (string.find(cmd, "!gcash list")) then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Displaying money for connected players:")
for k,v in pairs(player.GetAll()) do
ply:PrintMessage(HUD_PRINTTALK, v:Nick()..": "..PlyCash[PlayID[v]].." dollars.")
end
return ""
end


--Subtract Command--
if (string.find(cmd, "!gcash take")) then
if not ply:IsSuperAdmin( ) then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Only admins can use this command.")
return "";end
local stt = string.Explode( " ", cmd )
if stt[3] == nil or stt[4] == nil then
ply:PrintMessage(HUD_PRINTTALK, "Not enough arguments were passed! (!gcash give player amount)")
return "";end
if tonumber(stt[4]) == nil then
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Error: Something was wrong with the amount you tried to take.")
return "";end
for k,v in pairs(player.GetAll()) do
local ns = string.lower(v:GetName())
if (string.find(ns, stt[3])) then
GCashSub(ply, v, PlayID[v],stt[4])
ply:PrintMessage(HUD_PRINTTALK, "[GCash] Taking "..math.Round(stt[4]).." dollars from "..v:Nick()..".")
end
return ""
end
end
end

concommand.Add("gcstore",GCashStore)
concommand.Add("gcc",GCashInit)
hook.Add ( "PlayerInitialSpawn", "Init spawn", GCashInit )
hook.Add ( "PlayerSay", "Cash Command", GCCommand )
hook.Add ( "PlayerDisconnected ", "Cash Quit", GCashStore )
--- End code ---

TomatoSoup:
Yeah, it would be best if you just recode the whole thing, because as is, it breaks the Contraption Saver.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version