[FOTL]Aphex Posted January 26, 2012 Share Posted January 26, 2012 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) Link to comment
Castillo Posted January 26, 2012 Share Posted January 26, 2012 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 Link to comment
[FOTL]Aphex Posted January 26, 2012 Author Share Posted January 26, 2012 Hey, thanks a lot Can't test it til tomorrow, but yea... thanks muchly! Link to comment
[FOTL]Aphex Posted January 26, 2012 Author Share Posted January 26, 2012 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! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now