Jump to content

How can you trigger this script with a command?


Moony

Recommended Posts

Scrolling through the tutorials, I found a script that displays players in the server when joining and quitting.

function playerCount ( ) 
    outputChatBox("#ff8800[server]: #ffffffPlayers: #ffffff(" .. getPlayerCount() .. "#ffffff)" , root, 255, 0, 0, true) 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), playerCount ) 
addEventHandler ( "onPlayerQuit", getRootElement(), playerCount ) 

What would I have to add to trigger it with a command, such as "/players"?

Link to comment
  • Scripting Moderators
3 hours ago, Moony said:

Scrolling through the tutorials, I found a script that displays players in the server when joining and quitting.


function playerCount ( ) 
    outputChatBox("#ff8800[server]: #ffffffPlayers: #ffffff(" .. getPlayerCount() .. "#ffffff)" , root, 255, 0, 0, true) 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), playerCount ) 
addEventHandler ( "onPlayerQuit", getRootElement(), playerCount ) 

What would I have to add to trigger it with a command, such as "/players"?

function checkOnline(player, cmd)
	if player then
		outputChatBox(#getElementsByType("player"))
	end
end
addCommandHandler("players", checkOnline)

 

Link to comment
5 hours ago, Moony said:

Scrolling through the tutorials, I found a script that displays players in the server when joining and quitting.


function playerCount ( ) 
    outputChatBox("#ff8800[server]: #ffffffPlayers: #ffffff(" .. getPlayerCount() .. "#ffffff)" , root, 255, 0, 0, true) 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), playerCount ) 
addEventHandler ( "onPlayerQuit", getRootElement(), playerCount ) 

What would I have to add to trigger it with a command, such as "/players"?

addCommandHandler('players',root
  function(thePlayer)
    outputChatBox("Current number of players: "..tostring(getPlayerCount()),thePlayer)
    end

Edited by Skadoosh
Is the code blocks working? I am sorry I am new to the forums ...
Link to comment
8 hours ago, majqq said:

function checkOnline(player, cmd)
	if player then
		outputChatBox(#getElementsByType("player"))
	end
end
addCommandHandler("players", checkOnline)

 

Excuse my second beginner's question.

I'm struggling with the functions. Do you come up with them? Is there a list to such commands?
Scrolling through this, this, and this doesn't show any 'checkOnline' function. It does show a 'check' function, but it doesn't seem to be one for such script.

UPDATE: is it normal that it doesn't work from the console, yet it does when tiggered from the in-game chatbox?

Edited by Moony
Info acquired from test.
Link to comment
  • Scripting Moderators

checkOnline is custom function, which doesn't exist normally in MTA.

For console command - delete/comment check for player.

function checkOnline(player, cmd)
	--if player then
		outputChatBox(#getElementsByType("player"))
	--end
end
addCommandHandler("players", checkOnline)

 

Edited by majqq
Link to comment
On 19/02/2020 at 02:09, majqq said:

checkOnline is custom function, which doesn't exist normally in MTA.

Ok, so you've got the official functions for mta on one side. They can be found in the link provided above. On the other side, custom functions are functions created by the developer. Having a 'function functionName' will asign the name to a function, and between that and 'end', you add the official functions that will do what the developer wants, such as throwing something to the chatbox, creating elements, teleporting the player, changing names, setting timers, and many many other things. Is this correct?

Link to comment
  • Scripting Moderators
3 hours ago, Moony said:

Ok, so you've got the official functions for mta on one side. They can be found in the link provided above. On the other side, custom functions are functions created by the developer. Having a 'function functionName' will asign the name to a function, and between that and 'end', you add the official functions that will do what the developer wants, such as throwing something to the chatbox, creating elements, teleporting the player, changing names, setting timers, and many many other things. Is this correct?

Yes, but those do not need to be official functions. It could be operations on variables/tables, and other custom functions.

Edited by majqq
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...