• LocalPlayer() is null?
    6 replies, posted
https://i.imgur.com/oWgWwlT.png So I get the error above, and I can't find out why... Here's the code it's referring to: local function comma_value(amount)     local formatted = amount     while true do           formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')         if (k == 0) then             break         end     end     return formatted end local ply = LocalPlayer() -- Main Rectangle local mainRect = vgui.Create("DPanel") mainRect:SetSize(ScrW(), 20) -- Set the size of the panel mainRect:DockPadding(5, 5, 5, 5) mainRect:SetBackgroundColor(Color(50, 50, 50, 255)) local Health = math.Clamp(ply:Health(), 0, 100) local HealthText = "Health: " .. tostring(Health) .. "%" mainRect.HealthLabel = mainRect:Add("DLabel") mainRect.HealthLabel:SetDark(false) mainRect.HealthLabel:Dock(TOP) mainRect:Hide() function mainRect:Think()     self.HealthLabel:SetText(("Health: %d"):format(LocalPlayer():Health()))     --self.ArmorLabel:SetText(("Armor: %d"):format(LocalPlayer():Armor())) end hook.Add("HUDPaint", "Custom_Hud_DarkRP", function()     mainRect:PaintAt(0, 0) end) Now, at first I thought, "this must be because the LocalPlayer() isn't loaded at the time of the script running, right?"... Welp, for one that wouldn't make sense, as it's a module that's loaded with DarkRP, so the gamemode must be initialized before this script runs... And two, just to try it, I tried putting this all in GM:Initialize(), but it didn't work. I literally have no idea wtf is going on lol.
Perhaps check if LocalPlayer() is valid?
Yes localplayer isn't valid when the script is loaded and vgui isn't either since the player hasn't loaded in yet
Tried checking, but I just get no results. So I assume I need to check each frame. So I also tried checking in the HUDPaint and Think... Then I get errors... Code: if LocalPlayer():IsValid() then     local ply = LocalPlayer()     local Health = math.Clamp(ply:Health(), 0, 100)     local Armor = math.Clamp(ply:Armor(), 0, 100)     local Money = comma_value(ply:getDarkRPVar("money"))     local Salary = comma_value(ply:getDarkRPVar("salary"))     local Job = ply:getDarkRPVar("job")     -- Main Rectangle     local mainRect = vgui.Create("DPanel")     mainRect:SetSize(ScrW(), 20)     mainRect:DockPadding(5, 5, 5, 5)     mainRect:SetBackgroundColor(Color(50, 50, 50, 255))     mainRect.HealthLabel = mainRect:Add("DLabel")     mainRect.HealthLabel:Dock(TOP)     mainRect:Hide() end function mainRect:Think()     if LocalPlayer():IsValid() then         self.HealthLabel:SetText(("Health: %d"):format(Health))         --self.ArmorLabel:SetText(("Armor: %d"):format(LocalPlayer():Armor()))     end end hook.Add("HUDPaint", "Custom_Hud_DarkRP", function()     if LocalPlayer():IsValid() then         mainRect:PaintAt(0, 0)     end end) Error: [ERROR] addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:76: attempt to index global 'mainRect' (a nil value)   1. unknown - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:76    2. doInclude - [C]:-1     3. loadModules - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:102      4. Call - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:147       5. unknown - gamemodes/darkrp/gamemode/cl_init.lua:56
Could just delay until after InitPostEntity
You seem to be misunderstanding how lua files are read and now hooks work. This is should work: local function comma_value(amount) local formatted = amount while true do   formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k == 0) then break end end return formatted end -- Main Rectangle local mainRect = vgui.Create("DPanel") mainRect:SetSize(ScrW(), 20) mainRect:DockPadding(5, 5, 5, 5) mainRect:SetBackgroundColor(Color(50, 50, 50, 255)) mainRect.HealthLabel = mainRect:Add("DLabel") mainRect.HealthLabel:Dock(TOP) mainRect:Hide() function mainRect:Think()     local ply = LocalPlayer() if !IsValid(ply) then return end local Health = math.Clamp(ply:Health(), 0, 100)     local Armor = math.Clamp(ply:Armor(), 0, 100)     local Money = comma_value(ply:getDarkRPVar("money"))     local Salary = comma_value(ply:getDarkRPVar("salary"))     local Job = ply:getDarkRPVar("job") self.HealthLabel:SetText(("Health: %d"):format(Health)) --self.ArmorLabel:SetText(("Armor: %d"):format(LocalPlayer():Armor())) end hook.Add("HUDPaint", "Custom_Hud_DarkRP", function() mainRect:PaintAt(0, 0) end)
LocalPlayer will never return a non-entity - it is safe to use obj:IsValid() in that case.
Sorry, you need to Log In to post a reply to this thread.