Hey everybody,

I want to create a script where I drive to a gas station where my car can be repaired thus I have to check the player’s location whether it’s in that gas station location or not. It doesn’t work out for me, no message, nor I get killed.

What am I doing wrong?

-- Check if player is in area
local function PlayerInEventArea()
  SetTimeout(1000, function()
    local playerPos = GetEntityCoords(GetPlayerPed())
    --if playerPos <= vector3(284.14865112305,-1277.6623535156,29.248533248901) and playerPos >= vector3 (284.14865112305,-1277.6623535156,29.248533248901) then
    if IsEntityInArea(GetPlayerPed(-1), 249.55541992188, -1249.13671875, 29.127578735352, 284.14865112305,-1277.6623535156,29.248533248901, 0, 1, 0) then
      --TriggerEvent('chat:addMessage', {
        --args = {'To repair your vehicle, simply press E. Cost: $300'}})
        SetEntityHealth(GetPlayerPed(-1), 0)
    end
    PlayerInEventArea()
  end)
end

PlayerInEventArea()

Thanks in advance!

I would put that in a loop instead of a function like this. Also, if you’re trying to repair the vehicle, why are you trying to heal the player?

I’ve done that for testing. No real meaning. :smiley: I’ll try to loop that.

Don’t I have to create a thread? Will the server be stuck in a loop?

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
                local ped = GetPlayerPed(-1)
		if GetDistanceBetweenCoords(249.55541992188, -1249.13671875, 29.127578735352, GetEntityCoords(ped)) < 15.0 then
                SetEntityHealth(GetPlayerPed(-1), 0) --Change to whatever you want
		end
       end
end)

This should help

Use GetDistanceBetweenCoords(249.55541992188, -1249.13671875, 29.127578735352, GetEntityCoords(GetPlayerPed(-1))) < 15.0 to check if the Player is in an Area

I found that out some time ago too. I expanded my code, but there’s a little problem with the key pressed detection. This is so far I’ve got:

local playerPed = GetPlayerPed(-1)

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)

    local vehicle = IsPedInAnyVehicle(playerPed, false)

    if (GetDistanceBetweenCoords(265.10101318359, -1260.369140625, 29.292947769165, GetEntityCoords(playerPed)) < 15 and isPlayerBeingRepaired == false) then
      Notification("Press ~o~'E'~s~ for the following service:~n~Type: ~o~Repair~s~~n~Cost: ~r~$300~s~")

      if IsControlJustPressed(1, 38) then -- E
        isPlayerBeingRepaired = true
        Repair()
      end
    end
  end
end)


function Notification(msg)
	SetNotificationTextEntry("STRING")
	AddTextComponentString(msg)
  SetNotificationMessage("CHAR_DEFAULT", "CHAR_DEFAULT", true, 1, "Gas Station Attendant", "")
	DrawNotification(false, false)
end

function Repair()
  local plyCoords = GetEntityCoords(ply, 0)
  veh = GetClosestVehicle(plyCoords["x"], plyCoords["y"], plyCoords["z"], 5.001, 0, 70)

  Notification("~w~Your car is now being repaired! It might take up to ~g~10 seconds~w~.")
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), true)
  Citizen.Wait(10000)
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), false)
  SetVehicleEngineHealth(veh, 9999)
  SetVehiclePetrolTankHealth(veh, 9999)
  SetVehicleFixed(veh)
  Notification("~g~Car successfully fixed! Thank you for considering our services!")
  isPlayerBeingRepaired = false
end

Notification("~w~Your car is now being repaired! It might take up to ~g~10 seconds~w~.") never appears. Any suggestions?

Where do you define what ply is?

1 Like

It was a typo. Changed something a little bit.

function Repair()
  local veh = GetVehiclePedIsIn(playerPed, false)
  Notification("~w~Your car is now being repaired! It might take up to ~g~10 seconds~w~.")
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), true)
  Citizen.Wait(10000)
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), false)
  SetVehicleEngineHealth(veh, 1000)
  SetVehiclePetrolTankHealth(veh, 1000)
  SetVehicleDirtLevel(veh, 0)
  SetVehicleFixed(veh)
  Notification("~g~Car successfully fixed!~n~ Thank you for considering our services!")
  isPlayerBeingRepaired = false
  Citizen.Wait(0)
end

Wait a moment, i’ll try to get it to work on my server. I’ll send it if it works :slight_smile:

Well i kinda gut it to work, i’ve been trying to do it for the last hour and then i just thought f**k it and used Missiontext xD

Here if you don’t have Mission Text xD HERE

local playerPed = GetPlayerPed(-1)
local isPlayerBeingRepaired = false

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)

    local vehicle = IsPedInAnyVehicle(playerPed, false)

    if (GetDistanceBetweenCoords(265.10101318359, -1260.369140625, 29.292947769165, GetEntityCoords(playerPed)) < 15 and isPlayerBeingRepaired == false) then
      --Notification("Press ~o~'E'~s~ for the following service:~n~Type: ~o~Repair~s~~n~Cost: ~r~$300~s~")
	  TriggerEvent("mt:missiontext", "Press ~o~'E'~s~ for the following service:~n~Type: ~o~Repair~s~~n~Cost: ~r~$300~s~", 5000)
      if IsControlJustPressed(1, 38) then -- E
        isPlayerBeingRepaired = true
        Repair()
      end
    end
  end
end)


function Notification(msg)
	SetNotificationTextEntry("STRING")
	AddTextComponentString(msg)
  SetNotificationMessage("CHAR_DEFAULT", "CHAR_DEFAULT", true, 1, "Gas Station Attendant", "")
	DrawNotification(false, false)
