Hello, I made a start on a leveling system but now I cannot create a custom printer that calls the custom hook. Everytime it tries to call the custom hook it says that I am trying to index ply a nil value, I have tried defining ply = Player and LocalPlayer but it says the same.
[lua]
function ENT:Think()
if not self.sparking then return end
local effectdata = EffectData()
effectdata:SetOrigin(self:GetPos())
effectdata:SetMagnitude(1)
effectdata:SetScale(1)
effectdata:SetRadius(2)
util.Effect("Sparks", effectdata)
hook.Call("Custom")
end
[/lua]
All that I added in the DarkRp printer is the hook at the bottom (init.lua)
[lua]
function Check(ply)
levelcheck = ply:GetPData("level")
if !levelcheck then
ply:SetPData("level",1) --Players starting level
end
xpcheck = ply:GetPData("xp")
if !xpcheck then
ply:SetPData("xp",1) --Players starting xp
end
level = ply:GetPData("level")
xp = ply:GetPData("xp")
if (level > "1") then
print("Welcome back, Your level is "..level..".")
else
print("Welcome to BritishRP your level has been set.")
end
if (xp > "1") then
print("Your xp is "..xp..".")
else
print("Your xp has been set.")
end
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", Check )
function Stats(ply)
level = ply:GetPData("level")
speed = (150 + (level*10))
ply:SetWalkSpeed(speed)
hp = (100 + (level*10))
ply:SetHealth(hp)
armor = (0 + (level*10))
ply:SetArmor(armor)
end
hook.Add("PlayerSpawn","playerspawn",Stats)
function Xpgain(ply)
xpset = ply:GetPData("xp") + 1
ply:SetPData("xp",xpset)
level = ply:GetPData("level")
lvlup = (level*60)
print("You have "..xpset.."xp out of "..lvlup.."xp.")
if xpset == lvlup then
ply:SetPData("xp",0)
ply:SetPData("level", (level+1))
end
end
hook.Add("Custom", "customhook", Xpgain)
function Levelup(ply)
ply:SetPData("level", (ply:GetPData("level") + 1))
levelup = ply:GetPData("level")
print("Your level is now "..levelup..".")
end
concommand.Add("level", Levelup )
function LevelXpReset(ply)
ply:SetPData("level",1)
ply:SetPData("xp",1)
print("Your stats have been reset.")
end
concommand.Add("levelxpreset", LevelXpReset)
[/lua]
This is the leveling system. It works without the custom hook.
[editline]02:19PM[/editline]
Anyone? Or have I made a really simple mistake that no one wants to point out?
Anyone?
[highlight](User was banned for this post ("Bump" - mahalis))[/highlight]
You would need to use hook.Call with the ply arg. Etc: hook.Call( "Custom", ply ) Ply needs to be defined though.
The second argument of hook.Call is actually the gamemode object. ply would be passed into that, what you really want is this:
[lua]
hook.Call( "Custom", nil, ply )
[/lua]
It just makes the gamemode object nil, which I believe skips the portion of the code in hook.Call where it checks for any GAMEMODE:----.
[editline]12:36PM[/editline]
And yes, you need ply to be defined somewhere, like Trivkz said.
Okay I have this, now the error is
Hook 'customhook' Failed: lua\autorun\level.lua:42: attempt to call method 'GetPData' (a nil value)
[lua] function ENT:Initialize()
self:SetModel("models/props_c17/consolebox01a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
if phys:IsValid() then phys:Wake() end
self.sparking = false
self.damage = 100
self.IsMoneyPrinter = true
timer.Simple(10, self.CreateMoneybag, self)
hook.Call("Custom", nil, ply)
end[/lua]
[lua]function Xpgain(ply)
ply = Player()
xpset = ply:GetPData("xp") + 1
ply:SetPData("xp",xpset)
level = ply:GetPData("level")
lvlup = (level*60)
print("You have "..xpset.."xp out of "..lvlup.."xp.")
if xpset == lvlup then
ply:SetPData("xp",0)
ply:SetPData("level", (level+1))
end
end
hook.Add("Custom", "customhook", Xpgain)[/lua]
Your everwriting ply by using Player()
[LUA]
function Xpgain(ply)
ply = Player()
xpset = ply:GetPData("xp") + 1
ply:SetPData("xp",xpset)
level = ply:GetPData("level")
lvlup = (level*60)
print("You have "..xpset.."xp out of "..lvlup.."xp.")
if xpset == lvlup then
ply:SetPData("xp",0)
ply:SetPData("level", (level+1))
end
end
hook.Add("Custom", "customhook", Xpgain)
[/LUA]
line 2! wich means that ply is no longer the parsed player object and you therefor cannot use the Player.SetPData function on it!
You should also check if the player is a player :)
ply is nil when you call the hook. It needs to be the owner of the money printer.
Never mind I found a way around this, defined the xpgain in the spawned money. So when anyone uses it they get the xp, simples :P
Sorry, you need to Log In to post a reply to this thread.