Pretty much name says it all.. Trying to check if the player has godmode enabled.
Then I'm trying to print true or false into console.
I basically want to use this for my hud so when an admin is godded their health will turn gold.
Though, currently I cant seem to get the check to work. Please help <3
serverside - addons/gui_hud/lua/autorun/server/goddem.lua
// used to make functions which apply to every instance of inputted class
local Player = FindMetaTable("Player")
// overriding the GodEnable() function in order to add a custom networked boolean
function Player:GodEnable()
self:AddFlags(FL_GODMODE) // sets the user as godded
self:SetNWBool("rm_godmode", true) // setting a networked boolean
end
function Player:GodDisable()
self:RemoveFlags(FL_GODMODE) // ungods the user
self:SetNWBool("rm_godmode", false) // setting a networked boolean
end
clientside - addons/gui_hud/lua/autorun/client/hud.lua
// used to make functions which apply to every instance of inputted class
local Player = FindMetaTable("Player")
// overriding the HasGodMode() function to return our networked boolean
// the original function actually only returns false when used clientside
function Player:HasGodMode()
return self:GetNWBool("rm_godmode", false)
end
// btw there's no need to add a custom HasGodMode() function to the server because the one
// serverside actually returns the correct value
print(LocalPlayer():HasGodMode())
ERROR
[ERROR] addons/gui_hud/lua/autorun/client/hud.lua:220: attempt to call method 'HasGodMode' <a nil value>
1. unknown - addons/hui_hud/lua/autorun/client/hud.lua:220
Sorry, I'm confused. I posted the client & server side code that I'm trying to use to check if goded.
Small snippets of code don't really help. We need to see much more of addons/gui_hud/lua/autorun/client/hud.lua. The error occurs on line 220, that means there's at least 220 other lines' worth of context that we don't have. It could be trying to run that function on something that isn't a player object.
Oh okay. I understand.
Anyways, for testing purposes, I've deleted everything client side except for;
// used to make functions which apply to every instance of inputted class
local Player = FindMetaTable("Player")
// overriding the HasGodMode() function to return our networked boolean
// the original function actually only returns false when used clientside
function Player:HasGodMode()
return self:GetNWBool("rm_godmode", false)
end
// btw there's no need to add a custom HasGodMode() function to the server because the one
// serverside actually returns the correct value
print(LocalPlayer():HasGodMode())
ERROR when I do: ulx god LOT
[ERROR] addons/gui_hud/lua/autorun/client/hud.lua:12: attempt to call method 'HasGodMode' <a nil value>
1. unknown - addons/hui_hud/lua/autorun/client/hud.lua:12
That looks like it should be a startup error, not one that happens when you run the command.
Have you tried just getting rid of that line entirely? I know when the file first gets parsed, LocalPlayer will not be a valid entity yet. It could have something to do with that.
I removed it & restarted.
No error, though nothing happens at all when I switch to god mode because nothing is checking if I'm goded.
The print(LocalPlayer():HasGodMode()) served as a place holder.
Your print, in the location that it was, wouldn't have helped you anyway even if it worked. It was in a spot where it runs immediately when the script is loaded, not when you run the command and not on a continuous basis
You have to override the GodmodeEnable functions if you want it to work with the built in godmode system.
Give us more code.
That's all the code I have.
You can test this in-game by putting something like the following into your client console once your player has spawned:
[code]lua_run_cl print(LocalPlayer():HasGodMode())[/code]
Bear in mind that your code will not track the [i]god[/i] console command since this sets the flag internally and does not trigger [i]GodEnable()[/i].
It's good practice not to use already-defined names for variables and functions, even in a limited scope. Using [i]pl[/i] instead of [i]Player[/i] will prevent you overriding the [i]Player()[/i] function. It's also good practice to pass on the function call if you don't want to completely override something, since the game code could get changed in the future:
// used to make functions which apply to every instance of inputted class
local pl = FindMetaTable("Player")
// overriding the GodEnable() function in order to add a custom networked boolean
pl.RealGodEnable = pl.GodEnable
function pl:GodEnable()
self:SetNWBool("rm_godmode", true) // setting a networked boolean
return self:RealGodEnable()
end
pl.RealGodDisable = pl.GodDisable
function pl:GodDisable()
self:SetNWBool("rm_godmode", false) // setting a networked boolean
return self:RealGodDisable()
end
Thank you Freeman, I appreciate the helpfulness & detail
Anywho, this is what I get in console;
While goded
] lua_run_cl print(LocalPlayer():HasGodMode())
false
false
While ungoded
] lua_run_cl print(LocalPlayer():HasGodMode())
false
Could you post the code you used to get this result? I gave it a quick test and it worked my end:
if SERVER then
local pl = FindMetaTable("Player")
pl.RealGodEnable = pl.GodEnable
function pl:GodEnable()
self:SetNWBool("rm_godmode", true)
return self:RealGodEnable()
end
pl.RealGodDisable = pl.GodDisable
function pl:GodDisable()
self:SetNWBool("rm_godmode", false)
return self:RealGodDisable()
end
elseif CLIENT then
local pl = FindMetaTable("Player")
function pl:HasGodMode()
return self:GetNWBool("rm_godmode", false)
end
end
That snippet isn't refresh safe. You should use
pl.RealGodEnable = pl.RealGodEnable or pl.GodEnable
and
pl.RealGodDisable = pl.RealGodDisable or pl.GodDisable
Otherwise a refresh will cause an infinite loop once the functions are called again. Currently it will cache the already detoured function thinking that it's the original function if the file is ever reloaded in the same session.
Sorry, you need to Log In to post a reply to this thread.