Jump to content

my First Script help....


Mr.Solo

Recommended Posts

so hai..well i made my first script that detatch Truck Trailer if you press "o" . but after i asked and tried all , i gave up..because bindkey dosn't seems to work with function.. help me!! this is script i used for server side.

  
function unhookTrailer(source, commandName) 
   if (source and isPedInVehicle(source)) then 
      local theVehicle = getPedOccupiedVehicle(source) 
      local success = detachTrailerFromVehicle(theVehicle) 
      if (success) then 
         outputChatBox("Trailer detached!", source) 
      else 
         outputConsole("Trailer not attatched.", source) 
      end 
   end 
end 
addCommandHandler("unhook", unhookTrailer) 
bindkey( source, "o", "up", unhookTrailer) 
  

Link to comment

bindkey( source, "o", "up", unhookTrailer)

What is the source there?

If its outside of any function, it won't work like that.

You could loop through all players and just bind the key for each of them.

Also you should probably rather name the first parameter of the unhookTrailer function "player" instead of "source",

since "source" is predefined when working with events.

Link to comment
addEventHandler("onPlayerLogin", root, 
   function() 
      bindKey(source, "o", "up", unhookTrailer) 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot, 
   function() 
       for _, v ipairs(getElementsByType("player")) do 
            bindKey(v, "o", "up", unhookTrailer) 
        end 
    end 
) 
  
function unhookTrailer(thePlayer, commandName) 
    if (thePlayer and isPedInVehicle(thePlayer)) then 
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
        local success = detachTrailerFromVehicle(theVehicle) 
        if (success) then 
           outputChatBox("Traller detached!", thePlayer) 
        else 
           outputChatBox("Traller not attatched.". thePlayer) 
        end 
    end 
end 
addCommandHandler("unhook", unhookTrailer) 

Link to comment
addEventHandler("onPlayerLogin", root, 
   function() 
      bindKey(source, "o", "up", unhookTrailer) 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot, 
   function() 
       for _, v ipairs(getElementsByType("player")) do 
            bindKey(v, "o", "up", unhookTrailer) 
        end 
    end 
) 
  
function unhookTrailer(thePlayer, commandName) 
    if (thePlayer and isPedInVehicle(thePlayer)) then 
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
        local success = detachTrailerFromVehicle(theVehicle) 
        if (success) then 
           outputChatBox("Traller detached!", thePlayer) 
        else 
           outputChatBox("Traller not attatched.". thePlayer) 
        end 
    end 
end 
addCommandHandler("unhook", unhookTrailer) 

well tried using your code didn't work , tried edit it , same thing , i'm not even sure if i can do this script .. i guess i will try another idea :( , anyways thanks for trying..

Link to comment
addEventHandler("onPlayerLogin", root, 
   function() 
      bindKey(source, "o", "up", unhookTrailer) 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot, 
   function() 
       for _, v ipairs(getElementsByType("player")) do 
            bindKey(v, "o", "up", unhookTrailer) 
        end 
    end 
) 
  
function unhookTrailer(thePlayer, commandName) 
    if (isPedInVehicle(thePlayer)) then 
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
        local success = detachTrailerFromVehicle(theVehicle) 
        if (success) then 
           outputChatBox("Traller detached!", thePlayer) 
        else 
           outputChatBox("Traller not attatched.". thePlayer) 
        end 
    end 
end 
addCommandHandler("unhook", unhookTrailer) 

try that

Link to comment
addEventHandler("onPlayerLogin", root, 
   function() 
      bindKey(source, "o", "up", unhookTrailer) 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot, 
   function() 
       for _, v ipairs(getElementsByType("player")) do 
            bindKey(v, "o", "up", unhookTrailer) 
        end 
    end 
) 
  
function unhookTrailer(thePlayer, commandName) 
    if (isPedInVehicle(thePlayer)) then 
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
        local success = detachTrailerFromVehicle(theVehicle) 
        if (success) then 
           outputChatBox("Traller detached!", thePlayer) 
        else 
           outputChatBox("Traller not attatched.". thePlayer) 
        end 
    end 
end 
addCommandHandler("unhook", unhookTrailer) 

try that

both command and bindkey dosn't work , it's hopeless nvm bro, i will try make other things ;)

Link to comment

You guys are all forgetting a single thing.. if you detach a trailer, it'll get attached right away again due to the position of the trailer and the truck.

@OP: try to use setElementPosition on the trailer when it gets detached to move it away from the truck to avoid it being attached again. Also, try to avoid isPedInVehicle since it fails quite often, a more reliable approach would be getPedOccupiedVehicle. also make sure that this script is on the server's side, not a client sided file.

Link to comment
addEventHandler("onPlayerLogin", root,  
   function() 
       bindKey(source, "o", "up", unhookTrailer) 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot,  
   function() 
       for _, v in ipairs(getElementsByType("player")) do 
           bindKey(v, "o", "up", unhookTrailer) 
        end 
    end 
) 
  
function unhookTrailer(thePlayer, commandName) 
   if isPedInVehicle(thePlayer) then 
      local theVehicle = getPedOccupiedVehicle(thePlayer) 
      local success = detachTrailerFromVehicle(theVehicle) 
      local x, y, z = getElementPosition(theVehicle) 
      if (success) then 
          if getElementType("trailer") then 
             setElementPosition(trailer, x+2, y, z) 
          end 
             outputChatBox("Trailer detached!", thePlayer) 
      else 
             outputChatBox("Trailer not attatched.", thePlayer) 
      end 
    end 
end 
addCommandHandler("unhook", unhookTrailer) 

Link to comment
addEventHandler("onPlayerLogin", root,  
   function() 
       bindKey(source, "o", "up", unhookTrailer) 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot,  
   function() 
       for _, v in ipairs(getElementsByType("player")) do 
           bindKey(v, "o", "up", unhookTrailer) 
        end 
    end 
) 
  
function unhookTrailer(thePlayer, commandName) 
   if isPedInVehicle(thePlayer) then 
      local theVehicle = getPedOccupiedVehicle(thePlayer) 
      local success = detachTrailerFromVehicle(theVehicle) 
      local x, y, z = getElementPosition(theVehicle) 
      if (success) then 
          if getElementType("trailer") then 
             setElementPosition(trailer, x+2, y, z) 
          end 
             outputChatBox("Trailer detached!", thePlayer) 
      else 
             outputChatBox("Trailer not attatched.", thePlayer) 
      end 
    end 
end 
addCommandHandler("unhook", unhookTrailer) 

ur legend thanks man , btw i do setelementPos x , y - 2 ,z or what??

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