Jump to content

Key presses to trigger something


[FOTL]Aphex

Recommended Posts

Posted

Hi, how can I make a script that will allow players to change vehicles by pressing a key?

I have a script that will change you to a random vehicle when you hit a checkpoint, but I dont know how to convert it to work with key presses instead!

function randomCar(checkpoint, time) 
local veh = getPedOccupiedVehicle(source) 
    if 1 <= checkpoint and checkpoint <= 8 then 
        local x,y,z = getElementPosition(veh) 
        setElementModel (veh, carids[math.random(#carids)]) 
        setElementPosition (veh, x, y, z+2) 
    end 
end 
addEvent('onPlayerReachCheckpoint') 
addEventHandler('onPlayerReachCheckpoint', getRootElement(), randomCar) 

I've been looking at this example from the wiki for keypresses

function playerPressedKey(button, press) 
    if (press) then -- Only output when they press it down 
        outputChatBox("You pressed the "..button.." key!") 
    end 
end 
addEventHandler("onClientKey", root, playerPressedKey) 

Posted
addEventHandler("onPlayerJoin",root, 
function () 
    bindKey(source,"F1","down",randomCar) 
end) 
  
addEventHandler("onResourceStart",resourceRoot, 
function () 
    for index, player in ipairs(getElementsByType("player")) do 
        bindKey(player,"F1","down",randomCar) 
    end 
end) 
  
function randomCar(player) 
    local veh = getPedOccupiedVehicle(player) 
    if (veh) then 
        local x,y,z = getElementPosition(veh) 
        setElementModel (veh, carids[math.random(#carids)]) 
        setElementPosition (veh, x, y, z+2) 
    end 
end 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
addEventHandler("onPlayerJoin",root, 
function () 
    bindKey(source,"F1","down",randomCar) 
end) 
  
addEventHandler("onResourceStart",resourceRoot, 
function () 
    for index, player in ipairs(getElementsByType("player")) do 
        bindKey(player,"F1","down",randomCar) 
    end 
end) 
  
function randomCar(player) 
    local veh = getPedOccupiedVehicle(player) 
    if (veh) then 
        local x,y,z = getElementPosition(veh) 
        setElementModel (veh, carids[math.random(#carids)]) 
        setElementPosition (veh, x, y, z+2) 
    end 
end 

It works!

Thanks immensely! :)

Posted

You're welcome.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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