I can't create any commands.
#1
I tried to create a command but it gave an error. Then, I tried to use the example code in the documentation but it did the same. Here is the code and the error.

function Initialize(Plugin)
	Plugin:SetName("DerpyPluginThatBlowsPeopleUp")
	Plugin:SetVersion(9001)

	cPluginManager.BindCommand("/explode", "derpyplugin.explode", Explode, " ~ Explode a player");

	cPluginManager:AddHook(cPluginManager.HOOK_COLLECTING_PICKUP, OnCollectingPickup)

	LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
	return true
end

function Explode(Split, Player)
	if (#Split ~= 2) then
		-- There was more or less than one argument (excluding the "/explode" bit)
		-- Send the proper usage to the player and exit
		SendMessage(Player, "Usage: /explode [playername]")
		return true
	end

	-- Create a callback ExplodePlayer with parameter Explodee, which MCS calls for every player on the server
	local HasExploded = false
	local ExplodePlayer = function(Explodee)
		-- If the player we are currently at is the one we specified as the parameter
		if (Explodee:GetName() == Split[2]) then
			-- Create an explosion at the same position as they are; see API docs for further details of this function
			Player:GetWorld():DoExplosionAt(Explodee:GetPosX(), Explodee:GetPosY(), Explodee:GetPosZ(), false, esPlugin)
			SendMessageSuccess(Player, Split[2] .. " was successfully exploded")
			HasExploded = true;
			return true -- Signalize to MCS that we do not need to call this callback for any more players
		end
	end
	
	-- Tell MCS to loop through all players and call the callback above with the Player object it has found
	cRoot:Get():FindAndDoWithPlayer(Split[2], ExplodePlayer)
	
	if not(HasExploded) then
		-- We have not broken out so far, therefore, the player must not exist, send failure
		SendMessageFailure(Player, Split[2] .. " was not found")
	end
	
	return true
end

function OnCollectingPickup(Player, Pickup) -- Again, see the API docs for parameters of all hooks. In this case, it is a Player and Pickup object
	if (Player:GetClientHandle():GetPing() > 100) then -- Get ping of player, in milliseconds
		return true -- Discriminate against high latency - you don't get dropsBig Grin
	else
		return false -- You do get the drops! Yay~
	end
end

[Image: zUxd82d.png]
Reply
Thanks given by:
#2
Hey!

That's not all the code, or? Looks like you try to access the method SendMessage() in function Explode not over the Player object
I think you have there around line 17 in file asd.lua: SendMessage() instead of Player:SendMessage().

regards,
Seadragon91
Reply
Thanks given by:
#3
Thank you for the report, it seems the example in the LuaAPI docs is outdated. There used to be an additional file that you would download and it would provide the SendMessage function for you; since then the function has moved directly to the API.

To fix this, you need to change your line 17 to:
Player:SendMessage("Usage: /explode [playername]")

your line 28 to:
Player:SendMessageSuccess(Split[2] .. " was successfully exploded")

and your line 39 to:
Player:SendMessageFailure(Split[2] .. " was not found")
Reply
Thanks given by:
#4
I haven't tried yet, but I'm sure this is the problem. Thank you!
Reply
Thanks given by:
#5
Hey guys,

First of all, C++ server, Awesome!

Now for my problem,

I seem to have the same errors, but fixing it as detailed above didn't help.
Trying to get a simple command going, but my Lua knowledge is limited.

Here is the code: "As I originaly figured it should work"
function Initialize(Plugin)
        Plugin:SetName("STONED")
        Plugin:SetVersion(1)
        
        -- Hooks
        
        cPluginManager:AddHook(cPluginManager.HOOK_EXECUTE_COMMAND, RollJoint);
        
        PLUGIN = Plugin
         
        -- Command Bindings

        cPluginManager.BindCommand("/j", "STONED.j", RollJoint, " - is rolling a joint."); -- RollJoint binding       
        
        LOG("Initialised " ..Plugin:GetName() .. " v." .. Plugin:GetVersion())
        return true
end

function OnDisable()
        LOG(PLUGIN:GetName() .. " is shutting down.")
end

-- Default callback name is: function OnExecuteCommand(Player, Command)

-- RollJoint Command: 
function RollJoint(Player, j)
        SendMessage(Player, " " .. Player:GetName(Player) .. " is rolling a joint.")
        return true
end

And here is the drama the console gave me:

--Warn [21:43:59] LUA: Plugins/STONED/stoned.lua:31: attempt to call global 'SendMessage' (a nil value)
--Warn [21:43:59] Stack trace:
--Warn [21:43:59] Plugins/STONED/stoned.lua(31): (no name)
--Warn [21:43:59] Stack trace end
--Warn [21:43:59] Error in plugin STONED calling function <callback>()
--Warn [21:43:59] LUA: Plugins/STONED/stoned.lua:31: attempt to call method 'GetName' (a nil value)
--Warn [21:43:59] Stack trace:
--Warn [21:43:59] Plugins/STONED/stoned.lua(31): (no name)
--Warn [21:43:59] Stack trace end
--Warn [21:43:59] Error in plugin STONED calling function <callback>()[/php]

Tried changing SendMessage to Player:SendMessage and the other solutions pointed out in the post above..
What am I overlooking here?
Reply
Thanks given by:
#6
What errors did you get?
Reply
Thanks given by:
#7
(09-12-2014, 03:55 AM)STR_Warrior Wrote: What errors did you get?

And here is the drama the console gave me:

--Warn [21:43:59] LUA: Plugins/STONED/stoned.lua:31: attempt to call global 'SendMessage' (a nil value)
--Warn [21:43:59] Stack trace:
--Warn [21:43:59] Plugins/STONED/stoned.lua(31): (no name)
--Warn [21:43:59] Stack trace end
--Warn [21:43:59] Error in plugin STONED calling function <callback>()
--Warn [21:43:59] LUA: Plugins/STONED/stoned.lua:31: attempt to call method 'GetName' (a nil value)
--Warn [21:43:59] Stack trace:
--Warn [21:43:59] Plugins/STONED/stoned.lua(31): (no name)
--Warn [21:43:59] Stack trace end
--Warn [21:43:59] Error in plugin STONED calling function <callback>()
Reply
Thanks given by:
#8
Hello!

Change
Player:GetName(Player)
to
Player:GetName()

SendMessage
to
Player:SendMessage and remove the "Player," there

regards,
Seadragon91
Reply
Thanks given by:
#9
(09-12-2014, 04:47 AM)Seadragon91 Wrote: Hello!

Change
Player:GetName(Player)
to
Player:GetName()

SendMessage
to
Player:SendMessage and remove the "Player," there

regards,
Seadragon91

Hello!

Thank you very much, this works! Greatly appreciated.
I do get a console msg tho saying:

Player "myplayername" tried to execute command "/j" that was stopped by the HOOK_EXECUTE_COMMAND hook

No idea what that is about. EDIT: It gives the same console remark on /me command.
Also, this command is visible for all players, and not just the player typing the command. For this command all players should see it, so that works as intended, but how would I go about in making it visible to only the player typing the command?
Reply
Thanks given by:
#10
(09-12-2014, 05:04 AM)Lo_Pan Wrote: Player "myplayername" tried to execute command "/j" that was stopped by the HOOK_EXECUTE_COMMAND hook

function Initialize(Plugin)
        Plugin:SetName("STONED")
        Plugin:SetVersion(1)
        
        -- Hooks
        
        cPluginManager:AddHook(cPluginManager.HOOK_EXECUTE_COMMAND, RollJoint);
        
        PLUGIN = Plugin
         
        -- Command Bindings

        cPluginManager.BindCommand("/j", "STONED.j", RollJoint, " - is rolling a joint."); -- RollJoint binding       
        
        LOG("Initialised " ..Plugin:GetName() .. " v." .. Plugin:GetVersion())
        return true
end

function OnDisable()
        LOG(PLUGIN:GetName() .. " is shutting down.")
end

-- Default callback name is: function OnExecuteCommand(Player, Command)

-- RollJoint Command: 
function RollJoint(Player, j)
        SendMessage(Player, " " .. Player:GetName(Player) .. " is rolling a joint.")
        return true
end

Why do you use the same function for a Hook and a command?
It is called first from the hook, then you after sending the message to the player, you are returning "true", which means "hook successfully executed", if you want that the command get executed, you have to return "false".
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)