TP to vehicle function

Hello, i am working to make tp to vehicle working, what do you think?

RegisterNetEvent('es_admin:spawnTp')
AddEventHandler('es_admin:spawnTp', function(v)
	local carid = GetHashKey(v)
	local playerPed = GetPlayerPed(0)
	local playerVeh = GetVehiclePedIsIn(playerPed, 0)
	local playerCoords = GetEntityCoords(GetPlayerPed(player), 1)
	
if GetPlayerPed(player) ~= 0 then
		if IsPedInAnyVehicle(GetPlayerPed(player), true) then
			local playerVeh = GetVehiclePedIsIn(GetPlayerPed(player), 0)
			local seat = 0
			if (IsVehicleSeatFree(playerVeh, 0) == false) then
				seat = 1
				if (IsVehicleSeatFree(playerVeh, 1) == false) then
					seat = 2
			end
		end
	end
  end
end)

You never once use carid nor playerPed further in the script. If you’d actually do that for playerPed you’d save some function calls.
Furthermore I don’t know if GetPlayerPed(0) returns the right thing, I’ve always seen -1 as the argument but whatever.

Please don’t put this as a release if it’s 1) not ready for release and 2) you’re just looking for help/info.

Have a nice day.

Lol what?

I don’t think this would work… At all.

If you want to get the current players ped you should be doing GetPlayerPed(-1) if you’re trying to get another players ped you should be passing their ID (I don’t remember which) to the event.

You haven’t defined “player” this should throw an error. In fact, it shouldn’t run at all in the client because if this.

What’s the point in this? It’s never used again.

I’m going to move this topic to discussion instead of release as this doesn’t look like a release to me and looks more like you’re trying to get help.

Okay, sorry for the topic there…

I changed to it:

RegisterNetEvent('es_admin:spawnTp')
AddEventHandler('es_admin:spawnTp', function(v)
	local playerPed = GetPlayerPed(-1)
	local player = tonumber(action)
	local playerVeh = GetVehiclePedIsIn(playerPed, 0)
	local playerCoords = GetEntityCoords(GetPlayerPed(player), 1)
	
if GetPlayerPed(player) ~= 0 then
		if IsPedInAnyVehicle(GetPlayerPed(player), true) then
			local playerVeh = GetVehiclePedIsIn(GetPlayerPed(player), 0)
			local seat = 0
			if (IsVehicleSeatFree(playerVeh, 0) == false) then
				seat = 1
				if (IsVehicleSeatFree(playerVeh, 1) == false) then
					seat = 2
			end
		end
	end
  end
end)

Is it better?

dont use if (IsVehicleSeatFree(playerVeh, 1) == false) then,

prefer if not IsVehicleSeatFree(playerVeh, 1) then

So it can work? :PPPP PD: It doesn’t

RegisterNetEvent('es_admin:spawnTp')
AddEventHandler('es_admin:spawnTp', function()
	local myPed = PlayerPedId()
	local myPedVeh = GetVehiclePedIsIn(myPed, 0)
	local myPedCoords = GetEntityCoords(myPed)
	local seat = 0
	if IsPedInAnyVehicle(myPed, true) then
		if (IsVehicleSeatFree(myPedVeh, 0) == false) then
			seat = 1
			if (IsVehicleSeatFree(myPedVeh, 1) == false) then
				seat = 2
			end
		end
	end
end)

here a correct code, but i dont understand what you are trying to do

The command doesn’t work, i have this command that calls this function:

TriggerEvent('es:addGroupCommand', 'gotocar', "mod", function(source, args, user)
		if(GetPlayerName(tonumber(args[2])))then
			local player = tonumber(args[2])

			-- User permission check
			TriggerEvent("es:getPlayerFromId", player, function(target)
				if(target)then
					if(tonumber(target.permission_level) > tonumber(user.permission_level))then
						TriggerClientEvent("chatMessage", source, "SYSTEM", {255, 0, 0}, "You're not allowed to target this person!")
						return
					end

					TriggerClientEvent('es_admin:spawnTp', player, source)

					TriggerClientEvent('chatMessage', player, "SYSTEM", {255, 0, 0}, "You have been teleported to by ^2" .. GetPlayerName(source))
					TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Teleported to player ^2" .. GetPlayerName(player) .. "")
				end
			end)
		else
			TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Incorrect player ID!")
		end
end, function(source, args, user)
	TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Insufficienct permissions!")
end)

But doesn’t work, it only shows: You have teleported to “name”

I see, you just took this snippets out of my modified version of the Scorpion Trainer, but don’t know how it works and how to use it. :joy:

Baguette?

Haha yeah i was about to ask you…any idea how to make it as a command? haha in the trainer it works very well
Yep baguette :smiley:

doesn’t surprises me :joy:

Your are missing the actual teleport part. You just have the part about getting a free seat.

If you can wait till tomorrow(19.06) evening, I can help you.

But i am more like tortilla de patata :smiley: Spanish!!

1 Like

Sure i can wait ^^ :smiley:

1 Like

Sooo, I need some Informations. You want to make a function to teleport into the vehicle of another player? Did I understood it right?

btw, I just can help with the Client stuff, I still did not figure out the ServerEvent Stuff

Yeah just to tp into another players vehicle nothing more !! ^^

I am just going to post my code here, after optimizing it. So you can check, modifiy and use it.

