Jump to content

One vehicle for one player


3eBpA

Recommended Posts

Hello guys, I'm asking u to help me to create one vehicle for one player, if vehicle exists, it should spawn near player, I've got something like this :

local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') then 
    createVehicle(403, 872, -1216, 18, 0 , 0 , 180); 
    else 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 

Link to comment
  • MTA Team
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 

Link to comment
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 

ok, one more nooby question, how to delete spawned vehicle ?

Link to comment
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 

ok, one more nooby question, how to delete spawned vehicle ?

destroyElement 

Link to comment
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 

thanks :)

ok, one more nooby question, how to delete spawned vehicle ?

destroyElement 

Link to comment
  • MTA Team

Try this, tho i don't know if it will work, i'm from my cell xD

  
local vehicles = { } 
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    local veh = createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    table.insert(vehicles,hitElement,veh) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 
function delete () 
destroyElement(vehicles[source] 
table.remove(vehicles,vehicles[source]) 
setElementData(hitElement,"vehicle",false) 
end 
addCommandHandler("delete",delete) 
  

Link to comment
Try this, tho i don't know if it will work, i'm from my cell xD
  
local vehicles = { } 
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    local veh = createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    table.insert(vehicles,hitElement,veh) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 
function delete () 
destroyElement(vehicles[source] 
table.remove(vehicles,vehicles[source]) 
setElementData(hitElement,"vehicle",false) 
end 
addCommandHandler("delete",delete) 
  

hey wait, client sided scripts works only for 1 client ? Or for all clients on the server ?

Link to comment
  • MTA Team

Yes, they work only for the client that uses it. Didn't know how you wanted to use it, tho you can use it client side this way:

  
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    veh = createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 
function delete () 
destroyElement(veh) 
setElementData(hitElement,"vehicle",false) 
end 
addCommandHandler("delete",delete) 
  

PD: Do know that this way, you can't enter the vehicle.

Link to comment
Yes, they work only for the client that uses it. Didn't know how you wanted to use it, tho you can use it client side this way:
  
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    veh = createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 
function delete () 
destroyElement(veh) 
setElementData(hitElement,"vehicle",false) 
end 
addCommandHandler("delete",delete) 
  

PD: Do know that this way, you can't enter the vehicle.

ohh, well, I just need to create a script that can change color and destroy car on the markers

Link to comment
Try this, tho i don't know if it will work, i'm from my cell xD
  
local vehicles = { } 
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    local veh = createVehicle(403, x, y+1, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    table.insert(vehicles,hitElement,veh) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler( "onMarkerHit", marker1, MarkerHit ) 
function delete () 
destroyElement(vehicles[source] 
table.remove(vehicles,vehicles[source]) 
setElementData(hitElement,"vehicle",false) 
end 
addCommandHandler("delete",delete) 
  

also, one more question in setElementData I can put any data that I want ?

Link to comment

Well, guys, I know that I'm very annoying but can you help me again ? :3

why this code doesn't works ?

  
local workmarker = createMarker(860, -1208, 16, 'cylinder', 2.0, 0, 255, 0 , 150); 
  
function TakeJob(hitElement, matchingDimension) 
    local element = getElementType(hitElement); 
    if(getElementType(hitElement) == 'Player') and getElementData(hitElement,"trucker") == false then 
    setPedSkin(source, 202); 
    setElementData(hitElement,"trucker",true); 
    setPlayerTeam(source, truckers); 
    elseif getElementData(hitElement,"trucker") == true then 
    outputChatBox("You are already a trucker"); 
    end 
end 
addEventHandler("onMarkerHit", workmarker, TakeJob); 

Link to comment
  • MTA Team
local workmarker = createMarker(860, -1208, 16, 'cylinder', 2.0, 0, 255, 0 , 150); 
trucks = createTeam("Truckers",255,0,0) 
function TakeJob(hitElement, matchingDimension) 
    local element = getElementType(hitElement); 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"trucker") == false then 
    setPedSkin(source, 202); 
    setElementData(hitElement,"trucker",true); 
    setPlayerTeam(hitElement, trucks); 
    elseif getElementData(hitElement,"trucker") == true then 
    outputChatBox("You are already a trucker"); 
    end 
end 
addEventHandler("onMarkerHit", workmarker, TakeJob); 

Link to comment
local workmarker = createMarker(860, -1208, 16, 'cylinder', 2.0, 0, 255, 0 , 150); 
trucks = createTeam("Truckers",255,0,0) 
function TakeJob(hitElement, matchingDimension) 
    local element = getElementType(hitElement); 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"trucker") == false then 
    setPedSkin(source, 202); 
    setElementData(hitElement,"trucker",true); 
    setPlayerTeam(hitElement, trucks); 
    elseif getElementData(hitElement,"trucker") == true then 
    outputChatBox("You are already a trucker"); 
    end 
end 
addEventHandler("onMarkerHit", workmarker, TakeJob); 

oh, I've got team creation, I forgot to share it here :P, ohh, I'm stupid, forgot to setElementData, thanks again, I hope I won't disturb u today anymore with my stupid questions

Link to comment
Relax man, no problem we are here to help you out :)

ok, well, then my last script, it always writes me that I'm a trucker but I am not :?

  
local truckers = createTeam("Truckers", 222, 153, 25); 
  
local marker1 = createMarker(872, -1208, 16, 'cylinder', 2.0, 255, 0, 0, 150) -- create myMarker 
local workmarker = createMarker(860, -1208, 16, 'cylinder', 2.0, 0, 255, 0 , 150); 
  
function MarkerHit( hitElement, matchingDimension ) 
    local elementType = getElementType( hitElement ) 
    local playermoney = getPlayerMoney(hitElement) 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"vehicle") == false then 
    local x,y,z = getElementPosition(hitElement) 
    createVehicle(403, x + 10, y, z, 0 , 0 , 180) 
    setElementData(hitElement,"vehicle",true) 
    elseif getElementData(hitElement,"vehicle") == true then 
    outputChatBox("You have got already a vehicle"); 
    end 
