Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. function createMyBoat(cmd)
  2. local x, y, z = getElementPosition(localPlayer)
  3. local veh = createVehicle(430, x, y+4, z+2)
  4. local ped = createPed(0, x+4, y, z+2)
  5. warpPedIntoVehicle(ped, veh)
  6. setPedControlState(ped, "accelerate", true)
  7. setElementSpeed(veh, 1, 50)
  8. end
  9.  
  10. addCommandHandler("wtf", createMyBoat)
  11.  
  12. function getElementSpeed(theElement, unit)
  13. -- Check arguments for errors
  14. assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
  15. local elementType = getElementType(theElement)
  16. assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
  17. assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
  18. -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
  19. unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
  20. -- Setup our multiplier to convert the velocity to the specified unit
  21. local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
  22. -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
  23. return (Vector3(getElementVelocity(theElement)) * mult).length
  24. end
  25.  
  26. function setElementSpeed(element, unit, speed)
  27. local unit = unit or 0
  28. local speed = tonumber(speed) or 0
  29. local acSpeed = getElementSpeed(element, unit)
  30. if (acSpeed) then -- if true - element is valid, no need to check again
  31. local diff = speed/acSpeed
  32. if diff ~= diff then return false end -- if the number is a 'NaN' return false.
  33. local x, y, z = getElementVelocity(element)
  34. return setElementVelocity(element, x*diff, y*diff, z*diff)
  35. end
  36.  
  37. return false
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement