SetElementModel

From Multi Theft Auto: Wiki
Revision as of 19:19, 14 March 2020 by Dutchman101 (talk | contribs) (improved script examples)
Jump to navigation Jump to search

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