So what i am trying to achieve is when you push a button, it either sets or remove a cruise speed of the vehicle, i got that part working. Only thing i am wondering is how i would call a event how to set the cruise speed, and how i would get the vehicle speed.
What i got right now is:
function MyPed()
return GetPlayerPed(-1)
end
local enableCruise = false;
local vehicleSpeed = 30;
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if IsControlJustPressed(1, 167) then --F6
if IsPedInAnyVehicle(MyPed(), false) then
if enableCruise then
TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Cruise is True");
enableCruise = false;
TriggerServerEvent('updateCruise', enableCruise, vehicleSpeed)
else
TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Cruise is False");
enableCruise = true;
end
end
end
end
end)
RegisterNetEvent('updateCruise')
AddEventHandler('updateCruise', function(PID, Toggle, Speed)
local Veh = GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(PID)), false)
SET_VEHICLE_FORWARD_SPEED(Speed, Toggle)
end)
So basicly, the part where it switches the bool etc. stll works, but i am not sure how to call in the eventHandler, and also not sure if how i set the vehicle speed is correct. Thanks in advance!
(I am familiair with other languages, just not with LUA tough)
Edit: I should note, i am trying to make it all server sided.
function MyPed()
return GetPlayerPed(-1)
end
local enableCruise = false;
local vehicleSpeed = 30;
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if IsControlJustPressed(1, 167) then --F6
if IsPedInAnyVehicle(MyPed(), false) then
local veh = PED.GET_VEHICLE_PED_IS_IN(MyPed(),true);
if enableCruise then
TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Cruise is True");
enableCruise = false;
SET_VEHICLE_FORWARD_SPEED(veh, vehicleSpeed);
else
TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Cruise is False");
enableCruise = true;
end
end
end
end
end)
It gives the error: Cannot trace back global ped at line 13.Attempt to index a nill value, and i have no idea why it responds nill.
Yeah, exactly what Tyler said. Also the SET_VEHICLE_FORWARD_SPEED needs to be changed. Check the natives.lua file in your FiveM folder for all of the natives in the format FiveM uses with Lua. Usually the functions are the same name, just without the underscores, but this isn’t always the case. For example, it would be SetVehicleForwardSpeed (I believe). I am not at my home computer right now so I can’t check it for you.
If you are using http://www.dev-c.com/nativedb/ , to the right of each function is green text (// 353FC880830B88FA 9C080899 for example). You can search that natives.lua file for the hash of the function (in that example, it would be 353FC880830B88FA) to get the function name.
Well i also got it from the side, but doing it the way you said indeed works, next time i will check in the if it works with this method then. Anyways, now it switches for 1 second, and then contuines.
I tried to search for a function to get the actual vehicle speed, but cant find one in the manuel.
Also how would you be able to make the SetVehicleForwardSpeed to contuine till enableCruise is false again, i tried it with while but that doesnt work considering it gets the veh when its pressed, and the while loop needs to be out of the IsControlPressed?
To get the speed use GET_ENTITY_SPEED(Entity entity) // D5037BA82E12416F 9E1E4798. Not sure of the exact name for FiveM, check that file with the method I described above.
For the speed to actually be set and remain, it would need to be in a loop. What I would do (probably not the best way, I’m no expert) is have it call an event. Inside that event, have another while loop. while enableCruise do
setspeed
end
Then once you hit the key again to disable the cruise, that should stop setting the speed because enableCruise would be false.
You would want to set the speed in the “else” part of your “if enableCruise then”. The way you have it set now is that it would set the speed as you disable it.
Citizen.CreateThread(function()
while true do
local playerped = GetPlayerPed(-1)
if IsControlJustPressed(1, 167) then --F6
if IsPedInAnyVehicle(playerped , false) then
local veh = GetVehiclePedIsUsing(playerped)
if enableCruise then
TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Cruise is True");
enableCruise = false;
SetVehicleForwardSpeed(veh, vehicleSpeed);
else
TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Cruise is False");
enableCruise = true;
end
end
Citizen.Wait(0)
end
end)
RegisterNetEvent('updateCruise')
AddEventHandler('updateCruise', function(sender , toggle)
local peds = GetplayerfromserveriD(sender)
local ped = GetPlayerPed(peds)
if DoesEntityExist(ped) and not IsEntityDead(ped) then
if ped ~= GetPlayerPed(-1) then
if IsPedInAnyVehicle(ped, false) then
local veh = GetVehiclePedIsUsing(ped)
SetVehicleForwardSpeed(veh, Toggle)
end
end
end
end)
This is what you’re looking for but it’s incomplete, you are missing a lot from it but this is how it should be . I just took your code and adjusted a little, added some vehicle / ped ID , identify and fixed all the small errors in the code.
Edit :
You are missing the function that is triggering the server part and the handler for the speed.
So trying this out, it doesnt get the speed but thats fixable, the main problem i have right now it goes through the loop once, but it doenst stay doing the EventHandler, so it also doesnt lock the speed.
as i said, trigerring on the server and handler. I am fighting with the /hu & /hd script, its not triggering and i don’t know why , if i will have some free time i will look on the natives and send you a PM with what you need to check.
Did you find a way? I am still strugling with it, tried to get something from other scripts but its still not triggering the function (Got the chat function in the eventhandler, but it doesnt trigger anything in it.
You shouldn’t have to use any events. Since you are doing this all to your own vehicle…
Make the keypress toggle enableCruise:
veh = GetVehiclePedIsUsing(GetPlayerPed(-1))
if IsControlJustPressed() then
enableCruise = not enableCruise -- inverts bool
cruiseSpeed = GetEntitySpeed(veh)
end
In a loop:
local veh = GetVehiclePedIsUsing(GetPlayerPed(-1))
Citizen.CreateThread(function()
while (enableCruise) do
Citizen.Wait(0)
SetVehicleForwardSpeed(veh, cruiseSpeed)
end
end)
For my own cruise control I just used SetEntityMaxSpeed(). This does not need to be in a loop so I call it once to change the max speed to 60MPH and then again to restore the original max speed.
no you didn;t get it, to take the car speed and go with it, not press a key and go with 35 , press it again you stop going, to take your curent speed in game and freeze it there.