end

function Repair()
  local veh = GetVehiclePedIsIn(playerPed, false)
  --Notification("~w~Your car is now being repaired! It might take up to ~g~10 seconds~w~.")
  TriggerEvent("mt:missiontext", "~w~Your car is now being repaired! It might take up to ~g~10 seconds~w~.", 10000)
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), true)
  Citizen.Wait(10000)
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), false)
  SetVehicleEngineHealth(veh, 1000)
  SetVehiclePetrolTankHealth(veh, 1000)
  SetVehicleDirtLevel(veh, 0)
  SetVehicleFixed(veh)
  --Notification("~g~Car successfully fixed!~n~ Thank you for considering our services!")
  TriggerEvent("mt:missiontext", "~g~Car successfully fixed!~n~ Thank you for considering our services!", 50000)
  Citizen.Wait(5000)
  isPlayerBeingRepaired = false
end

It looks like this:



local playerPed = GetPlayerPed(-1) should be in the thread loop incase the ped changes, ie: if they die.

True, forgot that xD…

Alright. Seems to work. I guess that the one notification doesn’t appear because as soon as Repair() is called, the Notification(“Press ~o~‘E’~s~ for the following service:~n~Type: ~o~Repair~s~~n~Cost: ~r~$300~s~”) still needs to spam out. I don’t know, maybe someone of you can explain this to me.

Thank you for your help.

What I have been trying to do now is having the player money deducted when using repair. I recently installed EssentialMode and managed to add some money to my account. This is what it currently looks like in client.lua:

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)

    local vehicle = IsPedInAnyVehicle(playerPed, false)
    playerPed = GetPlayerPed(-1)

    if (GetDistanceBetweenCoords(265.10101318359, -1260.369140625, 29.292947769165, GetEntityCoords(playerPed)) < 15 and isPlayerBeingRepaired == false) then
    Notification("Press ~o~'E'~s~ for the following service:~n~Type: ~o~Repair~s~~n~Cost: ~r~$300~s~")
	  --TriggerEvent("mt:missiontext", "Press ~o~'E'~s~ for the following service:~n~Type: ~o~Repair~s~~n~Cost: ~r~$300~s~", 5000)
      if IsControlJustPressed(1, 38) then -- E
        TriggerServerEvent('gasstation:calc')
      end
    end
  end
end)


function Notification(msg)
	SetNotificationTextEntry("STRING")
	AddTextComponentString(msg)
  SetNotificationMessage("CHAR_DEFAULT", "CHAR_DEFAULT", true, 1, "Gas Station Attendant", "")
	DrawNotification(false, false)
end

AddEventHandler('gasstation:repair', function()
  Citizen.Wait(20)
  isPlayerBeingRepaired = true
  local veh = GetVehiclePedIsIn(playerPed, false)
  local time = 10
  --Notification("~w~Your car is now being repaired! It might take up to ~g~10 seconds~w~.")
  NotificationRepairTime(time)
  Citizen.Wait(1000)
  time = time - 1
  Notification("~w~Alrighty! Let's do this. Gonna do some fixing here and there. Oh, and by the way: I'll clean your car too.")
  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), true)
  NotificationRepairTime(time)

  for i = 1, time do
    Citizen.Wait(1000)
    time = time - 1
    NotificationRepairTime(time)
  end

  FreezeEntityPosition(GetVehiclePedIsUsing(playerPed), false)
  SetVehicleEngineHealth(veh, 1000)
  SetVehiclePetrolTankHealth(veh, 1000)
  SetVehicleDirtLevel(veh, 0)
  SetVehicleFixed(veh)
  Notification("~g~Car successfully fixed!~n~ Thank you for considering our services!")
  --TriggerEvent("mt:missiontext", "~g~Car successfully fixed!~n~ Thank you for considering our services!", 10000)
  Citizen.Wait(5000)
  isPlayerBeingRepaired = false
end)

function NotificationRepairTime(time)
  TriggerEvent("mt:missiontext", "~w~Repair in progress. Estimated time: ~g~" .. time .. " seconds", 10000)
end

server.lua:


RegisterNetEvent('gasstation:calc')
AddEventHandler('gasstation:calc', function()
  TriggerEvent('es:getPlayerFromId', source, function(user)
    if (user.getMoney() < tonumber(300)) then
      TriggerEvent('mt:missiontext', "Sorry, but you haven't got enough money!", 10000)
    else
      user.removeMoney(tonumber(300))
      TriggerClientEvent('gasstation:repair')
    end
  end)
end)

Removing money works fine, but as soon as it gets to TriggerClientEvent('gasstation:repair'), I get this message in my console:

InvokeNative: execution failed: Argument at index 1 was null.
Await failure: Execution of native 000000002f7a49e6 in script host failed.
stack traceback:
        [C]: in upvalue '_in'
        citizen:/scripting/lua/natives_server.lua:334: in function 'TriggerClientEventInternal'
        (...tail calls...)
        server.lua:10: in upvalue 'ref'
        citizen:/scripting/lua/scheduler.lua:337: in function <citizen:/scripting/lua/scheduler.lua:336>
        [C]: in function 'xpcall'
        citizen:/scripting/lua/scheduler.lua:336: in function <citizen:/scripting/lua/scheduler.lua:335>
stack traceback:
        citizen:/scripting/lua/scheduler.lua:577: in local 'cb'
        server/player/login.lua:145: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:175: in function <citizen:/scripting/lua/scheduler.lua:174>Sending heartbeat to live-internal.fivem.net:30110

Got any ideas? Thanks in advance!