Need help with making a "Clear Inventory on join" plugin
#1
Hi!
I'm currently trying to learn lua, and I tried to make a simple plugin that clears the player's inventory when joining the server. However, I would need some help on making this plugin work (sorry for my noobishness).

Here's what I've done so far:
Code:
function Initialize(Plugin)
        PLUGIN = Plugin
        Plugin:SetName("ClearInv")
        Plugin:SetVersion(1)

        cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_JOINED, MyOnPlayerJoined);

        LOG("Initialized ClearInv")
        return true
end

function OnPlayerJoined(Player)
        Player:GetInventory():Clear()
        return true
end
Thanks for your help! I really want to learn more about lua.
Reply
Thanks given by:
#2
Change MyOnPlayerJoined to OnPlayerJoined.
Reply
Thanks given by:
#3
(10-07-2014, 02:26 AM)STR_Warrior Wrote: Change MyOnPlayerJoined to OnPlayerJoined.
Thank you! It worked.
Reply
Thanks given by:
#4
(10-07-2014, 02:26 AM)STR_Warrior Wrote: Change MyOnPlayerJoined to OnPlayerJoined.

Had this problem long time ago, too. Every example for adding a hook has a My at start and the function starts without My at start, I find that confusingConfused
Reply
Thanks given by:
#5
The "OnPlayerJoined" is the default name for the hook - it is used when you call the register function like this (old, deprecated but still working way):
cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_JOINED)
This is from the times when plugins could only register a single hook callback.

When multiple callbacks were introduced, the functions were added as the parameter:
local function AnyNameReally(a_Player)
  a_Player:SendMessage("Hello")
end

cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_JOINED, AnyNameReally)

You could even do this:
cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_JOINED, 
  function (a_Player)
    a_Player:SendMessage("Hello")
  end
)
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)