Okay waiting for you :))

I optimized it, but couldn’t test it, because none of my friend (which play FiveM too) are online.
BUt I think it would work how it is supposed :smile:

--CLIENTSIDED
local player, op

AddEventHandler('es_admin:spawnTp', function(v)
	player = tonumber(data.action) --You have to get the player somehow else, this is just how I use it in the Trainer
	local localplayerCoords = GetEntityCoords(GetPlayerPed(-1), 1)
	local playerCoords = GetEntityCoords(GetPlayerPed(player), 1)

	if GetPlayerPed(player) ~= 0 then
		if GetPlayerPed(player) ~= GetPlayerPed(-1) then
			if IsPedInAnyVehicle(GetPlayerPed(-1), false) and (GetPedInVehicleSeat(GetVehiclePedIsIn(GetPlayerPed(-1), 0), -1) == GetPlayerPed(-1)) then
				SetEntityCoords(GetVehiclePedIsIn(GetPlayerPed(-1), 0), 0.0, 0.0, 0.0)
				FreezeEntityPosition(GetVehiclePedIsIn(GetPlayerPed(-1), 1), true)
			else
				SetEntityCoords(GetPlayerPed(-1), 0.0, 0.0, 0.0)
				FreezeEntityPosition(GetPlayerPed(-1), true)
			end
			op = true
		else
			drawNotification("~r~Can't Teleport To Yourself.")
		end
	else
		drawNotification("~r~Player " .. player + 1 .. " Doesn't Exist.")
	end
end)

Citizen.CreateThread(function() --Teleport To Online Player
	while true do
		Citizen.Wait(0)
		local playerCoords = GetEntityCoords(GetPlayerPed(player), 1)
		local localplayerCoords = GetEntityCoords(GetPlayerPed(-1), 1)
		local localvehCoords = GetEntityCoords(GetVehiclePedIsIn(GetPlayerPed(-1), 1))
		
		if (op == true) then
			if (localvehCoords.z == 0.0) or (localplayerCoords.z == 0.0) then
				while playerCoords.z == 0.0 do
					playerCoords = GetEntityCoords(GetPlayerPed(player), 1)
				end
				if IsPedInAnyVehicle(GetPlayerPed(-1), false) and (GetPedInVehicleSeat(GetVehiclePedIsIn(GetPlayerPed(-1), 0), -1) == GetPlayerPed(-1)) then
					FreezeEntityPosition(GetVehiclePedIsIn(GetPlayerPed(-1), false))
					SetEntityCoords(GetVehiclePedIsIn(GetPlayerPed(-1), 0), playerCoords)
				else
					if IsPedInAnyVehicle(GetPlayerPed(player), true) and IsAnyVehicleSeatEmpty(GetVehiclePedIsIn(GetPlayerPed(player), 0)) then
						local playerVeh = GetVehiclePedIsIn(GetPlayerPed(player), 0)
						local seat = GetVehicleModelMaxNumberOfPassengers(GetEntityModel(playerVeh)) - 2
						local seatindex = -1
						while seatindex <= seat and ((IsVehicleSeatFree(playerVeh, seatindex)) == false) do
							Citizen.Wait(0)
							seatindex = seatindex + 1
						end
						FreezeEntityPosition(GetPlayerPed(-1), false)
						SetPedIntoVehicle(GetPlayerPed(-1), playerVeh, seatindex)
						drawNotification("~g~Teleported Into " .. player + 1 .. ". Players Vehicle.")
					else
						FreezeEntityPosition(GetPlayerPed(-1), false)
						SetEntityCoords(GetPlayerPed(-1), playerCoords)
						drawNotification("~g~Teleported To " .. player + 1 .. ". Player.")
					end
				end
			end
			op = false
		end
	end
end)

Like I already said, I couldn’t test it, but it should work.

1 Like

I have put it in cl_admin.lua and i have created this command in sv_admin:

TriggerEvent('es:addGroupCommand', 'gotocar', "mod", function(source, args, user)
		if(GetPlayerName(tonumber(args[2])))then
			local player = tonumber(args[2])

			-- User permission check
			TriggerEvent("es:getPlayerFromId", player, function(target)
				if(target)then
					if(tonumber(target.permission_level) > tonumber(user.permission_level))then
						TriggerClientEvent("chatMessage", source, "SYSTEM", {255, 0, 0}, "You're not allowed to target this person!")
						return
					end

					TriggerClientEvent('es_admin:spawnTp', source, player)

					TriggerClientEvent('chatMessage', player, "SYSTEM", {255, 0, 0}, "You have been teleported to by ^2" .. GetPlayerName(source))
					TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Teleported to player ^2" .. GetPlayerName(player) .. "")
				end
			end)
		else
			TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Incorrect player ID!")
		end
end, function(source, args, user)
	TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Insufficienct permissions!")
end)

But doesn’t work, it only says you have teleported to X…
what did i do wrong?

Like I said, I can just provide you the clientsided code, you still have to edit it a bit so it is working with your event.

For example this part:

	player = tonumber(data.action) --You have to get the player somehow else, this is just how I use it in the Trainer

I get the player I want to teleport to through data.action but you don’t, you get it through your Client Event I think. So you have to edit it.

Unfortunately I can’t help with this, because I still struggle with understanding Server --> Client Events. I just provide the (hopefully) working clientsided code.