Jump to content

Marquis Shop


Eshtiz

Recommended Posts

Small problem,

ERROR: maruis-shop/shop.lua:26: attempt to index field 'position' (a nil value)

for some reasons, the boats somewhat fly & kill people, it's getting quite annoying and I have no idea what to do,

(also about all my old topics, they're solved).

here's the script:

local function populateSpots( force ) 
    -- get all players positions to check if there's no nearby player 
    local players = { } 
    if not force then 
        for key, value in ipairs( getElementsByType( "player" ) ) do 
            if getElementDimension( value ) == 0 and getElementInterior( value ) == 0 then 
                players[ #players + 1 ] = { getElementPosition( value ) } 
            end 
        end 
    end 
     
    -- get vehicle positions in case any is near 
    local vehicles = { } 
    for key, value in ipairs( getElementsByType( "vehicle" ) ) do 
        if getElementDimension( value ) == 0 and getElementInterior( value ) == 0 then 
            vehicles[ #vehicles + 1 ] = { getElementPosition( value ) } 
        end 
    end 
     
    for key, value in ipairs( shops ) do 
        if #value.spots > 0 and #value.prices then 
            -- check that no player is near the first spot (as it should be close with the rest) 
            local canPopulate = true 
            for k, v in ipairs( players ) do 
                if getDistanceBetweenPoints3D( value.position[1], value.position[2], value.position[3], v[1], v[2], v[3] ) < 200 then 
                    canPopulate = false 
                    break 
                end 
            end 
             
            if canPopulate then 
                -- create new ones instead 
                for k, v in ipairs( value.spots ) do 
                    local data = value.prices[ math.random( 1, #value.prices ) ] 
                     
                    if v.vehicle and isElement( v.vehicle ) then 
                        -- set it back to its respawn position 
                        respawnVehicle( v.vehicle ) 
                        -- already have a car, change it with another one 
                        if setElementModel( v.vehicle, data.model ) then 
                            -- if we actually changed the model, assign a random color if we can 
                            local color1, color2 = getRandomVehicleColor( v.vehicle ) 
                            if color1 then 
                                setVehicleColor( v.vehicle, color1, color2 or color1, color1, color2 or color1 ) 
                            end 
                        end 
                    else 
                        -- make sure no vehicle is nearby 
                        local canPopulate = true 
                        for _, x in ipairs( vehicles ) do 
                            if getDistanceBetweenPoints3D( x[1], x[2], x[3], v[1], v[2], v[3] ) < 1 then 
                                canPopulate = false 
                                break 
                            end 
                        end 
                         
                        if canPopulate then 
                            -- create a new vehicle as we didn't have one before 
                            local vehicle = createVehicle( data.model, unpack( v ) ) 
                            setTimer(setVehicleFrozen, 1500, 1, vehicle, true) 
                             
                            -- assign it to our shop 
                            setElementParent( vehicle, value.root ) 
                             
                            -- it's locked or people would drive it off 
                            setVehicleLocked( vehicle, true ) 
                             
                            setElementData(vehicle, "shop.car", true, true) 
                             
                            -- we don't want people to trash 'em up really 
                            setVehicleDamageProof( vehicle, true ) 
                             
                            -- save it 
                            v.vehicle = vehicle 
                        end 
                    end 
                end 
            else 
                -- shouldn't update the old ones, respawn ours 
                for k, v in ipairs( value.spots ) do 
                    if v.vehicle and isElement( v.vehicle ) then 
                        -- already have a car, respawn it 
                        respawnVehicle( v.vehicle ) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler( "onResourceStart", resourceRoot, 
    function( ) 
        for key, value in ipairs( shops ) do 
            -- create an element for every shop 
            value.root = createElement( "vehicle-shop", "vehicle-shop " .. key ) 
             
            -- easier lookup 
            shops[ value.root ] = value 
        end 
         
        -- initalize our vehicles 
        populateSpots( true ) 
         
        -- populate with other cars every minute 
        setTimer( populateSpots, 60000, 0 ) 
    end 
) 
  
addEventHandler( "onVehicleStartEnter", resourceRoot, 
    function( player ) 
        if not isPedDead( player ) then 
            -- show popup for buying 
            triggerClientEvent( player, getResourceName( resource ) .. ":buyPopup", source ) 
        end 
         
        -- don't let him enter the vehicle 
        cancelEvent( ) 
    end 
) 
  
addEventHandler( "onVehicleEnter", resourceRoot, 
    function( ) 
        -- vehicles can't be entered 
        cancelEvent( ) 
    end 
) 
  
addEvent( getResourceName( resource ) .. ":buyVehicle", true ) 
addEventHandler( getResourceName( resource ) .. ":buyVehicle", getRootElement(), 
function ( vehicle ) 
    if (isElement(vehicle)) then 
        if (client) then 
            if exports.global:hasMoney( client, getVehiclePrice( vehicle ) ) then 
                local cost = getVehiclePrice( vehicle ) 
                local create_vehicle = exports['vehicle-system']:create( client, vehicle, cost ) 
            else 
                outputChatBox("You do not have enough money to buy this vehicle.", client, 255, 0, 0) 
            end 
        end 
    end  
end 
) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...