Jump to content

My script does not work [NEED HELP]


Timiimit

Recommended Posts

Hi !!!

I made 1 script but it looks like its not perfect. I want to make that when you would press left control [LCtrl] car of the driver would turn for 180 degrees with the same velocity(speed) and outputs in chatbox that you turned!

This is what i have so far:

function turn (thePlayer) 
    speedx, speedy, speedz = getElementVelocity ( source ) 
    pVehicle = getPedOccupiedVehicle(source) 
        if isPedInVehicle ( source ) then 
            actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5)  
  
                kmh = actualspeed * 180 
                mph = actualspeed * 111.847 
              setElementRotation(pVehicle,rX,rY,rZ+180) 
  
            outputChatBox ( "You turned your Vehicle With " ..kph.. " km/h Or " ..mph.. " mph", source ) 
        end 
end 
  
function bindKey ( thePlayer ) 
    Bind( "LControl", turn ) 
    outputChatBox( "#00FF00You can now press #0000FFLCtrl in vehicle to turn for 180 degrees!", source ) 
end 
  
addEventHandler( "onResourceStart", bindKey ) 

Thanks!

Link to comment

This is total mess...

function turn (thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    if (not vehicle) then return end 
    local speedx, speedy, speedz = getElementVelocity ( vehicle ) 
    local rX, rY, rZ = getElementRotation( vehicle ) 
    local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5) 
    local kmh = actualspeed * 180 
    local mph = actualspeed * 111.847 
    setElementRotation(vehicle,rX,rY,rZ+180) 
    outputChatBox ( "You turned your Vehicle With ".. tostring(kph) .." km/h Or ".. tostring(mph) .." mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
         bindKey(player, "lctrl", "down", turn ) 
         outputChatBox( "#00FF00You can now press #0000FFLCtrl in vehicle to turn for 180 degrees!", player ) 
     end 
end 
addEventHandler( "onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin",root, 
function () 
bindKey(source, "lctrl", "down", turn ) 
end) 

Edited by Guest
Link to comment

You didn't even try to find it out...

function turn(thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    local rX, rY, rZ = getVehicleRotation(vehicle) 
    if (not vehicle) then return end 
    local speedx, speedy, speedz = getElementVelocity(vehicle) 
    local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5) 
    local kmh = actualspeed * 180 
    local mph = actualspeed * 111.847 
    setElementRotation(vehicle, rX, rY, rZ + 180) 
    outputChatBox("You turned your vehicle with " .. tostring(kph) .. " km/h | " .. tostring(mph) .. " mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player, "lctrl", "down", turn) 
        outputChatBox("#00FF00You can now press #0000FF'left ctrl' #00FF00in vehicle to turn for 180 degrees!", player, 255, 255, 255, true) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin", root, 
function() 
    bindKey(source, "lctrl", "down", turn) 
end) 

Link to comment

Yes, I forgot about the rotation.

P.S: myonlake, you put getElementRotation before check if the vehicle exists.

function turn(thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    if (not vehicle) then return end 
    local speedx, speedy, speedz = getElementVelocity(vehicle) 
    local rX, rY, rZ = getVehicleRotation(vehicle) 
    local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5) 
    local kmh = actualspeed * 180 
    local mph = actualspeed * 111.847 
    setElementRotation(vehicle, rX, rY, rZ + 180) 
    outputChatBox("You turned your vehicle with " .. tostring(kph) .. " km/h | " .. tostring(mph) .. " mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player, "lctrl", "down", turn) 
        outputChatBox("#00FF00You can now press #0000FF'left ctrl' #00FF00in vehicle to turn for 180 degrees!", player, 255, 255, 255, true) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin", root, 
function() 
    bindKey(source, "lctrl", "down", turn) 
end) 
 
Link to comment
function turn(thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    if (not vehicle) then return end 
    local speedx, speedy, speedz = getElementVelocity(vehicle) 
    local rX, rY, rZ = getVehicleRotation(vehicle) 
    local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5) 
    local kmh = actualspeed * 180 
    local mph = actualspeed * 111.847 
    setElementRotation(vehicle, rX, rY, rZ + 180) 
    setElementVelocity ( getPlayerFromName ( source ), speedx, speedy, speedz - speedz^2 ) 
    outputChatBox("You turned your vehicle with " .. tostring(kph) .. " km/h | " .. tostring(mph) .. " mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player, "lctrl", "down", turn) 
        outputChatBox("#00FF00You can now press #0000FF'left ctrl' #00FF00in vehicle to turn for 180 degrees!", player, 255, 255, 255, true) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin", root, 
function() 
    bindKey(source, "lctrl", "down", turn) 
end) 

Link to comment
function turn(thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    if (not vehicle) then return end 
    local speedx, speedy, speedz = getElementVelocity(vehicle) 
    local rX, rY, rZ = getVehicleRotation(vehicle) 
    local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5) 
    local kmh = actualspeed * 180 
    local mph = actualspeed * 111.847 
    setElementRotation(vehicle, rX, rY, rZ + 180) 
    setElementVelocity ( vehicle, speedx, speedy, speedz - speedz^2 ) 
    outputChatBox("You turned your vehicle with " .. tostring(kph) .. " km/h | " .. tostring(mph) .. " mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player, "lctrl", "down", turn) 
        outputChatBox("#00FF00You can now press #0000FF'left ctrl' #00FF00in vehicle to turn for 180 degrees!", player, 255, 255, 255, true) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin", root, 
function() 
    bindKey(source, "lctrl", "down", turn) 
end) 

doesnt work

Edited by Guest
Link to comment

Hi ,

i used setElementSpeed + getElementSpeed from Useful Functions

https://wiki.multitheftauto.com/wiki/SetElementSpeed

https://wiki.multitheftauto.com/wiki/GetElementSpeed

  
-- From Useful Functions ! 
function setElementSpeed(element, unit, speed) -- only work if element is moving! 
    if (unit == nil) then unit = 0 end 
    if (speed == nil) then speed = 0 end 
    speed = tonumber(speed) 
    local acSpeed = getElementSpeed(element, unit) 
    if (acSpeed~=false) then -- if true - element is valid, no need to check again 
        local diff = speed/acSpeed 
        local x,y,z = getElementVelocity(element) 
        setElementVelocity(element,x*diff,y*diff,z*diff) 
        return true 
    end 
  
    return false 
end 
----- 
function getElementSpeed(element,unit) 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
------ 
function turn(thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    if (not vehicle) then return end 
    local rX, rY, rZ = getVehicleRotation(vehicle) 
    local Speeds = getElementSpeed(vehicle) 
    setElementRotation(vehicle, rX, rY, rZ + 180) 
    local Boost = 80 -- You Can set this to 0  if You Dont Need it ! 
    setTimer(setElementSpeed,1100,1,vehicle,1,Speeds+Boost)   -- i used timer to set the car speed after rotation 1 
    outputChatBox("You turned your vehicle with " .. tostring(Speeds) .. " mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player, "lctrl", "down", turn) 
        outputChatBox("#00FF00You can now press #0000FF'left ctrl' #00FF00in vehicle to turn for 180 degrees!", player, 255, 255, 255, true) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin", root, 
function() 
    bindKey(source, "lctrl", "down", turn) 
end) 
  

This Should Work !!

Link to comment
-- From Useful Functions ! 
function setElementSpeed(element, unit, speed) -- only work if element is moving! 
    if (unit == nil) then unit = 0 end 
    if (speed == nil) then speed = 0 end 
    speed = tonumber(speed) 
    local acSpeed = getElementSpeed(element, unit) 
    if (acSpeed~=false) then -- if true - element is valid, no need to check again 
        local diff = speed/acSpeed 
        local x,y,z = getElementVelocity(element) 
        setElementVelocity(element,x*diff,y*diff,z*diff) 
        return true 
    end 
  
    return false 
end 
----- 
function getElementSpeed(element,unit) 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
------ 
function turn(thePlayer) 
    local vehicle = getPedOccupiedVehicle(thePlayer) 
    if (not vehicle) then return end 
    local rX, rY, rZ = getVehicleRotation(vehicle) 
    local Speeds = getElementSpeed(vehicle, "mph") 
    setElementRotation(vehicle, rX, rY, rZ + 180) 
    local Boost = 0 -- You Can set this to 0  if You Dont Need it ! 
    setTimer(setElementSpeed,50,1,vehicle,1,-Speeds+Boost)   -- i used timer to set the car speed after rotation 1 
    outputChatBox("You turned your vehicle with " .. tostring(Speeds) .. " mph", thePlayer) 
end 
  
function bindTheKey() 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player, "lctrl", "down", turn) 
        outputChatBox("#00FF00You can now press #0000FF'left ctrl' #00FF00in vehicle to turn for 180 degrees!", player, 255, 255, 255, true) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, bindTheKey) 
  
addEventHandler("onPlayerJoin", root, 
function() 
    bindKey(source, "lctrl", "down", turn) 
end) 

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