• calling function error
    1 replies, posted
i have a money txt database and i want my hud to display the money amount but i cant get it to work this is the funtion in my database.lua [CODE] function ply:getmoney() return self:databaseGetValue( "money" ) end [/CODE] that works fine when i print it like this in init.lua [CODE] function chatCommand( ply, text, public ) if (string.sub(text, 1, 6) == "/money") ply:ChatPrint("Your cash is: " .. ply:getmoney()) return(false) end end hook.Add( "PlayerSay", "chatCommand", chatCommand ); [/CODE] and i did include and AddCSLuaFile the cl_hud.lua but when i try to make my hud display money amount it says "attempted to index local 'ply' a nil value [CODE] local ply = FindMetaTable("Player") function hidehud(name) for k, v in pairs{"CHudHealth", "CHudBattery"} do if name == v then return false end end end hook.Add("HUDShouldDraw", "hidehud", hidehud) function hud( ply ) local health = LocalPlayer():Health() local money = ply:getmoney() draw.RoundedBox(0, 5, ScrH() - 15 - 20, health, 15, Color(255,0,0,255)) draw.SimpleText(health, "default", 10, ScrH() - 15 - 40, Color(255,255,255,255)) draw.SimpleText(money, "default", 10, 20, Color(255,255,255,255)) end hook.Add("HUDPaint", "MyHudName", hud) [/CODE] not sure what i have done wrong ps im just starting to learn lua hope you guys or girls can help
You really should name that variable to something better to avoid later confusion, but ply doesn't refer to a player object in the HUDPaint hook, just change ply:getmoney() to LocalPlayer():getmoney(), and remove the function arguments from the hook, assuming database.lua is shared.
Sorry, you need to Log In to post a reply to this thread.