Jump to content

Attaching object to vehicle (tiggering from client side)


Xwad

Recommended Posts

Hi i made this script but its not working:/ if i press r in the vehicle then it will trigger a server event and attach an object , but its not working:( If i add onPlayerVehicleEnter event in server side then its working:/

so when i press r then thevdebugscript 3 says:

Bad Argumentum @ 'getElementPosition' [Expected element at argument 1,got nil]

Bad Argumentum @ 'getElementModel' [Expected element at argument 1,got nil]

client

  
function onVehicleEnter() 
bindKey ( "r", "down", bind_c ) 
end 
addEventHandler ( "onClientPlayerVehicleEnter", getRootElement(), onVehicleEnter ) 
  
  
  
function bind_c() 
triggerServerEvent ( "create", resourceRoot ) 
end 
  

server

  
weapOff = {} 
weapOff[422] = {-0.1, -1.9, -0.2, 0, 30, 60}  
weapOff[470] = {-0.1, 0.1, 1.0, 0, 30, 60} 
  
  
  
  
function createVehicleMachinegun (veh, thePlayer) 
 local x,y,z = getElementPosition(veh) 
 local mid = getElementModel(veh) 
 if weapOff[mid] then 
  local weapon = createObject ( 362, x+weapOff[mid][1],y+weapOff[mid][2],z+weapOff[mid][3]) 
  local weapon_base = createObject ( 336, x+weapOff[mid][1],y+weapOff[mid][2],z) 
  attachElements ( weapon_base, veh, weapOff[mid][1], weapOff[mid][2], weapOff[mid][3], 0, 0, 0) 
  attachElements ( weapon, weapon_base, 0, -0.3, 0.9, weapOff[mid][4], weapOff[mid][5], weapOff[mid][6]) 
  setElementData(veh, "weapon", weapon_base) 
  setElementData(veh, "weapon2", weapon) 
  setElementData(veh, "playerOnWeapon", nil) 
   outputChatBox ( "Machinegun attached!" ) 
 end 
end 
addEvent( "create", true ) 
addEventHandler( "create", root, createVehicleMachinegun ) 
  
  
  

Link to comment

Try this

CLIENT SCRIPT

  
function bind_c() 
    -- get the veh first 
    local veh = getPedOccupiedVehicle( localPlayer ) 
     
    if veh then 
        -- if the veh is valid 
        -- you need an argument IF the handled function has some arguments 
        triggerServerEvent ( "create", resourceRoot, veh ) 
    end 
end 
  

SERVER-SIDE

  
weapOff = {} 
weapOff[422] = {-0.1, -1.9, -0.2, 0, 30, 60} 
weapOff[470] = {-0.1, 0.1, 1.0, 0, 30, 60} 
  
  
  
  
function createVehicleMachinegun (veh) 
 local x,y,z = getElementPosition(veh) 
 local mid = getElementModel(veh) 
 if weapOff[mid] then 
  local weapon = createObject ( 362, x+weapOff[mid][1],y+weapOff[mid][2],z+weapOff[mid][3]) 
  local weapon_base = createObject ( 336, x+weapOff[mid][1],y+weapOff[mid][2],z) 
  attachElements ( weapon_base, veh, weapOff[mid][1], weapOff[mid][2], weapOff[mid][3], 0, 0, 0) 
  attachElements ( weapon, weapon_base, 0, -0.3, 0.9, weapOff[mid][4], weapOff[mid][5], weapOff[mid][6]) 
  setElementData(veh, "weapon", weapon_base) 
  setElementData(veh, "weapon2", weapon) 
  setElementData(veh, "playerOnWeapon", nil) 
   outputChatBox ( "Machinegun attached!" ) 
 end 
end 
addEvent( "create", true ) 
addEventHandler( "create", root, createVehicleMachinegun ) 
  

Link to comment
-- Client 
triggerServerEvent ( "thatthing", localPlayer, arg1, arg2 ) 
triggerServerEvent ( "thatthing2", localPlayer, arg1, arg2, arg3, arg4 ) 
triggerServerEvent ( "thatthing3", localPlayer, arg1 ) 
  
-- Server -- 
addEventHandler ( "thatthing", root, function ( arg1, arg2 ) ... 
addEventHandler ( "thatthing2", root, function ( arg1, arg2, arg3 ) ... 
addEventHandler ( "thatthing3", root, function ( arg1, arg2 ) ...   -- here arg2 is nil 

Here client is the player, who triggered it.

source is the one, who is source of the event. We used localPlayer in triggerServerEvent, so the same player is the source.

example:

-- Client -- 
triggerServerEvent ( "dothatthing", localPlayer, "hi", 255, 0, 0 ) 
  
-- Server -- 
addEvent ( "dothatthing", true )  
addEventHandler ( "dothatthing", root, function ( arg1, arg2, arg3, arg4 ) 
      outputChatBox ( getPlayerName(client)..": "..arg1, root, arg2, arg3, arg4 ) 
end ) 
  
-- Output: 
-- "Bonus: hi" in red color 

Link to comment

One more question. I want to make that when the minigun is already attached when i enter the vehicle, then when i press r it will be detached else it will attach it. The problem is that when i enter the vehicle then its not get the element data and will attach when i press r

debugscript3:

Bad Argumentum @ 'getElementData' [Expected element argumentum at argumentum 1, got nil]

Client

  
function onVehicleEnter(theElement) 
if isElement ( getElementData ( veh, "weapon" ) ) then 
guiSetVisible(text2, true) 
bindKey ( "r", "down", bind_d ) 
else 
guiSetVisible(text, true) 
bindKey ( "r", "down", bind_c ) 
end 
end 
addEventHandler ( "onClientPlayerVehicleEnter", getRootElement(), onVehicleEnter ) 
  
  
  
  
  
function bind_c() 
    local veh = getPedOccupiedVehicle( localPlayer ) 
    if veh then 
        guiSetVisible(text, false) 
        guiSetVisible(text2, true) 
        unbindKey ( "r", "down", bind_c ) 
        bindKey ( "r", "down", bind_d ) 
        triggerServerEvent ( "create", resourceRoot, veh ) 
    end 
end 
  
  
function bind_d() 
    local veh = getPedOccupiedVehicle( localPlayer )  
    if veh then 
        guiSetVisible(text, true) 
        guiSetVisible(text2, false) 
        unbindKey ( "r", "down", bind_d ) 
        bindKey ( "r", "down", bind_c ) 
        triggerServerEvent ( "destroy", resourceRoot, veh ) 
    end 
end 
  

server

  
weapOff = {} 
weapOff[422] = {-0.1, -1.9, -0.2, 0, 30, 60}  
weapOff[470] = {-0.1, 0.1, 1.0, 0, 30, 95} 
  
  
  
function createVehicleMachinegun (veh) 
 local x,y,z = getElementPosition(veh) 
 local mid = getElementModel(veh) 
 if weapOff[mid] then 
  local weapon = createObject ( 362, x+weapOff[mid][1],y+weapOff[mid][2],z+weapOff[mid][3]) 
  local weapon_base = createObject ( 336, x+weapOff[mid][1],y+weapOff[mid][2],z) 
  attachElements ( weapon_base, veh, weapOff[mid][1], weapOff[mid][2], weapOff[mid][3], 0, 0, 0) 
  attachElements ( weapon, weapon_base, 0.14, -0.3, 0.9, weapOff[mid][4], weapOff[mid][5], weapOff[mid][6]) 
  setElementData(veh, "weapon", weapon_base) 
  setElementData(veh, "weapon2", weapon) 
  setElementData(veh, "playerOnWeapon", nil) 
   outputChatBox ( "Machinegun attached!" ) 
 end 
end 
addEvent( "create", true ) 
addEventHandler( "create", root, createVehicleMachinegun ) 
  
  
  
  
function destroyVehicleMachinegun (veh) 
        destroyElement(getElementData(veh, "weapon2")) 
        destroyElement(getElementData(veh, "weapon")) 
        setElementData(veh, "weapon", false) 
        setElementData(veh, "weapon2", false) 
        outputChatBox ( "Machinegun detached!" ) 
        end 
addEvent( "destroy", true ) 
addEventHandler( "destroy", root, destroyVehicleMachinegun ) 
  
  
  

Link to comment

I have a problem again with getElementModel.. I want to make that when i press h then i will be attached to the minigun but its not triggering to server side when i press h and the debugscript 3 says:

Bad Argumentum @ 'getElementData' [Expected element argumentum at argumentum 1, got tring 'h']

client

function getIntoMacineGun (veh) 
    local weapon = getElementData ( veh, "weapon" ) 
            if isElement ( weapon ) then 
            if not isPedInVehicle(localPlayer) then 
                triggerServerEvent ( "attachPlayerToMinigun", localPlayer, veh) 
                setElementData (localPlayer,"attachedToWeapon",true) 
            end 
        end 
    end 
bindKey ( "h", "down", getIntoMacineGun) 

server

addEvent("attachPlayerToMinigun", true) 
addEventHandler("attachPlayerToMinigun",getRootElement(),function(veh) 
    local mid = getElementModel ( veh ) 
    if weapOff[mid] then 
        local weap = getElementData ( veh, "weapon" ) 
        setPedAnimation ( source, "SILENCED", "SilenceCrouchfire", 1, false, false, false, true ) 
        attachElements ( source, weap, -0.2, -0.5, 1, 0, 0, 90) 
        setElementCollisionsEnabled ( source, false ) 
        setElementData ( source, "attachedToWeapon_v", veh ) 
        setElementData ( source, "currentCol", nil ) 
        setElementData ( source, "attachedToWeapon_w", weap ) 
        setElementData ( veh, "playerOnWeapon", source ) 
        toggleControl (source,"fire", false) 
    end 
end) 

Link to comment

Read:

https://wiki.multitheftauto.com/wiki/BindKey

"The values passed to this function are:

key: The key that was pressed

keyState: The state of the key that was pressed, down if it was pressed, up if it was released.

arguments The optional arguments you specified when calling bindKey (see below)."

You cant use

getIntoMacineGun ( veh ) 

when bindKey gives you key (here "h", not vehicle!), keystate and other arguments.

Then you can use

getIntoMacineGun ( key, keystate ) 

but there is no vehicle.

If you wanna get the vehicle there you can use getPedOccupiedVehicle.

Or you can use bindKey only when someone enters vehicle:

function getIntoMacineGun  ( _, _, veh )   -- I use _ instead of key or state to show, that I don't use these arguments 
    ... 
end 
  
addEventHandler ( "onClientPlayerVehicleEnter", localPlayer, function ( veh, seat ) 
    if seat == 0 then 
        bindKey ( "h", "down", getIntoMacineGun, veh )  -- here veh is this "other arguments" 
    end 
end ) 
  
addEventHandler ( "onClientPlayerVehicleExit", localPlayer, function ( veh, seat ) 
    if seat == 0 then 
         unbindKey ( "h", "down", getIntoMacineGun ) 
    end 
end ) 
  

Link to comment

same debug, not working:/

  
function getIntoMacineGun ( key, keystate ) 
    local weapon = getElementData ( veh, "weapon" ) 
            if isElement ( weapon ) then 
            if not isPedInVehicle(localPlayer) then 
                triggerServerEvent ( "attachPlayerToMinigun", localPlayer, veh) 
                setElementData (localPlayer,"attachedToWeapon",true) 
            end 
        end 
    end 
bindKey ( "h", "down", getIntoMacineGun) 
  
  
  

Link to comment

i didnt really understand it but ok i had read it again. So.

i dont need it like this. I only want to attach the minigun in the vehicle, and not to bind. I want to make that if i press h then it will check that there is an elemnt minigun. if yes then it will trigger the server event and if theres no elemnt on the vehicle then not.

function getIntoMacineGun ( _, _, veh ) -- I use _ instead of key or state to show, that I don't use these arguments

...

end

addEventHandler ( "onClientPlayerVehicleEnter", localPlayer, function ( veh, seat )

if seat == 0 then

bindKey ( "h", "down", getIntoMacineGun, veh ) -- here veh is this "other arguments"

end

end )

addEventHandler ( "onClientPlayerVehicleExit", localPlayer, function ( veh, seat )

if seat == 0 then

unbindKey ( "h", "down", getIntoMacineGun )

end

end )

and i dont know now that what argumentums i need to use.. Im confused

Link to comment

I explain the same again now ...

bindKey gives you clientsided on a function 2 arguments:

key and keystate.

key is "h" here

keystate is "down"

Now there are 2 ways how to get vehicle (cause key and keystate arent vehicle):

1. Check if player is in a vehicle and if he is the driver, then get the vehicle by getPedOccupiedVehicle

2. Use bindKey on vehicle enter, if seat == 0, put the vehicle as argument for bindkey and use it. Unbind it again on vehicle exit.

The 2. way is faster and more professional, the first way is not so good, cause why always check if you are a driver when you can directly just bind it, if you are the driver.

Thats why I showed you the second way.

But before I wrote the 1. way too, you just ignored it and only saw the code, don't do that.

Link to comment

cool now i understand it and its working! thanks:D But as always when i solve a problem then a new problem appears.. So i have a problem again with an argumentum.. So when i press h and the script triggers the server event then the server side script has a bad argumentum and its not working..

debugscript 3:

Bad Argumentum @ 'getElementModel' [Expected element at argumentum 1,got nil]

(line 11)

  
weapOff = {} 
weapOff[422] = {-0.1, -1.9, -0.2, 0, 30, 60}  
weapOff[470] = {-0.1, 0.1, 1.0, 0, 30, 95} 
  
  
  
  
addEvent("attachPlayerToMinigun", true) 
addEventHandler("attachPlayerToMinigun",getRootElement(),function(veh) 
    local mid = getElementModel ( veh ) 
    if weapOff[mid] then 
        local weap = getElementData ( veh, "weapon" ) 
        setPedAnimation ( source, "SILENCED", "SilenceCrouchfire", 1, false, false, false, true ) 
        attachElements ( source, weap, -0.2, -0.5, 1, 0, 0, 90) 
        setElementCollisionsEnabled ( source, false ) 
        setElementData ( source, "attachedToWeapon_v", veh ) 
        setElementData ( source, "currentCol", nil ) 
        setElementData ( source, "attachedToWeapon_w", weap ) 
        setElementData ( veh, "playerOnWeapon", source ) 
        toggleControl (source,"fire", false) 
    end 
end) 
  

Link to comment

Show me clientside

And better use client instead of source.

source is the source of the event, client is the one who triggers it.

That means it's possible to fake event like

triggerServerEvent ( "eventname", getPlayerFromName ( "Bonus" ) ) 

I don't really know how to fake events, but it's possible.

Some hackers can join game and fake events by injecting LUA code, dunno how.

Thats why you should always use client when you wanna get the one, who triggered the event.

Link to comment

Okay:D i cahnged source to client but its the same debugscript

this is the client side. When the player is inside the colshapa and press h then the event will be triggered

client.lua

  
function getIntoMacineGun () 
    local x,y,z getElementPosition(localPlayer) 
    if isElementWithinColShape(localPlayer, col) and not isPedInVehicle(localPlayer) then 
                triggerServerEvent ( "attachPlayerToMinigun", localPlayer, veh) 
                setElementData (localPlayer,"attachedToWeapon",true) 
                outputChatBox ( "test" ) 
            end 
        end 
bindKey ( "h", "down", getIntoMacineGun) 
  
  

Link to comment

Don't you want to learn the basics first?

Your scripting-knowledge is terrible and you don't learn anything from my texts.

It's more like you are getting worse.

I said like 10 times that you can't use veh without getting it. You are using a variable which doesnt even exists.

Now you put a colshape (why?) and ask if the localPlayer is NOT in a vehicle.

IF NOT IN A VEHICLE

Wth are you trying to do?

Link to comment

listen. My script: when a player enters a patriot vehicle and press "r" then a minigun will attached to the vehicle. And if the player exit the vehicle and goes to the minigun and press h then he will be attached to the minigun. And the colshape is becaouse the minigun (the player will only be attached when he is inside the col). and "IF NOT IN A VEHICLE" is for that player cant enter the minigun if he is in the vehicle.

Link to comment

I already did that.

  
function onHit() 
local x,y,z getElementPosition(localPlayer) 
if not isPedInVehicle(localPlayer) then 
guiSetVisible(text3, true) 
end 
end 
addEventHandler( "onClientColShapeHit", col, onHit ) 
  
function onLeave() 
guiSetVisible(text3, false) 
guiSetVisible(text4, false) 
end 
addEventHandler( "onClientColShapeLeave", col, onLeave ) 
  
  
  

I still have the same problem

cool now i understand it and its working! thanks:D But as always when i solve a problem then a new problem appears.. So i have a problem again with an argumentum.. So when i press h and the script triggers the server event then the server side script has a bad argumentum and its not working..

debugscript 3:

Bad Argumentum @ 'getElementModel' [Expected element at argumentum 1,got nil]

(line 11)

  
weapOff = {} 
weapOff[422] = {-0.1, -1.9, -0.2, 0, 30, 60}  
weapOff[470] = {-0.1, 0.1, 1.0, 0, 30, 95} 
  
  
  
  
addEvent("attachPlayerToMinigun", true) 
addEventHandler("attachPlayerToMinigun",getRootElement(),function(veh) 
    local mid = getElementModel ( veh ) 
    if weapOff[mid] then 
        local weap = getElementData ( veh, "weapon" ) 
        setPedAnimation ( source, "SILENCED", "SilenceCrouchfire", 1, false, false, false, true ) 
        attachElements ( source, weap, -0.2, -0.5, 1, 0, 0, 90) 
        setElementCollisionsEnabled ( source, false ) 
        setElementData ( source, "attachedToWeapon_v", veh ) 
        setElementData ( source, "currentCol", nil ) 
        setElementData ( source, "attachedToWeapon_w", weap ) 
        setElementData ( veh, "playerOnWeapon", source ) 
        toggleControl (source,"fire", false) 
    end 
end) 
  

Link to comment

I didn't talk about guiSetVisible, I said:

"Then save the vehicle to a variable and use this variable in triggerServerEvent in getIntoMacineGun."

You can get the vehicle by colshape if you save it in an array like:

local colVehicle = {} 
local vehnearplayer = nil 
  
... 
colVehicle[col] = vehicle 
... 
  
addEventHandler ( "onClientColShapeHit", col, function ( hitElement, dim ) 
    if dim and hitElement == getLocalPlayer then 
        vehnearplayer = colVehicle[source] 
    end 
end ) 

Link to comment

One more question. I attached the player to the weapon object. Its working but i cant rotate the player when i attach it to the object..i mean not rotating after attaching, i mean rotating while attaching..So i wanna attach it to a given rx,rz,ry.. Pls help!

  
function attach() 
        setPedAnimation ( source, "SILENCED", "SilenceCrouchfire", 1, false, false, false, true ) 
        attachElements ( source, weapon, 0, 0.06, 0, 10, 20, 30 ) --<---- look here i added the x,y,z,rx,rz,ry but only the x,y,z is working. the rotation is not working.. When the player is attaching to the object then the player does not rotate 
        setElementCollisionsEnabled ( source, false ) 
        setElementData ( source, "attachedToWeapon_v", veh ) 
        setElementData ( source, "currentCol", nil ) 
        setElementData ( source, "attachedToWeapon_w", weap ) 
        setElementData ( veh, "playerOnWeapon", source ) 
        toggleControl (source,"fire", false) 
        end 
addEvent("attachPlayerToMinigun", true) 
addEventHandler("attachPlayerToMinigun",getRootElement(), attach) 
  

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