SetGarageOpen: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
Line 133: Line 133:
addEventHandler("onResourceStart", getResourceRootElement(), function(res)
addEventHandler("onResourceStart", getResourceRootElement(), function(res)
     for ID, pos in pairs(garages) do
     for ID, pos in pairs(garages) do
         if (pos and type(pos) == "table") then
         if pos then
            for _, subPos in ipairs(pos) do
            if (type(pos) == "table") then
                 createGarageColShape(subPos, ID)
                for _, subPos in ipairs(pos) do
                    createGarageColShape(subPos, ID)
                end
            else
                 createGarageColShape(pos, ID)
             end
             end
        else
            createGarageColShape(pos, ID)
         end
         end
     end
     end

Revision as of 19:35, 22 July 2024

This function opens or closes the specified garage door in the world.

[[{{{image}}}|link=|]] Note: setGarageOpen does not work with ID 32 (Pay 'n' Spray near Royal Casino). This garage has been disabled by Rockstar Games due to floor collision issues (see TheJizzy's video "BETA Leftovers and Glitches" at 12:12 timestamp). You can remove the door by using removeWorldModel and recreating it for later with moveObject.

Syntax

bool setGarageOpen ( int garageID, bool open )

Required Arguments

  • garageID: The garage ID that represents the garage door being opened or closed.
  • isOpen: A boolean indicating whether or not to open the door.

Returns

Returns true if successful, false if an invalid garage id was given.

Example

Click to collapse [-]
Server

This example opens a garage door when a player enters a collision shape near it, and closes it when they leave:

GARAGE_ID = 25

-- create a collision shape and attach event handlers to it when the resource starts
addEventHandler("onResourceStart", getResourceRootElement(),
function (resource)
	local garageCube = createColCuboid(1337, 194, 28, 6, 10, 4)
	addEventHandler("onColShapeHit", garageCube, onGarageCubeHit)
	addEventHandler("onColShapeLeave", garageCube, onGarageCubeLeave)
end)

-- open the door when someone enters the garage's collision shape
function onGarageCubeHit(hitElement, matchingDimension)
	if (getElementType(hitElement) == "player") then
		-- check to make sure the door is closed
		if (not isGarageOpen(GARAGE_ID)) then
			-- open the door
			setGarageOpen(GARAGE_ID, true)
		end
	end
end

-- close the door when someone leaves the garage's collision shape
function onGarageCubeLeave(leaveElement, matchingDimension)
	if (getElementType(leaveElement) == "player") then
		-- check to make sure the door is open
		if (isGarageOpen(GARAGE_ID)) then
			-- close the door
			setGarageOpen(GARAGE_ID, false)
		end
	end
end
Click to collapse [-]
Server

This example opens each garage door when a player enters a collision shape near it, and closes it when they leave:

local garages = {
    [0]  = Vector3(1643.43, -1520.3, 14.3438),
    [1]  = nil, -- LSPD Police Impound Garage (not working)
    [2]  = Vector3(1877.41, -2096.51, 14.0391),
    [3]  = Vector3(1843.37, -1856.32, 13.875),
    [4]  = Vector3(1798.69, -2146.73, 14),
    [5]  = Vector3(1698.91, -2088.74, 14.1406),
    [6]  = Vector3(2741.07, -2004.78, 14.875),
    [7]  = Vector3(2644.86, -2039.23, 14.0391),
    [8]  = Vector3(2071.48, -1831.42, 14.5625),
    [9]  = Vector3(2505.52, -1690.99, 14.3281),
    [10] = Vector3(1041.35, -1025.93, 32.6719),
    [11] = Vector3(1024.98, -1029.35, 33.1953),
    [12] = Vector3(488.28, -1734.7, 12.3906),
    [13] = Vector3(322.4141, -1769.0312, 5.25),
    [14] = Vector3(1353.48, -626.63, 109.82),
    [15] = Vector3(-2716.35, 217.48, 5.3828),
    [16] = Vector3(-2730.47, 72.32, 5.3516),
    [17] = Vector3(-2454.12, -123.06, 26.9844),
    [18] = Vector3(-1935.86, 239.53, 35.3516),
    [19] = Vector3(-1904.53, 277.9, 42.9531),
    [20] = Vector3(-2102.93, -16.05, 36.4844),
    [21] = Vector3(-2026.91, 129.41, 30.4531),
    [22] = Vector3(-2038.93, 178.81, 29.9375),
    [23] = Vector3(-2162.03, 654.66, 53.375),
    [24] = Vector3(-1786.81, 1209.42, 25.8359),
    [25] = Vector3(-2105.2, 896.93, 77.4453),
    [26] = nil, -- SFPD Police Impound Garage (not working)
    [27] = Vector3(-2425.73, 1027.99, 52.2812),
    [28] = Vector3(-2696.01, 821.45, 50.8516),
    [29] = nil, -- LVPD Police Impound Garage (not working)
    [30] = Vector3(1586.26, 1222.7, 19.75),
    [31] = Vector3(2609.52, 1438.37, 11.5938),
    [32] = nil, -- Pay 'n' Spray (Royal Casino) (not working)
    [33] = Vector3(2386.66, 1043.6, 11.5938),
    [34] = Vector3(2449.55, 698.08, 11.6797),
    [36] = Vector3(1968.74, 2162.49, 12.0938),
    [37] = Vector3(1408.64, 1902.69, 11.6797),
    [38] = Vector3(1278.7, 2529.81, 11.3203),
    [39] = Vector3(929.55, 2012.06, 11.6797),
    [40] = Vector3(-1420.55, 2591.16, 57.7422),
    [41] = Vector3(-100, 1111.41, 21.6406),
    [42] = Vector3(-360.77, 1194.26, 20.5938),
    [43] = Vector3(429.98, 2546.52, 17.3516),
    [44] = Vector3(-389.59, 2227.91, 42.9219),
    [45] = {
        Vector3(397.48, 2476.63, 19.5156),
        Vector3(412.12, 2476.63, 19.5156)
    },
    [46] = Vector3(-2113.04, -2460.62, 30.9141),
    [47] = Vector3(720.02, -462.52, 16.8594),
    [48] = Vector3(2231.24, 168.73, 27.7734),
    [49] = Vector3(786.01, -492.87, 17.6328)
}

local function createGarageColShape(pos, ID)
    local col = createColSphere(pos.x, pos.y, pos.z, 7)
    
    addEventHandler("onColShapeHit", col, function(hitElement, matchingDimension)
        if getElementType(hitElement) == "player" then
            if not isGarageOpen(ID) then
                setGarageOpen(ID, true)
            end
        end
    end)
    
    addEventHandler("onColShapeLeave", col, function(leaveElement, matchingDimension)
        if getElementType(leaveElement) == "player" then
            if isGarageOpen(ID) then
                setGarageOpen(ID, false)
            end
        end
    end)
end

addEventHandler("onResourceStart", getResourceRootElement(), function(res)
    for ID, pos in pairs(garages) do
        if pos then
            if (type(pos) == "table") then
                for _, subPos in ipairs(pos) do
                    createGarageColShape(subPos, ID)
                end
            else
                createGarageColShape(pos, ID)
            end
        end
    end
end)

See Also