Trying to run this script per player!


#1
local arrest = 0
selectedVehicle = {}
local pulledOver = false
pulledOverPed = nil
local pulledOverVehicles = {}
myPlayer = GetPlayerPed(-1)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		if(myPlayer == -1)then
			myPlayer = GetPlayerPed(-1))
		end
		
		if IsControlJustReleased(0, 27) then	
			
			if(IsPedInAnyVehicle(GetPlayerPed(-1), false))then
				if(selectedVehicle[myPlayer] ~= nil)then
					TriggerServerEvent("pulloverCanceled")
					ShowNotification("Pullover cancelled.")
					
					
					pulledOverPed = nil
					pulledOver = false
				else
					local pc = GetEntityCoords(GetPlayerPed(-1))
					local closest = GetClosestVehicle(pc['x'], pc['y'], pc['z'], 20.0, 0, 70)
					if(IsVehicleSeatFree(closest, -1) == false)then
						pulledOver = false
						pulledOverPed = nil
						if(closest ~= nil and closest ~= false)then
							TriggerServerEvent("selectedVehicle")
							ShowNotification("~r~Vehicle selected, ~w~Press ~g~Down Arrow ~w~to pull them over or ~g~Up Arrow ~w~to cancel.")
						end
					else
					end
				end
			end
		end
		if IsControlJustReleased(0, 173)then
			if(selectedVehicle[myPlayer] ~= nil)then
				
				if(IsPedInAnyVehicle(GetPlayerPed(-1), false))then
					if(pulledOver == true)then
						--ShowNotification("Arrow keys to move vehicle and { or } to rotate, press L-SHIFT to exist manual movement mode.")
						SetNuiFocus(true)
						SendNUIMessage({
							move_vehicle = 1
						})
					else
						local pc = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], 5.0, 35.0, 0.0)
						TriggerServerEvent("pulloverVehicle", pc['x'], pc['y'], pc['z'])
					end
				end
			end
		end
	end
end)

RegisterNetEvent("selectedVehicle")
AddEventHandler("selectedVehicle", function(owner)
	local pc = GetEntityCoords(GetPlayerPed(GetPlayerFromServerId(owner)))
	local entity = GetClosestVehicle(pc['x'], pc['y'], pc['z'], 20.0, 0, 70)
	
	TriggerServerEvent("print", "Myplayer: " .. tostring(myPlayer) .. ", " .. tostring(owner))
	
	selectedVehicle[owner] = entity
end)

RegisterNetEvent("pullingOver")
AddEventHandler("pullingOver", function(owner, x, y, z)		
	local entity = selectedVehicle[owner]
	
	TaskVehicleDriveToCoord(GetPedInVehicleSeat(entity, -1), entity, x, y, z, 40, 1, GetEntityModel(entity), 2883621, 0, -1)
end)

RegisterNetEvent("pulledOverOwner")
AddEventHandler("pulledOverOwner", function(owner, x, y, z)		
	local entity = selectedVehicle[myPlayer]
	local pc = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], 5.0, 25.0, 0.0)
	SetEntityCoords(entity, pc.x, pc.y, pc.z)
	SetVehicleOnGroundProperly(selectedVehicle[myPlayer])
	FreezeEntityPosition(selectedVehicle[myPlayer], true)
	
		pulledOverVehicles[selectedVehicle[myPlayer]] = true
	
	pulledOverPed = GetPedInVehicleSeat(selectedVehicle[myPlayer], -1)
end)

RegisterNetEvent("pulledOver")
AddEventHandler("pulledOver", function(owner, x, y, z)
	if(owner == myPlayer)then
		SetEntityCoords(selectedVehicle[owner], x, y, z)
		pulledOver = true
		pulledOverPed = GetPedInVehicleSeat(selectedVehicle[myPlayer], -1)
	end

	FreezeEntityPosition(selectedVehicle[owner], true)
	pulledOverVehicles[selectedVehicle[owner]] = true
	
	FreezeEntityPosition(selectedVehicle[owner], true)
	ShowNotification ("~y~Approuch the ~g~Driver!", 10000)
	Wait(13000)
	ShowNotification ("~y~Head back to your car and run the ~g~Drivers Name!", 10000)
	Wait(13000)
	ShowNotification ("~y~Approuch the ~g~Driver ~y~to issue a ~g~Ticket!", 10000)
	Wait(13000)
	arrest = math.random(35, 250)
	TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"Ticket Paid : "..arrest.."$")
	TriggerServerEvent('ar:missionComplete', arrest)
	ShowNotification ("~w~Head back to your car and press ~g~Up Arrow ~w~to let ~g~Driver ~w~go!", 10000)
end)		

