[Help]Police Call
#1
PLUGIN = nil
function Initialize(Plugin)
	Plugin:SetName("AltisLife")
	Plugin:SetVersion(1)
  
  --Loads Plugin Commands
  
	cPluginManager.BindCommand("/alert", "", AlertPol, "- Alert the police")

	-- Hooks
	
	PLUGIN = Plugin -- NOTE: only needed if you want OnDisable() to use GetName() or something like that
	
	-- Command Bindings

	LOG("Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
	return true
end
--Sets alarm triggered by AlertPol when the arguments are valid.
function setAlarm(playerName, Message)
  Root:BroadcastChat("CNN News: " ..Message.. "  From: " ..playerName)
end
--Function for Alert Police
function AlertPol(Split, Player)
  if (#Split ~= 2) then
    Player:SendMessageFailure("You're Arguments Are Invalid, here's the format: /alert 'Message'")
    return True
  end
  if(#Split ~= 1) then
    playerName = Player:GetName()
    Message = Split[2]
    Player:SendMessageSuccess("Your report has been sent to the police, please hold on")
    --Triggers setAlarm function, go to line 19 for more info
    setAlarm(playerName, Message)
    return True
  end
  
end
function OnDisable()
	LOG(PLUGIN:GetName() .. " is shutting down...")
end

So this is my code for an upcoming Altis Life plugin, which is of course gonna have a different name. I'm waiting for MCServer to be less derpy with ai and stuff, and add minecarts.

Console reports no problems on startup, but when executing /alarm Test...
-{INFO} Your report has been sent to the police, please hold on

-{INFO}Something went wrong while executing command "/alert test'

Then on the console...

[Image: xtcul7J.png]

So, i'm new to LUA programming, so can you guys explain to me in depth what the error is, and how I can prevent it next time? Thanks Smile
EDIT: Yes I did copy the template from the documentation, and snipped off some of the explode example.
Reply
Thanks given by:
#2
Hello, welcome to the forum.

Please use the [ shcode=lua ] tag for embedding Lua code next time, it is much more readable that way.

When Lua says "attempt to index global 'XYZ', it means that you're using XYZ and Lua doesn't know what it is. It even provides you with the location of the problem, and the stacktrace usually shows you which functions were called that lead to the problem.

In your particular case, the error says that line 34 called function setAlarm and it broke down on line 21 becayse the symbol 'Root' is not known to Lua. And indeed, there is no such symbol; I suppose you meant to write
cRoot:Get():BroadcastChat(...)
Reply
Thanks given by: GeekSalt
#3
Also, your AlertPol function is somewhat weird - it first checks if the number of parameters is inequal to 2, and if so, issues an error message. Then, although it knows that the number of parameters is 2, it still checks if it isn't equal to 1. There's no need for the "#Split ~= 1" condition.

I expect you want players to be able to send multi-word messages, too; in order for that to work, you should change the first condition to "#Split == 1" and then use "Message = table.concat(Split, " ", 2)" - this will take the Split table and join all items together with a space in between, starting with string #2.
Reply
Thanks given by: GeekSalt
#4
(09-16-2014, 05:39 PM)xoft Wrote: Also, your AlertPol function is somewhat weird - it first checks if the number of parameters is inequal to 2, and if so, issues an error message. Then, although it knows that the number of parameters is 2, it still checks if it isn't equal to 1. There's no need for the "#Split ~= 1" condition.

I expect you want players to be able to send multi-word messages, too; in order for that to work, you should change the first condition to "#Split == 1" and then use "Message = table.concat(Split, " ", 2)" - this will take the Split table and join all items together with a space in between, starting with string #2.

This post was heaven in words, thanks!
Reply
Thanks given by:
#5
Also you're sometimes returning "True" which is a nil value. You should return "true" instead. Lua is case sensitive.
Reply
Thanks given by: GeekSalt
#6
(09-17-2014, 12:30 AM)STR_Warrior Wrote: Also you're sometimes returning "True" which is a nil value. You should return "true" instead. Lua is case sensitive.
Need to fix that too, that's what was giving me the red infoTongue
Thanks
Reply
Thanks given by:




Users browsing this thread: 3 Guest(s)