Jump to content

Could anybody help with my mechanic repairing scirpts?


jingzhi

Recommended Posts

function getPlayerFromPartialName(name) 
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil 
    if name then 
        for _, player in ipairs(getElementsByType("player")) do 
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() 
            if name_:find(name, 1, true) then 
                return player 
            end 
        end 
    end 
end 
  
function repair(mechanic,command,player) 
    if getPlayerTeam(mechanic) == "Mechanic" then 
        getPlayerFromPartialName(player) == vehicleowner 
        if vehicleowner then 
            local mx,my,mz = getElementPosition(mechanic) 
            local x,y,z = getElementPosition(vehicleowner) 
            if  getDistanceBetweenPoints3D(mx,my,mz,x,y,z) < 3 then 
            local getPedVehicle(vehicleowner) == vehicle 
                if vehicle then 
                    repairfee = math.floor ( 1200 - getElementHealth(vehicle)) 
                    if getPlayerMoney(vehicleowner) >= repairfee then 
                        setElementData(vehicleowner,"temp.fixrequest", "yes") 
                        outputChatBox("Your repairing request was sent to "..getPlayerName(vehicleowner),mechanic,255,255,0) 
                        outputChatBox(mechanic.." wants to repair your vehicle for $ "..repairfee.." type /acceptrepair to accept",vehicleowner,255,255,0) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addCommandHandler("repair",repair) 
  
function acceptrepair(vehicleowner) 
    if getElementData(vehicleowner,"fixrequest") == "yes" then 
        fixVehicle(vehicle) 
        outputChatBox("Your "..getElementName(vehicle).."have been repaired",vehicleowner,255,255,0) 
    end 
end 
  
  
  
  

Hey guys I wrote this script based on the Useful function "getPlayerFromPartialName" from wiki, but it says there are some errors, here is the errors:

unexpected symbol near '==' (line 15)

syntax error near '==' (line 20)

Thank you! :)

Link to comment

Try now:

function getPlayerFromPartialName(name) 
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil 
    if name then 
        for _, player in ipairs(getElementsByType("player")) do 
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() 
            if name_:find(name, 1, true) then 
                return player 
            end 
        end 
    end 
end 
  
function repair(mechanic,command,player) 
    if getPlayerTeam(mechanic) == "Mechanic" then 
    local vehicleowner = getPlayerFromPartialName(player) 
        if vehicleowner then 
            local mx,my,mz = getElementPosition(mechanic) 
            local x,y,z = getElementPosition(vehicleowner) 
            if  getDistanceBetweenPoints3D(mx,my,mz,x,y,z) < 3 then 
                local vehicle = getPedOccupiedVehicle(vehicleowner) 
                if vehicle then 
                    repairfee = math.floor ( 1200 - getElementHealth(vehicle)) 
                    if getPlayerMoney(vehicleowner) >= repairfee then 
                        setElementData(vehicleowner,"temp.fixrequest", "yes") 
                        outputChatBox("Your repairing request was sent to "..getPlayerName(vehicleowner),mechanic,255,255,0) 
                        outputChatBox(mechanic.." wants to repair your vehicle for $ "..repairfee.." type /acceptrepair to accept",vehicleowner,255,255,0) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addCommandHandler("repair",repair) 
  
function acceptrepair(vehicleowner) 
    if getElementData(vehicleowner,"fixrequest") == "yes" then 
        fixVehicle(vehicle) 
        outputChatBox("Your "..getElementName(vehicle).."have been repaired",vehicleowner,255,255,0) 
    end 
end 

Link to comment
Try now:
function getPlayerFromPartialName(name) 
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil 
    if name then 
        for _, player in ipairs(getElementsByType("player")) do 
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() 
            if name_:find(name, 1, true) then 
                return player 
            end 
        end 
    end 
end 
  
function repair(mechanic,command,player) 
    if getPlayerTeam(mechanic) == "Mechanic" then 
    local vehicleowner = getPlayerFromPartialName(player) 
        if vehicleowner then 
            local mx,my,mz = getElementPosition(mechanic) 
            local x,y,z = getElementPosition(vehicleowner) 
            if  getDistanceBetweenPoints3D(mx,my,mz,x,y,z) < 3 then 
                local vehicle = getPedOccupiedVehicle(vehicleowner) 
                if vehicle then 
                    repairfee = math.floor ( 1200 - getElementHealth(vehicle)) 
                    if getPlayerMoney(vehicleowner) >= repairfee then 
                        setElementData(vehicleowner,"temp.fixrequest", "yes") 
                        outputChatBox("Your repairing request was sent to "..getPlayerName(vehicleowner),mechanic,255,255,0) 
                        outputChatBox(mechanic.." wants to repair your vehicle for $ "..repairfee.." type /acceptrepair to accept",vehicleowner,255,255,0) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addCommandHandler("repair",repair) 
  
function acceptrepair(vehicleowner) 
    if getElementData(vehicleowner,"fixrequest") == "yes" then 
        fixVehicle(vehicle) 
        outputChatBox("Your "..getElementName(vehicle).."have been repaired",vehicleowner,255,255,0) 
    end 