end 
addEventHandler("onMarkerHit", marker1, MarkerHit ) 
  
function TakeJob(hitElement, matchingDimension) 
    local element = getElementType(hitElement); 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"trucker") == false then 
    setPedSkin(source, 202); 
    setElementData(hitElement,"trucker",true); 
    setPlayerTeam(hitElement, truckers); 
    elseif getElementData(hitElement,"trucker") == true then 
    outputChatBox("You are already a trucker"); 
    end 
end 
addEventHandler("onMarkerHit", workmarker, TakeJob); 

Link to comment
  • MTA Team

That's because you're obtaining an element data representing if you're a trucker or not. The element data is not cleared unless you re join the server. You can also clear the element data, by using this:

setElementData(hitElement,"trucker",false) 

You can store the data using:

setAccountData 

Or using the SQL functions.

Link to comment
That's because you're obtaining an element data representing if you're a trucker or not. The element data is not cleared unless you re join the server. You can also clear the element data, by using this:
setElementData(hitElement,"trucker",false) 

You can store the data using:

setAccountData 

Or using the SQL functions.

yes, I have analyzed the situation and I'm thinking about creating a /exit command so I can exit trucker's team, this is a topic about commands, yes ?

https://forum.multitheftauto.com/viewtopic.php?f=148&t=43227

Link to comment
  • MTA Team

I strongly recommend not to use that if you're new to lua because that is mainly OOP.

Use this:

  
function kickme() 
if getElementData(source,"trucker") == true then 
setElementData(source,"trucker",false) 
setPlayerTeam(source,nil) 
end 
addCommandHandler("notrucker",kickme) 

Link to comment
I strongly recommend not to use that if you're new to lua because that is mainly OOP.

Use this:

  
function kickme() 
if getElementData(source,"trucker") == true then 
setElementData(source,"trucker",false) 
setPlayerTeam(source,nil) 
end 
addCommandHandler("notrucker",kickme) 

well, I'm .Net programmer, who wants to learn Lua, so I'm not new to OOP =\

and I think, I'll create a gui window with "hire" and "exit" buttons

Link to comment
Well, if you do know OOP then you have a lot of points in favor if you plan on using OOP to programm. In MTA 1.4 OOP will be incorporated in the MTA engine functions.

Please let me know if you need any further assistance.

why does skin don't puts on the player ? :o

function TakeJob(hitElement, matchingDimension, thePlayer) 
    local element = getElementType(hitElement); 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"trucker") == false 
    and getElementData(hitElement,"vehicle") == false then 
    setPedSkin(source, 206); 
    setElementData(hitElement,"trucker",true); 
    setPlayerTeam(hitElement, truckers); 
    setMarkerColor(marker1, 255, 0, 0, 150); 
    outputChatBox("To end you job stand back on this marker and you will automatically end your job"); 
    elseif getElementData(hitElement,"trucker") == true then 
    outputChatBox("You have ended your job"); 
    setPlayerTeam(hitElement, unemployedteam); 
    setElementData(hitElement,"trucker",false); 
    setMarkerColor(marker1, 255, 0, 0, 0); 
    end 
end 
addEventHandler("onMarkerHit", workmarker, TakeJob); 

Link to comment
Well, if you do know OOP then you have a lot of points in favor if you plan on using OOP to programm. In MTA 1.4 OOP will be incorporated in the MTA engine functions.

Please let me know if you need any further assistance.

why does skin don't puts on the player ? :o

function TakeJob(hitElement, matchingDimension, thePlayer) 
    local element = getElementType(hitElement); 
    if(getElementType(hitElement) == 'player') and getElementData(hitElement,"trucker") == false 
    and getElementData(hitElement,"vehicle") == false then 
    setPedSkin(source, 206); 
    setElementData(hitElement,"trucker",true); 
    setPlayerTeam(hitElement, truckers); 
    setMarkerColor(marker1, 255, 0, 0, 150); 
    outputChatBox("To end you job stand back on this marker and you will automatically end your job"); 
    elseif getElementData(hitElement,"trucker") == true then 
    outputChatBox("You have ended your job"); 
    setPlayerTeam(hitElement, unemployedteam); 
    setElementData(hitElement,"trucker",false); 
    setMarkerColor(marker1, 255, 0, 0, 0); 
    end 
end 
addEventHandler("onMarkerHit", workmarker, TakeJob); 

Replace it with:

setElementModel(hitElement, 206); 

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...