RegisterNetEvent("pulloverCanceled")
AddEventHandler("pulloverCanceled", function(owner)
	pulledOverVehicles[selectedVehicle[owner]] = nil
	SetVehicleEngineOn(selectedVehicle[owner], true, true, true)
	TaskSetBlockingOfNonTemporaryEvents(GetPedInVehicleSeat(selectedVehicle[owner], -1), false)
	SetVehicleSteerBias(selectedVehicle[owner], 0.0)
	FreezeEntityPosition(selectedVehicle[owner], false)
	selectedVehicle[owner] = nil
end)	

RegisterNetEvent("droveOff")
AddEventHandler("droveOff", function(owner)
	FreezeEntityPosition(selectedVehicle[owner], false)
	selectedVehicle[owner] = nil
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)		
		if(selectedVehicle[myPlayer] ~= nil)then
			local playerLoc = GetEntityCoords(GetPlayerPed(-1))
			local vehLoc = GetEntityCoords(selectedVehicle[myPlayer])
			if(Vdist(vehLoc['x'], vehLoc['y'], vehLoc['z'], playerLoc['x'], playerLoc['y'], playerLoc['z']) > 100.0)then
				TriggerServerEvent("droveOff", selectedVehicle[myPlayer])
			end
			
			DrawMarker(0, vehLoc['x'], vehLoc['y'], vehLoc['z'] + 1.8, 0, 0, 0, 0, 0, 0, 0.5001, 0.5001, 0.5001, 255,255,0,255, 0,0, 0,0)
		end
		for k,v in pairs(pulledOverVehicles) do
			FreezeEntityPosition(k, true)
			SetVehicleEngineOn(k, false, true, true)
			TaskSetBlockingOfNonTemporaryEvents(GetPedInVehicleSeat(k, -1), true)
		end
	end
end)

RegisterNUICallback('move', function(data, cb)
	if(pulledOver == false or selectedVehicle == false or IsPedInAnyVehicle(GetPlayerPed(-1)) == false)then
	else
		if(data.amount == nil)then
			data.amount = 0
		end
		
		local park = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], 0.0, 0.0, 0.0)
		local yaw = GetEntityHeading(selectedVehicle[myPlayer])
		local direction = data.direction
		if(direction == "forward")then
			park = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], 0.0, 0.5, 0.0)
		elseif(direction == "backward")then
			park = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], 0.0, -0.5, 0.0)
		elseif(direction == "left")then
			park = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], -0.5, 0.0, 0.0)
		elseif(direction == "right")then
			park = GetOffsetFromEntityInWorldCoords(selectedVehicle[myPlayer], 0.5, 0.0, 0.0)
		elseif(direction == "rotate")then
			yaw = yaw + data.amount
		end
		
		TriggerServerEvent("moveVehicle", park.x, park.y, park.z, yaw)
	end
end)

RegisterNetEvent("pulledOverMoveVehicle")
AddEventHandler("pulledOverMoveVehicle", function(owner, x, y, z, yaw)
	pulledOverVehicles[selectedVehicle[owner]] = nil
	FreezeEntityPosition(selectedVehicle[owner], false)
	SetEntityCoords(selectedVehicle[owner], x, y, z)
	SetEntityHeading(selectedVehicle[owner], yaw)
	FreezeEntityPosition(selectedVehicle[owner], true)
	pulledOverVehicles[selectedVehicle[owner]] = true
end)

RegisterNUICallback('close', function(data, cb)
	if(data.message == false)then
		SetNuiFocus(false)
		return
	end

	ShowNotification("" .. data.message)
	SetNuiFocus(false)
end)

Trying to make this script not send everyone msgs, And pay everyone. Any ideas thanks in advance :slight_smile:


#2

Don’t run TriggerClientEvent("event", -1) on the server then?


#3

So just remove all those? Sorry still pretty new here with Lua.


#4

If you don’t want to trigger the event on all clients then, yea.

See: https://wiki.fivem.net/wiki/TriggerClientEvent


#5

k thanks for the info maybe you could help me out in Discord or something :slight_smile: Realy trying to learn!


#6

So basically its all server triggers right I just need to change them to be client. Just trying to understand this.


#7

No… Not what I said. You have server side code, don’t you? The server side code will be triggering an event on all clients, that needs to be removed. I can’t/won’t tell you exactly how to get this working. Even if I wanted to, I couldn’t due to you not showing all of the source code.


#8

so all this is client scripts but sync is server side. And of course the payout. The script is a modified LSPDFR script to have a police job that gets random ticket money. I had to remove most the UI stuff I don’t need and conflicts with my other UI. So I have it working the way I want it. But every gets paid and everyone sees the messages. Do you want to see the server sync script? Thanks for your time :slight_smile:


