SetElementModel: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(improved script examples)
Line 22: Line 22:
==Example==
==Example==
<section class="server" name="Example 1 (Server)" show="true">
<section class="server" name="Example 1 (Server)" show="true">
This example lets players change their own skin with a command (/skin [ID])
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler ( "changeveh",
local spam = {}
    function ( thePlayer, command, newModel )
 
        local theVehicle = getPedOccupiedVehicle ( thePlayer ) -- get the vehicle the player is in
function setSkin(player, cmd, skin)
        newModel = tonumber ( newModel )                         -- try to convert the string argument to a number
if spam[player] then
        if theVehicle and newModel then                           -- make sure the player is in a vehicle and specified a number
return outputChatBox("You cannot change skin that often!", player, 255, 0, 0, false)
            setElementModel ( theVehicle, newModel )
end
        end
 
    end
if getElementModel(player) == skin or isPedDead(player) then return end
)
 
skin = skin and tonumber(skin)
if skin and string.len(skin) <= 4 then
setElementModel(player, skin)
spam[player] = setTimer(function() spam[player] = nil end, 4000, 1)
else
outputChatBox("Invalid skin ID!", player, 255, 0, 0)
end
end
addCommandHandler("skin", setSkin)
</syntaxhighlight>
</syntaxhighlight>
After the above code is executed, a player can get in any vehicle and execute e.g. the command "/changeveh 520" to change it into a hydra.
</section>
</section>
<section class="server" name="Example 2 (Server)" show="true">
<section class="server" name="Example 2 (Server)" show="true">
This will continually change an object model every 2.5 seconds at the location -1084.52, -1634.81, 76.36 (Truth's farm).
This example allows players to transform their current vehicle into another vehicle model.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
myobject = createObject ( 5822, -1084.52, -1634.81, 76.36 )
local spam = {}
-- We create an initial object element. I choose object model 5822 to begin with.


function objectRandomization ()
function changeMyVehicle(player, command, newModel)
    local randomobjectnumber = math.random(1, 18000)
if spam[player] then
    -- Choose a random number between 1 and 18000 as a whole integer and assign it to
return outputChatBox("Don't spam vehicle changes!", player, 255, 0, 0)
    -- the variable 'randomobjectnumber'
end
    setElementModel ( myobject, randomobjectnumber )
 
    -- Change our object appearance by applying a new model ID
local theVehicle = getPedOccupiedVehicle(player)
 
if isValidModel(newModel) and isElement(theVehicle) then
setElementModel(theVehicle, newModel)
spam[player] = setTimer(function() spam[player] = nil end, 2500, 1)
else
outputChatBox("Invalid vehicle ID!", player, 255, 0, 0)
end
end
end
addCommandHandler("changeveh", changeMyVehicle)


setTimer ( objectRandomization, 2500, 0 )
function isValidModel(modelID)
-- Every 2.5 seconds, the function 'objectRandomization' is called by this timer.
modelID = tonumber(modelID)
-- Each time the function runs, it changes the object model by applying a new whole-
if modelID and modelID >= 400 and modelID <= 611 then
-- integer random object ID. This timer is called an infinite amount of times since 
return true
-- its repeat value is set to 0.
end
return false
end
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 19:19, 14 March 2020

Sets the model of a given element. This allows you to change the model of a player (or ped), a vehicle or an object.

Syntax

bool setElementModel ( element theElement, int model )

OOP Syntax Help! I don't understand this!

Method: element:setModel(...)
Variable: .model
Counterpart: getElementModel


Required Arguments

  • theElement: the element you want to change.
  • model: the model ID to set.
    • For players/peds: A GTASA player model (skin) ID. See Character Skins.
    • For vehicles: The vehicle ID of the vehicle being changed.
    • For objects/projectiles/weapons: An int specifying the model id.

Returns

Returns true if successful, false otherwise.

Example

Click to collapse [-]
Example 1 (Server)

This example lets players change their own skin with a command (/skin [ID])

local spam = {}

function setSkin(player, cmd, skin)
	if spam[player] then
		return outputChatBox("You cannot change skin that often!", player, 255, 0, 0, false)
	end

	if getElementModel(player) == skin or isPedDead(player) then return end

	skin = skin and tonumber(skin)
	if skin and string.len(skin) <= 4 then
		setElementModel(player, skin)
		spam[player] = setTimer(function() spam[player] = nil end, 4000, 1)
	else
		outputChatBox("Invalid skin ID!", player, 255, 0, 0)
	end
end
addCommandHandler("skin", setSkin)
Click to collapse [-]
Example 2 (Server)

This example allows players to transform their current vehicle into another vehicle model.

local spam = {}

function changeMyVehicle(player, command, newModel)
	if spam[player] then
		return outputChatBox("Don't spam vehicle changes!", player, 255, 0, 0)
	end

	local theVehicle = getPedOccupiedVehicle(player)

	if isValidModel(newModel) and isElement(theVehicle) then
		setElementModel(theVehicle, newModel)
		spam[player] = setTimer(function() spam[player] = nil end, 2500, 1)
	else
		outputChatBox("Invalid vehicle ID!", player, 255, 0, 0)
	end
end
addCommandHandler("changeveh", changeMyVehicle)

function isValidModel(modelID)
	modelID = tonumber(modelID)
	if modelID and modelID >= 400 and modelID <= 611 then
		return true
	end
	return false
end

See Also