Jump to content

warpPedIntoVehicle works not


LabiVila

Recommended Posts

Hello,

can you help me fix this part? It's the 'warpPedIntoVehicle', every time I use this I get 'Network Trouble' and I don't know why,

 

    for a, b in ipairs (getElementsByType ("player")) do
        local mySpawn = math.random (1, #mapSpawns)
        
        for c, d in ipairs (mapSpawns) do
            if (c == mySpawn) then
                local veh = createVehicle (d.id, d.x, d.y, d.z)
                
                if veh then
                    outputDebugString ("created veh")
                end
                
                warpPedIntoVehicle (b, veh)
                
                setCameraTarget (b, b)
                fadeCamera (b, true)
            end
        end
    end

mapSpawns is fine, I can output any value of it. And I as well get the 'created veh' from outputDebugString. Dunno what's wrong...

Edited by LabiVila
Link to comment

How about this code? Instead of looping through the "mapSpawns" table to find the spawn that has the correct key, you can directly access the table. See line 3 below.

for _, player in ipairs (getElementsByType ("player")) do
  local spawnIndex = math.random (1, #mapSpawns)
  local spawn = mapSpawns[spawnIndex]
  
  local veh = createVehicle (spawn.id, spawn.x, spawn.y, spawn.z)
  if veh then
    outputDebugString ("created veh")
  end

  warpPedIntoVehicle (player, veh)

  setCameraTarget (player, player)
  fadeCamera (player, true)
end

It's also a good idea to name variables something more informative, so I have made this change for you too.

Depending on how many items you have in `mapSpawns`, and how many players you have, this could have been the source of your issue.

It's unlikely this was the actual cause of the network trouble, but nevertheless, give it a go and get back to us.

  • Like 1
Link to comment

I just solved that by setting a timer to warpPedIntoVehicle (100 ms). I don't know why but somehow it's solved. Does it matter that I call the functions with: function () without event handlers? Maybe that messed me up earlier. I'll definitely change it to the style your style tho

Link to comment
3 minutes ago, LabiVila said:

Does it matter that I call the functions with: function () without event handlers?

It is only necessary to use event handlers with function variables if you plan to use removeEventHandler on that event later on.

 

This will not actually remove the event:

-- Add an event
addEventHandler("onPlayerJoin", root,
  function()
    outputChatBox("Player joined!")
  end
)

-- Now to remove the event (WILL NOT ACTUALLY REMOVE THE EVENT)
removeEventHandler("onPlayerJoin", root,
  function()
    outputChatBox("Player joined!")
  end
)

It does not work because even though both functions contain the same code, they are technically different functions.

 

This will remove the event, however:

function announceJoin()
  outputChatBox("Player joined!")
end

-- Add an event
addEventHandler("onPlayerJoin", root, announceJoin)

-- Now to remove the event
removeEventHandler("onPlayerJoin", root, announceJoin)

 

  • Like 1
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...