SegSnigel Posted September 24, 2020 Share Posted September 24, 2020 Hai So, this will make it able for a player to create a vehicle by command for example /vh 445. It works fine when i creating a vehicle, and if i try to create a new one when im inside of the vehicle it will output the text ''The vehicle is already in use..'', and if i exit the first vehicle and creating a new vehicle the first one will get destroyed and i will get my new vehicle i created. The problem is, when im inside of the vehicle and anyone else is using the command he will be able to use the command.. I just want the command to work for one person, i only wanna allow someone to create a vehicle when noone is in it. Thanks in advance! Here is my code: function createVehicleForPlayer(thePlayer, command, model) local x,y,z = getElementPosition(thePlayer) local rx,ry,rz = getElementRotation(thePlayer) local theVehicle = getPedOccupiedVehicle(thePlayer) if not theVehicle then if isElement(createdVehicle) then destroyElement(createdVehicle) end createdVehicle = createVehicle(model, x,y,z, rx,ry,rz) warpPedIntoVehicle (thePlayer, createdVehicle) else outputChatBox("The vehicle is already in use..", thePlayer, 0, 255, 0) end end addCommandHandler("vh", createVehicleForPlayer) Link to comment
KronoS Lettify Posted September 24, 2020 Share Posted September 24, 2020 For the command to work correctly according to your problem, it is good to use tables to manage the players and their created vehicles. Do you know how tables work in Lua? Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 (edited) Maybe this can help you function createVehicleForPlayer(thePlayer, command, model) if not isPedInVehicle (thePlayer) then if creados[thePlayer] == nil then local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) else destroyElement(creados[thePlayer]) local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) end end end addCommandHandler("vh", createVehicleForPlayer) function onPlayerQuitPlayer () if isElement(creados[source]) then destroyElement(creados[source]) creados[source] = nil end end addEventHandler ( "onPlayerQuit", getRootElement(), onPlayerQuitPlayer ) Edited September 24, 2020 by RekZ Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 13 hours ago, KronoS Lettify said: For the command to work correctly according to your problem, it is good to use tables to manage the players and their created vehicles. Do you know how tables work in Lua? Im pretty new to Lua scripting but yes i know how tables(array) works in Lua. Maybe you could give me a little hint how i could solve my problem? 13 hours ago, RekZ said: Maybe this can help you Still the same problem, when im inside of the vehicle others can still create a new vehicle, they should only be allowed to use the command when noone is inside. But thanks anyways, i appreciate the help Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 Soo you want to create the car and if there is someone inside the car stop the command ? Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 4 minutes ago, RekZ said: Soo you want to create the car and if there is someone inside the car stop the command ? Yes exactly Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 I'm not sure if this can help you i dont test it function getVehicleInUse(veh) local user = 0 for seat, player in pairs(getVehicleOccupants(veh)) do user = user + 1 end return (user or 0) end function createVehicleForPlayer(thePlayer, command, model) if not isPedInVehicle (thePlayer) then if creados[thePlayer] == nil then local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) else if getVehicleInUse(creados[thePlayer]) > 0 then destroyElement(creados[thePlayer]) local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) end end end end addCommandHandler("vh", createVehicleForPlayer) function onPlayerQuitPlayer () if isElement(creados[source]) then destroyElement(creados[source]) creados[source] = nil end end addEventHandler ( "onPlayerQuit", getRootElement(), onPlayerQuitPlayer ) Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 I tested it but still the same Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 Ups i see my error now creados = {} function getVehicleInUse(veh) local user = 0 for seat, player in pairs(getVehicleOccupants(veh)) do user = user + 1 end return (user or 0) end function createVehicleForPlayer(thePlayer, command, model) if tonumber(model) then if not isPedInVehicle (thePlayer) then if creados[thePlayer] == nil then local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) else if getVehicleInUse(creados[thePlayer]) == 0 then destroyElement(creados[thePlayer]) local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) end end end end end addCommandHandler("vh", createVehicleForPlayer) function onPlayerQuitPlayer () if isElement(creados[source]) then destroyElement(creados[source]) creados[source] = nil end end addEventHandler ( "onPlayerQuit", getRootElement(), onPlayerQuitPlayer ) Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 I found out the error with ''debugscript 3'' at the first code you gave me so added this line ''local creados = {}'' but the code is still not working Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 (edited) 5 minutes ago, SegSnigel said: I found out the error with ''debugscript 3'' at the first code you gave me so added this line ''local creados = {}'' but the code is still not working Because is a table you cant use local in the table, remove it xd in my last code is fine, i test the code now in my server and work good local table = {} -- BAD table = {} -- good :v Edited September 24, 2020 by RekZ Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 I tested it too with both local and without local, when im in the vehicle my friend can still create a new vehicle and the vehicle i created got destroyed local have only with the scope to do if im right, the table is only visible inside this script, and access to local variables is faster what i heard ( i can be wrong ) But yes the same problem occurs Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 16 minutes ago, SegSnigel said: I tested it too with both local and without local, when im in the vehicle my friend can still create a new vehicle and the vehicle i created got destroyed local have only with the scope to do if im right, the table is only visible inside this script, and access to local variables is faster what i heard ( i can be wrong ) But yes the same problem occurs I test it whit my users and work fine like you want, send the code where you are working Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 Weird because im using the code you gav me last time. This one: 1 hour ago, RekZ said: Ups i see my error now creados = {} function getVehicleInUse(veh) local user = 0 for seat, player in pairs(getVehicleOccupants(veh)) do user = user + 1 end return (user or 0) end function createVehicleForPlayer(thePlayer, command, model) if tonumber(model) then if not isPedInVehicle (thePlayer) then if creados[thePlayer] == nil then local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) else if getVehicleInUse(creados[thePlayer]) == 0 then destroyElement(creados[thePlayer]) local x, y, z = getElementPosition(thePlayer) creados[thePlayer] = nil creados[thePlayer] = createVehicle( model, x+5, y, z) warpPedIntoVehicle(thePlayer, creados[thePlayer]) end end end end end addCommandHandler("vh", createVehicleForPlayer) function onPlayerQuitPlayer () if isElement(creados[source]) then destroyElement(creados[source]) creados[source] = nil end end addEventHandler ( "onPlayerQuit", getRootElement(), onPlayerQuitPlayer ) Same problem, my friend created a vehicle then i used the command, his vehicle got destroyed and i created a new. I should not be allowed to use the command if hes using the vehicle Link to comment
Bilal135 Posted September 24, 2020 Share Posted September 24, 2020 (edited) This should do the job. (Not tested) local vehicle = {}; function isVehicleOccupied(vehicleID) if vehicle[vehicleID] = true then return true end return false end function createVehicleForPlayer(player, cmd, model) if not tonumber(model) then return false end if isVehicleOccupied(model) then return false end local x, y, z = getElementPosition(player) local veh = createVehicle(model, x + 5, y, z) if not veh then return false end vehicle[model] = true; end addCommandHandler("vh", createVehicleForPlayer) function destroyVehicleOnQuit(player) if not isPedInVehicle(player) then return false end local veh = getPedOccupiedVehicle(player) if not veh then return false end local model = getElementModel(veh) if not isVehicleOccupied(model) then return false end if not isElement(veh) then return false end destroyElement(veh) vehicle[model] = false; end addEventHandler("onPlayerQuit", root, destroyVehicleOnQuit) Edited September 24, 2020 by Bilal135 Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 1 hour ago, SegSnigel said: Weird because im using the code you gav me last time. This one: Same problem, my friend created a vehicle then i used the command, his vehicle got destroyed and i created a new. I should not be allowed to use the command if hes using the vehicle I think i dont known what you want jajaja, This is what you want ? (see the video xd) Video Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 1 hour ago, Bilal135 said: This should do the job. (Not tested) local vehicle = {}; function isVehicleOccupied(vehicleID) if vehicle[vehicleID] = true then return true end return false end function createVehicleForPlayer(player, cmd, model) if not tonumber(model) then return false end if isVehicleOccupied(model) then return false end local x, y, z = getElementPosition(player) local veh = createVehicle(model, x + 5, y, z) if not veh then return false end vehicle[model] = true; end addCommandHandler("vh", createVehicleForPlayer) function destroyVehicleOnQuit(player) if not isPedInVehicle(player) then return false end local veh = getPedOccupiedVehicle(player) if not veh then return false end local model = getElementModel(veh) if not isVehicleOccupied(model) then return false end if not isElement(veh) then return false end destroyElement(veh) vehicle[model] = false; end addEventHandler("onPlayerQuit", root, destroyVehicleOnQuit) Thank you so much for the help mate but sadly not how i want it to work. This will create a vehicle and if im not in the vehicle then me or anyone else is not able to create a new one which is in the same model, only if the model id is not the same but then there will be 2 existing vehicles thats not what i want, i only want 1 vehicle to exist. This is how i want it: Lets say i creating a vehicle, now i need to exit the vehicle to be able to create a new one, or anyone else in the server. When i creating another vehicle the first one will get destroyed, so there will only be 1 existing vehicle.. My english isnt the best but hope you understood. 49 minutes ago, RekZ said: I think i dont known what you want jajaja, This is what you want ? (see the video xd) Video Sorry but im not gonna click any links check what i wrote above thats how i want it Link to comment
RekZ Posted September 24, 2020 Share Posted September 24, 2020 (edited) 3 hours ago, SegSnigel said: Thank you so much for the help mate but sadly not how i want it to work. This will create a vehicle and if im not in the vehicle then me or anyone else is not able to create a new one which is in the same model, only if the model id is not the same but then there will be 2 existing vehicles thats not what i want, i only want 1 vehicle to exist. This is how i want it: Lets say i creating a vehicle, now i need to exit the vehicle to be able to create a new one, or anyone else in the server. When i creating another vehicle the first one will get destroyed, so there will only be 1 existing vehicle.. My english isnt the best but hope you understood. Sorry but im not gonna click any links check what i wrote above thats how i want it i cant help you if you dont help me to do it.. i do what you said maybe you dont restart the resource or dont put the script in the correct place i cant install the resource for you, sorry. --- A part of the post was hidden, please don't go to the extremes (Tut) Edited September 24, 2020 by Tut Offensive part removed Link to comment
SegSnigel Posted September 24, 2020 Author Share Posted September 24, 2020 No but virus, i dont click on links that i have no idea what it is. Everything is in the correct place so nothing wrong there. I wrote above exactly how i want the script to work. Your code works exactly like mine i posted in my first post, the only problem is that other users can use the command to create a new vehicle when im inside of the vehicle. 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