Jump to content

I have a vague idea of what I'm doing


David1544

Recommended Posts

Hello,

I'm here today to ask if anybody can say whether I am on the right track or not. The only experience I have in LUA is 2 years of Roblox LUA (yes you can laugh, but for being 12 years old at the time I thought I did a pretty good job). So, this is my first script in a while, what I'm trying to do is that when a player spawns or enters a new vehicle, it gets the old vehicle's ID (assuming the player had a previous vehicle) and deletes the old vehicle, then it will spawn (or the player enters) a vehicle and assign a new ID to it, with the ID being "(playername)1". I'm not asking for somebody to do all the work and fix this for me, I'm just asking for feedback.

--Assigns ID on vehicle spawn and deletes old vehicle 
function createVehicleForPlayer(thePlayer, command, vehicleModel) 
    local x, y, z = getElementPosition(thePlayer) --gets position of players x,y,z 
    x = x + 3 
    local createdVehicle = createVehicle(tonumber(vehicleModel),x, y, z) 
        i=1 
        getElementByID ( player[i] ) 
        destroyElement 
        getPedOccupiedVehicle ( player ) 
        setElementID ( player[i] )else 
            if (createdVehicle == false) then 
            outputChatBox("Failed to create vehicle.", thePlayer) 
end 
addCommandHandler("createvehicle", createVehicleForPlayer) 
--Assigns ID on vehicle enter and deletes old vehicle 
function PlayerEntersVehicle 
    getElementByID ( player[i] ) 
    destroyElement 
    setElementID ( player[i] ) 
addEventHandler("onVehicleEnter", PlayerEntersVehicle) 

Here's some output from the MTA Script Editor

'=' expected near 'getPedOccupiedVehicle' - Line 9 
'<eof>' expected near 'else' - Line 10 
'(' expected near 'getElementByID' - Line 17 
'=' expected near 'setElementID' - Line 19 

Link to comment

I read over the wiki and the forums a bit more and was able to modify the script so it looks like this. It wont load because of the error "'=' expected near 'local' Line 9" though. Also, no offense taken, I expect my scripts to be crap until I get back up to speed on coding.

  
--Assigns ID on vehicle spawn and deletes old vehicle 
function createVehicleForPlayer(thePlayer, command, vehicleModel) 
    local x, y, z = getElementPosition(thePlayer) -- Gets player Position 
    x = x + 3 -- Adds 3 to x-axis of player location 
    local createdVehicle = createVehicle(tonumber(vehicleModel),x, y, z) -- Creates vehicle at player location +3 to x-axis 
        i=1 -- A variable, I think I need it, not entirely sure 
        getElementByID ( player[i] ) -- Gets ID of old car 
        destroyElement -- Destroys old car 
        local checkedPlayer = getPlayerFromName ( PlayerVehicle ) -- Checks if player is in vehicle 
        if ( PlayerVehicle ) then 
            if isPedInVehicle ( checkedplayer ) then -- Player is in vehicle 
                 setElementID ( player[i] ) -- Assigns ID to newly created vehicle 
            else 
                 outputChatBox("Failed to create vehicle.", thePlayer) -- Player is not in vehicle 
end 
addCommandHandler("createvehicle", createVehicleForPlayer) 
-- Extra code removed until I can resolve the current issue. 
  

Link to comment

EDIT: I fixed it by mashing my hands on my keyboard and this came out.

EDIT 2: It loads but does not function :/

function spawnPlayerVehicle (thePlayer, command, vehicleModel) 
    local check isPedInVehicle(thePlayer) 
        if isPedInVehicle == true then 
            local x, y, z = getElementPosition(thePlayer) 
            local theVehicle = getPedOccupiedVehicle(thePlayer) 
            if theVehicle then 
                destroyElement(theVehicle) 
                createVehicle(tonumber(vehicleModel),x, y, z) else 
                x = x + 3 
                createVehicle(tonumber(vehicleModel),x, y, z) 
            setElementID ("theVehicle",player[i]) 
        end 
    end 
end 
addCommandHandler("spawnVehicle", spawnPlayerVehicle) 
function AssignVehicleID() 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
    local check isPedInVehicle(thePlayer) 
    if isPedInVehicle == true then 
        setElementID ("theVehicle", player[i]) else 
        end 
end 
addEventHandler("onVehicleEnter", root, AssignVehicleID)) 

Edited by Guest
Link to comment

Okay, when a player spawns a vehicle, it will delete the current one that it is in. Then, it assigns an unique ID to that vehicle when it is either spawned or when a player enters it. I tried to make some more changes after I posted that, which is actually broken. No errors get thrown out but the command /spawnvehicle (ID) doesn't work.

Link to comment

You should really learn a few more.

viewtopic.php?f=148&t=40809

Here is the code, which should work fine for your needs. I haven't really used setElementID before and I find tables a little bit better way of managing vehicles.

local vehicles = {} 
  
addCommandHandler("spawnVehicle", 
    function(thePlayer, command, vehicleModel) 
        local vehicleModel = tonumber(vehicleModel) -- Get the number value of the model 
         
        if (not vehicleModel) then -- If there wasn't any model passed in, display an error 
            outputChatBox("SYNTAX: /" .. command .. " [vehicle model]", thePlayer, 220, 180, 20, false) 
            return 
        end 
         
        if (not getVehicleNameFromModel(vehicleModel)) then -- If the vehicle model is invalid, display an error 
            outputChatBox("Invalid vehicle model.", thePlayer, 220, 180, 20, false) 
            return 
        end 
         
        local theVehicle = getPedOccupiedVehicle(thePlayer) -- Get the occupied vehicle 
        if (theVehicle) then 
            if (getVehicleController(theVehicle) ~= thePlayer) then -- If the player is not the driver, remove him from the vehicle 
                removePedFromVehicle(thePlayer) 
            else -- If the player is the driver, then delete their current vehicle 
                destroyElement(theVehicle) 
            end 
        end 
         
        local x, y, z = getElementPosition(thePlayer) 
        local newVehicle = createVehicle(vehicleModel, x+3, y, z) -- Create a vehicle with the player's coordinates 
        table.insert(vehicles, newVehicle) -- Insert the vehicle to the vehicles table 
        setElementData(newVehicle, "vehicle.id", #vehicles, false) -- Give the vehicle a unique ID as element data 
         
        outputChatBox("Vehicle spawned with ID " .. #vehicles .. ".", thePlayer, 20, 245, 20, false) 
    end 
) 
  
addEventHandler("onVehicleEnter", root, 
    function(thePlayer, seat, jacked) 
        if (not getElementData(source, "vehicle.id")) then -- If the vehicle has no vehicle ID, then continue 
            table.insert(vehicles, source) -- Insert the vehicle to the vehicles table 
            setElementData(source, "vehicle.id", #vehicles, false) -- Give the vehicle a unique ID as element data 
            outputChatBox("This vehicle's ID has been set to " .. #vehicles .. ".", thePlayer, 20, 245, 20, false) 
        end 
    end 
) 

Edited by Guest
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...