#9
local arresting = {}
local arrested = {}

RegisterServerEvent("COPArrestingPed")
AddEventHandler("COPArrestingPed", function(ped)
	if(ped ~= nil and arresting[ped] == nil)then
		TriggerClientEvent("arrestingPed", -1, ped)
		SetTimeout(1500, function()
			arresting[ped] = true
		end)
	end
end)

RegisterServerEvent("COPArrestedPed")
AddEventHandler("COPArrestedPed", function(ped)
	if(ped ~= nil and arresting[ped] ~= nil)then
		arresting[ped] = nil
		arrested[ped] = true
		
		TriggerClientEvent("arrestPed", -1, source)
		TriggerClientEvent("notify", source, "Arrested, walk to them and press E for more options.")
	end
end)

RegisterServerEvent("releasePed")
AddEventHandler("releasePed", function(ent)
	TriggerClientEvent("releasePed", -1, ent)
	TriggerClientEvent("pulloverCanceled", -1, source)
end)

RegisterServerEvent("selectedVehicle")
AddEventHandler("selectedVehicle", function()
	TriggerClientEvent("selectedVehicle", -1, source)
end)

RegisterServerEvent("pulloverCanceled")
AddEventHandler("pulloverCanceled", function()
	TriggerClientEvent("pulloverCanceled", -1, source)
end)

RegisterServerEvent("pulloverVehicle")
AddEventHandler("pulloverVehicle", function(x, y, z)
	TriggerClientEvent("notify", source, "Vehicle pulling over.")	
	TriggerClientEvent("pullingOver", -1, source, x, y, z)
	
	SetTimeout(3500, function()
		TriggerClientEvent("notify", source, "Not happy with the position? Press L-SHIFT to enable manual movement or press E to cancel pullover.")
		TriggerClientEvent("pulledOver", -1, source, x, y, z)
	end)
end)

RegisterServerEvent("print")
AddEventHandler("print", function(t)
	print(""..t)
end)

RegisterServerEvent("droveOff")
AddEventHandler("droveOff", function(ent)
	print("Cancelled")
	TriggerClientEvent("droveOff", -1, source)
end)

RegisterServerEvent("moveVehicle")
AddEventHandler("moveVehicle", function(x, y, z, yaw)
	TriggerClientEvent("pulledOverMoveVehicle", -1, source, x, y, z, yaw)
end)

RegisterServerEvent("COPUnarrestPed")
AddEventHandler("COPUnarrestPed", function(ped)
	if(ped ~= nil)then
		arresting[ped] = nil
		TriggerClientEvent("unarrestingPed", -1, ped)
	end
end)

This is the server side. And I do not use arrest ped script its disabled. So just trying to get pullover only client seen for msgs and payouts.


#10

Got it just set to check ped client side now it works :slight_smile:


#11

yea not sure still on how to keep it to one player. If anyone is in a vehicle or cop car they see all the msgs. Any way to target a specific player only?


#12

FreezeEntityPosition(selectedVehicle[owner], true)
pulledOverVehicles[selectedVehicle[owner]] = true

FreezeEntityPosition(selectedVehicle[owner], true)
if IsVehicleModel(GetVehiclePedIsUsing(GetPlayerPed(-1)), GetHashKey("police", _r)) then
ShowNotification ("~y~Approuch the ~g~Driver!", 10000)
Wait(13000)
ShowNotification ("~y~Head back to your car and run the ~g~Drivers Name!", 10000)
Wait(13000)
ShowNotification ("~y~Approuch the ~g~Driver ~y~to issue a ~g~Ticket!", 10000)
Wait(13000)
arrest = math.random(35, 250)
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0},"Ticket Paid : "..arrest.."$")
TriggerServerEvent('ar:missionComplete', arrest)
ShowNotification ("~w~Head back to your car and press ~g~Up Arrow ~w~to let ~g~Driver ~w~go!", 10000)
end

end)

Something that will only target the player doing the pullover.


#13

If a person keeps asking this many times, he should get “Justin Bieber” rank on this forum. Swear to God…

Asking someone for personal assistance is breaking the main purpose of this forum - sharing knowledge with everyone on this forum. He is not your personal teacher. If he has time, he will reply to your thread where you are asking for help. Anyone who has time and is willing to help will reply. You want him to help you privately so he could be asked the same question by other people privately for hundreds or thousands of times more?

And make friends with the edit button instead of making separate continuous posts.

Sorry for the off-topic…


Getting Started
#14

My bad :frowning: Just trying to get some help. It’s not like you have to follow this post or anything.

My issue is fixed just needed to add this before the shownotifications if(selectedVehicle[myPlayer] ~= nil)then Solved!