end 

The errors are gone, but it still doesnt work :-|

Link to comment
what is supposed to do ?

A mechanic comes around a car which need a repair while is in the car, so the mechanic types /repair , and then it will calculate the amount of money needed and see if can afford the repair, if yes, then it will out put a chatbox to to ask if the player want the mechanic to repair his vehicle, if types /accept, the vehicle will be fixed and he will pay the mechanic the fee

Hope you can help me :)

Link to comment

Why you can use GUI it's more easy example when you type t/repair then a custom GUI appear to this player

Ex :

  
local resX, resY = guiGetScreenSize() 
repairWindow = guiCreateWindow((resX/2)-(393/2),(resY/2)-(81/2),393,81,"",false) 
guiWindowSetSizable(repairWindow, false) 
acceptRepair = guiCreateButton(9,28,183,44,"Accept",false,repairWindow) 
declineRepair = guiCreateButton(202,28,183,44,"Decline",false,repairWindow) 
guiSetVisible(mechWindow, false) 

-- then you can add this

function acceptDecline(decline) 
    if (not guiGetVisible(repairWindow)) then return end 
    if (not isElement(theVeh)) then 
        -- triggerServerEvent  add you server event 
    elseif (source == acceptRepair) then 
        -- triggerServerEvent  add you server event 
    elseif (source == declineRepair or decline) then 
        -- triggerServerEvent  add you server event 
    end 
    guiSetVisible(repairWindow, false) 
    showCursor(false) 
end 
addEventHandler("onClientGUIClick", declineRepair, acceptDecline, false) 
addEventHandler("onClientGUIClick", acceptRepair, acceptDecline, false) 

Link to comment

Try this:

function getPlayerFromPartialName(name) 
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil 
    if name then 
        for _, player in ipairs(getElementsByType("player")) do 
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() 
            if name_:find(name, 1, true) then 
                return player 
            end 
        end 
    end 
end 
  
function onPlayerRequestRepair(command,player) 
    local mechanic = getPlayerTeam(player, "Mechanic") 
    if mechanic then 
        local vehicleowner = getPlayerFromPartialName(player) 
        if vehicleowner then 
            local mx,my,mz = getElementPosition(mechanic) 
            local x,y,z = getElementPosition(vehicleowner) 
            if getDistanceBetweenPoints3D(mx,my,mz,x,y,z) < 3 then 
                local vehicle = getPedOccupiedVehicle(vehicleowner) 
                if vehicle then 
                    local repairfee = math.floor( 1200 - getElementHealth(vehicle)) --not sure about this 
                    if getPlayerMoney(vehicleowner) >= repairfee then 
                        setElementData(vehicleowner,"fixrequest", true) 
                        setElementData(vehicleowner,"fee", tonumber(repairfee)) 
                        outputChatBox("Your repairing request was sent to "..getPlayerName(vehicleowner),mechanic,255,255,0) 
                        outputChatBox(mechanic.." wants to repair your vehicle for "..repairfee.." $ | Type /acceptrepair to accept",vehicleowner,255,255,0) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addCommandHandler("repair", onPlayerRequestRepair) 
  
function playerNeedToAcceptRepair(player) 
    if getElementData(player,"fixrequest", true) then 
        local vehicle = getPedOccupiedVehicle(player) 
        local fee = getElementData(player, "fee") 
        if vehicle then 
            fixVehicle(vehicle) 
            takePlayerMoney( tonumber(fee)) 
            setElementData(player, "fixrequest", false) 
            outputChatBox("Your vehicle have been repaired!",vehicleowner,255,255,0) 
        end 
    else 
        outputChatBox("No need to repair!",vehicleowner,255,255,0)     
    end 
end 
addCommandHandler("acceptrepair", playerNeedToAcceptRepair) 

Link to comment
Why you can use GUI it's more easy example when you type t/repair then a custom GUI appear to this player

Ex :

  
local resX, resY = guiGetScreenSize() 
repairWindow = guiCreateWindow((resX/2)-(393/2),(resY/2)-(81/2),393,81,"",false) 
guiWindowSetSizable(repairWindow, false) 
acceptRepair = guiCreateButton(9,28,183,44,"Accept",false,repairWindow) 
declineRepair = guiCreateButton(202,28,183,44,"Decline",false,repairWindow) 
guiSetVisible(mechWindow, false) 

-- then you can add this

function acceptDecline(decline) 
    if (not guiGetVisible(repairWindow)) then return end 
    if (not isElement(theVeh)) then 
        -- triggerServerEvent  add you server event 
    elseif (source == acceptRepair) then 
        -- triggerServerEvent  add you server event 
    elseif (source == declineRepair or decline) then 
        -- triggerServerEvent  add you server event 
    end 
    guiSetVisible(repairWindow, false) 
    showCursor(false) 
end 
addEventHandler("onClientGUIClick", declineRepair, acceptDecline, false) 
addEventHandler("onClientGUIClick", acceptRepair, acceptDecline, false) 

Thank you very much, but can you also tell me how to trigger this with the command that mechanic enters? (because mechanic enter /repair and this gui should be showed to :)

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