SetElementModel: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(improved script examples)
m (Edited language change)
 
(3 intermediate revisions by one other user not shown)
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])
This example allows players to change their own skin with a command (/skin [ID])
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local spam = {}
local spam = {}


function setSkin(player, cmd, skin)
function setSkin(player, cmd, skin)
if spam[player] then
    if spam[player] and getTickCount() - spam[player] < 4000 then
return outputChatBox("You cannot change skin that often!", player, 255, 0, 0, false)
        return outputChatBox("You cannot change skin that often!", player, 255, 0, 0)
end
    end
 
    skin = skin and tonumber(skin)


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


skin = skin and tonumber(skin)
    if skin and skin <= 99999 then -- what do we know about dynamic ped ID range?
if skin and string.len(skin) <= 4 then
        setElementModel(player, skin)
setElementModel(player, skin)
        spam[player] = getTickCount()
spam[player] = setTimer(function() spam[player] = nil end, 4000, 1)
    else
else
        outputChatBox("Invalid skin ID!", player, 255, 0, 0)
outputChatBox("Invalid skin ID!", player, 255, 0, 0)
    end
end
end
end
addCommandHandler("skin", setSkin)
addCommandHandler("skin", setSkin)
function cleanUp()
    if spam[source] then
        spam[source] = nil
    end
end
addEventHandler("onPlayerQuit", root, cleanUp)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 50: Line 60:


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


local theVehicle = getPedOccupiedVehicle(player)
local theVehicle = getPedOccupiedVehicle(player)
if not (theVehicle and getVehicleController(theVehicle) == player) then
return outputChatBox("You must be driving a vehicle in order to change it!", player, 255, 0, 0)
end


if isValidModel(newModel) and isElement(theVehicle) then
if isValidModel(newModel) and isElement(theVehicle) then
setElementModel(theVehicle, newModel)
setElementModel(theVehicle, newModel)
spam[player] = setTimer(function() spam[player] = nil end, 2500, 1)
spam[player] = getTickCount()
else
else
outputChatBox("Invalid vehicle ID!", player, 255, 0, 0)
outputChatBox("Invalid vehicle ID!", player, 255, 0, 0)
Line 72: Line 86:
return false
return false
end
end
function cleanUp()
if spam[source] then
spam[source] = nil
end
end
addEventHandler("onPlayerQuit", root, cleanUp)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 77: Line 98:
==See Also==
==See Also==
{{Element functions}}
{{Element functions}}
[[ru:setElementModel]]

Latest revision as of 17:36, 6 June 2022

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 allows players to change their own skin with a command (/skin [ID])

local spam = {}

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

    skin = skin and tonumber(skin)

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

    if skin and skin <= 99999 then -- what do we know about dynamic ped ID range?
        setElementModel(player, skin)
        spam[player] = getTickCount()
    else
        outputChatBox("Invalid skin ID!", player, 255, 0, 0)
    end
end
addCommandHandler("skin", setSkin)

function cleanUp()
    if spam[source] then
        spam[source] = nil
    end
end
addEventHandler("onPlayerQuit", root, cleanUp)
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] and getTickCount() - spam[player] < 2500 then
		return outputChatBox("Don't spam vehicle changes!", player, 255, 0, 0)
	end

	local theVehicle = getPedOccupiedVehicle(player)
	
	if not (theVehicle and getVehicleController(theVehicle) == player) then
		return outputChatBox("You must be driving a vehicle in order to change it!", player, 255, 0, 0)
	end

	if isValidModel(newModel) and isElement(theVehicle) then
		setElementModel(theVehicle, newModel)
		spam[player] = getTickCount()
	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

function cleanUp()
	if spam[source] then
		spam[source] = nil
	end
end
addEventHandler("onPlayerQuit", root, cleanUp)

See Also