Jump to content

Bilal135

Members
  • Posts

    843
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by Bilal135

  1. Try this,

    addEventHandler ( "onClientMarkerHit", root, 
        function ( hitElement ) 
            if source == marker then
                if ( hitElement == localPlayer and getElementType ( hitElement ) == "player" ) then 
                    outputChatBox("working")
                    destroyElement(marker)    
                end
            end
        end
    )

     

  2. This should do the job. (Not tested)

    local vehicle = {};
    
    function isVehicleOccupied(vehicleID)
    	if vehicle[vehicleID] = true then
    		return true
    	end
    	return false
    end
    
    function createVehicleForPlayer(player, cmd, model)
    	if not tonumber(model) then return false end
    	if isVehicleOccupied(model) then return false end
    	local x, y, z = getElementPosition(player)
    	local veh = createVehicle(model, x + 5, y, z)
    	if not veh then return false end
    	vehicle[model] = true;
    end
    addCommandHandler("vh", createVehicleForPlayer)
    
    function destroyVehicleOnQuit(player)
    	if not isPedInVehicle(player) then return false end
    	local veh = getPedOccupiedVehicle(player)
    	if not veh then return false end
    	local model = getElementModel(veh)
    	if not isVehicleOccupied(model) then return false end
    	if not isElement(veh) then return false end
    	destroyElement(veh)
    	vehicle[model] = false;
    end
    addEventHandler("onPlayerQuit", root, destroyVehicleOnQuit)

     

  3. local crewData = {};
    
    function createCrew(crew, tag, r, g, b)
        if not tostring(crew) or not tostring(tag) or not tonumber(r) or not tonumber(g) or not tonumber(b) then
            return outputDebugString("* createCrew: Wrong args.", 1)
        end
        if doesCrewExist(crew) then
            return outputDebugString("* createCrew: Crew already exists.", 1)
        end
        crewData[crew] = {
            Members = {},
            crewTag = tag,
            Color = {r, g, b},
            Leader = {},
            Experience = 0,
        };
        outputDebugString(crew.." successfully created.")
        return crew
    end
    
    -- getPlayerCrew
    function getPlayerCrew(player)
        if not player then 
            return outputDebugString("* getPlayerCrew: Wrong args.", 1)
        end
        local account = getPlayerAccount(player)
        if isGuestAccount(account) then return false end
        local accountName = getAccountName(account)
        for k, v in pairs(crewData) do
            for k1, v1 in pairs(crewData[v].Members) do -- Error: attempt to index field '?' (a nil value)
                if v1 == accountName then
                    return v
                end
            end
        end
        return false
    end 
    
    -- addPlayer
    function addPlayer(player, crew)
        if not player or not tostring(crew) then 
            return outputDebugString("* addPlayer: Wrong args.", 1)
        end
        local account = getPlayerAccount(player)
        local accountName = getAccountName(account)
        if isGuestAccount(account) then return end
        if getPlayerCrew(player) then return end -- getPlayerCrew is called here.
        table.insert(crewData[crew].Members, accountName)
        return true
    end

    I've been scratching my head over this error for a while now. crewData[v]. Members should not be nil. I've tested that by adding a few test strings in Members table (in addPlayer function), and then by this command. It does output the strings, but still gives error when getPlayerCrew is called.

    function listMembers(player, cmd, crew)
        for k, v in pairs(crewData[crew].Members) do
            outputChatBox(v, root)
        end
    end
    addCommandHandler("fmem", listMembers)

    Thanks.

  4. I have heard that inserting players in a new table with 'for i = 1, #playerTable' method reduces the time it takes for the loop to complete, as compared to the more common 'for k, v in ipairs / pairs' loop. I know that the first method is more efficient from Lua guides, but I fail to understand the logic behind it.

    In the case of the common loop, it would directly iterate over the table returned by getElementsByType. As for the for i = 1 method, we would first have to add the contents of the table (returned by getElementsByType) in a new indexed table, in this instance, playerTable, and then we would loop over it once again to get the players in it.

    I wrote the following code to compare the time it takes to finish each type of iteration. However, both results were 0. Perhaps, it is because the difference is so minute that the machine is not able to identify it? If that's so, whats the point of preferring the first type over the second, when the difference is barely noticeable?

    local players = getElementsByType("player");
    local playerTable = {};
    local sT = getTickCount();
    
    -- Supposed to be a faster way?
    for i = 1, #players do
        playerTable[i] = players; -- Insert players in a new indexed table.
        outputChatBox(getPlayerName(playerTable[i][1]), root);
        local eT = getTickCount();
        outputChatBox(eT - sT, root); -- Output: 0
    end
    
    -- Compared to the more common loop
    for k, v in ipairs(players) do
        outputChatBox(getPlayerName(v, root)); -- Directly access the contents of the table.
        local endTime = getTickCount();
        outputChatBox(endTime - sT, root); -- Output: 0
    end

    EDIT:

    I realised there is no need to insert the players in a new indexed table. (Still the same output)

    for i = 1, #players do
        outputChatBox(getPlayerName(players[i]), root);
        local eT = getTickCount();
        outputChatBox(eT - sT, root); -- Output: 1
    end
    
    -- Compared to the more common loop
    for k, v in ipairs(players) do
        outputChatBox(getPlayerName(v, root)); -- Directly access the contents of the table.
        local endTime = getTickCount();
        outputChatBox(endTime - sT, root); -- Output: 1
    end
    • Why is the first sort of iteration faster? 
    • Would the difference be even larger, if the number of players were extraordinarily large? If that's the case, I'd understand why we would want to use the first loop over the second.
  5. From what I have come to understand is, when you type 'test', you want to create an object, and if it already exists, then delete it. Imo you don't even need an event to do that, but if you want to do it your way, it would be better to pass the object as an argument to the trigger event.

    local obj
    
    addEventHandler("test", function(plr, cmd)
        if not isElement(obj) then
            obj = createObject(1234, getElementPosition(plr)) -- spawn object with '1234' id at our current position.
        else
            triggerEvent("deleteObject", obj)
        end
    end)
    
    addEvent("deleteObject", true)
    addEventHandler("deleteObject", function(object)
        if not isElement(object) then
            return outputChatBox("Object does not exist.", source, 255, 0)
        end
        destroyElement(object)
        end
    end)

    An alternative and much simpler way to do this would be like this (but of course it depends on how you intend your script to work).

    local obj
    
    addEventHandler("test", function(plr, cmd)
        if not isElement(obj) then
            obj = createObject(1234, getElementPosition(plr)) -- spawn object with '1234' id at our current position.
        else
            outputChatBox("Destroying object.", plr)
            destroyElement(obj)
        end
    end)

    -----------

    @stPatrick, with all due respect, where did you trigger the 'trigger' event? Custom events have to be manually triggered.

     

  6. Line 20: Replace m_dthru with m_dthru[index]. You may also want to make sure that r, g, b, and a are defined, and for the sake of testing, remove -1 from third arg of createMarker, just to make sure that isn't causing any problem.

    If it still does not work, try running the script server side. (Don't forget to change the outputChatBox arguments in this case)

  7. Check if this works.

    local drivethru = {
        {2376.21, -1908.87, 13.11},
        {2409.42, -1488.03, 23.56},
        {789.30, -1619.01, 13.11}
    }
    
    function usunGui()
        outputChatBox("test2")
    end
    
    function triggerujKupno()
        outputChatBox("test")
    end
    
    local index = 1;
    local m_dthru = {};
    
    for k,v in ipairs(drivethru) do
        m_dthru[index] = createMarker(v[1], v[2], v[3]-1, "cylinder", 2.0, 255, 255, 0, 200)
        createBlipAttachedTo(m_dthru, 17, 1, r, g, b, a, 0, 300)
        index = index + 1;
    end
    
    addEventHandler("onMarkerHit", root,
        function(hitElement, matchingDimension)
            if (source == m_dthru) then
                if ((getElementType(hitElement) == "vehicle") and matchingDimension) then
                    triggerujKupno()
                end
            end
        end
    )
    
    addEventHandler("onMarkerLeave", root,
        function(leaveElement, matchingDimension)
            if (source == m_dthru) then
                if ((getElementType(leaveElement) == "vehicle") and matchingDimension) then
                usunGui()
                end
            end
        end
    )

     

  8. Try this,

    local drivethru = {
        {2376.21, -1908.87, 13.11},
        {2409.42, -1488.03, 23.56},
        {789.30, -1619.01, 13.11}
    }
    
    function usunGui()
        outputChatBox("test2")
    end
    
    function triggerujKupno()
        outputChatBox("test")
    end
    
    local index = 1;
    local m_dthru = {};
    
    for k,v in ipairs(drivethru) do
        m_dthru[index] = createMarker(v[1], v[2], v[3]-1, "cylinder", 2.0, 255, 255, 0, 200)
        createBlipAttachedTo(m_dthru, 17, 1, r, g, b, a, 0, 300)
        addEventHandler("onMarkerLeave", m_dthru[index], usunGui, false)
        addEventHandler("onMarkerHit", m_dthru[index], triggerujKupno, false)
        index = index + 1;
    end

     

×
×
  • Create New...