FAQ / issues
changelog
developers
history
contact
game-monitor.com

This week's script shows a random clothes changing system. We'll be setting the change interval to 2 seconds in this example, but keep in mind there is a small loading pause when changing clothes: a larger interval is advised, to avoid disrupting gameplay.

Forum support thread: forum.mtavc.com
Please ask your questions in this forum thread.

Video footage: scriptvideo3.avi - XviD codec (640x480, 30fps)
Complete script: script3.lua


Counting the clothes

First, we are going to count how many items there are in each category, and store the counts in this table:

local count = {}

We have 18 clothing types to check (these will be documented along with the rest of IDs on release):

for type = 0, 17 do
	--initialize a counter
	local clothes = 0
	--we increase the counter until it's an invalid clothing ID
	while getClothesByTypeIndex ( type, clothes ) ~= false
		do clothes = clothes + 1 end
	--remember the last one didn't exist: we've got to remove it
	--from the count when storing it
	count[type] = clothes - 1
end

Note: This step may be replaced by declaring a hardcoded table with fixed clothes counts, as all unmodded games have the same number of clothes in each category.

Creating the clothes change function

Now we'll create the clothes changing function itself. It will have to take a few things into account, which will be described in a moment. The declaration starts here:

function doChangeClothes ()

Now, we're going to run it for every connected player. This is done by first retrieving a table of every player in the server, then looping through that table and changing their clothes. First we retrieve a table of our players:

local players = getElementsByType ( "player" )

Next we use Lua's generic for loop to go through every player in that table.

for i, player in players do

First, we have to choose a random type using Lua's math.random. We'll leave tattoos (type IDs 4-12) out because they're not visible with clothes on anyways:

		local accessory = math.random ( 0, 1 )
		if accessory == 1 then
			--pick an accessory type
			type = math.random ( 13, 17 )
		else
			--pick hairstyles/shirts/trousers/shoes type
			type = math.random ( 0, 3 )
		end

Complete suits (type ID 17) are applied on top of shirts and trousers, so we'll want to remove them if any of these two are selected:

		if type == 0 or type == 2 then
			removePlayerClothes ( player, 17 )
		end

Now that we've got our type, we have to select a random piece of clothing within it:

		local clothing = math.random ( 0, count[type] )

Only adding these clothes to the player is left to do.

		--we get the texture and model via getClothesByTypeIndex
		texture, model = getClothesByTypeIndex ( type, clothing )
		--and we add that piece of clothing to the player.
		addPlayerClothes ( player, texture, model, type )

We mustn't forget to close the "for" loop, and the function declaration.

	end
end

Starting the function

Lastly, we'll trigger our function repeatedly when this script "resource" (these will be explained later) is started.

addEventHandler("onResourceStart", getRootElement(), "clothesStart")
function clothesStart ( resourcename )

Our clothesStart function should only be triggered when this script loads. The following line makes the script ignore any resource loads other than its own:

if resourcename ~= getThisResource() then return end

Finally, we'll employ the setTimer function, as in the last tutorial, to trigger the function every two seconds.

--0 means infinite times, until the timer is killed
	setTimer ( "doChangeClothes", 2000, 0 )
end

That's it for this week. If you have any questions please feel free to head down to our forums.