FANDOM


Variables Edit

Variables can be used to store numbers and strings, and later work with them. As their name suggests, they can be changed anytime, even during a Lua script.

First, they have to be defined at the very start of the code. Defining them is easy:

spawned=0

A variable named "spawned" has been created, and its value is currently 0. The variable later can be increased - for example, by 1 - by making them equal with themselves + 1, something like this:

spawned=spawned+1

It forces the "spawned" variable to change its value to itself's value plus one, so, it increases itself by 1. With this knowledge and some hooks, one can make a spawning counter, as shown below.

spawned=0

addhook("spawn","counter")
function counter(id)
  spawned=spawned+1
end

Putting them after text is done by connecting the text's last quotation mark (") with two dots (..), then writing the variable name. Works exactly the same when putting before text, except firstly the variable name is written, then the two dots, then the beginning quotation mark of the text. Here is the result:

msg("Spawned players:"..spawned) --After text.
msg(spawned.." players has been spawned so far.") --Before text.
msg("Currently, "..spawned.." players spawned in this server today.")
--Using the first two methods, this one can be done too.

Variable can be even "numbered" by using arrays. Using this "numbering", one can use only one variable to count how many times every player is spawned, individually, for example. But because of the arrays, one need an initArray code, shown here:

function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
end

It has to be put before the variables. Now, after it is done, the variables could be defined, but before that, one needs to do a little code, shown below:

function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
end

spawned=initArray(32) --Because the maximum number of players is 32.

This allows the spawned variable to be different for 32 players, in this case. Setting the variable for a predefined player works like this:

spawned[id]=0 --Where "id" is the player's ID.

Using this, join, and spawn hooks, a simple spawning counter for every player can be created like this:

function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
end

spawned=initArray(32) --Because the maximum number of players is 32.

addhook("join","countjoin")
function countjoin(id)
  spawned[id]=0 --The player just joined, and never been spawned before.
end

addhook("spawn","spawncount")
function spawncount(id)
  spawned[id]=spawned[id]+1 --The player got spawned.
  msg2(id,"You have been spawned "..spawned[id].." times since you joined.")
  --msg2 is used to show the message to the player with the ID number "id".
end

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.