Police Dog Script

Hello all,

I was making a simple toggleable K9 for vRP police.

here’s what i have so far. When F1 is pressed it should spawn the dog, get it to follow the player. Then when its pressed again it simply deletes the dog.

RegisterNetEvent('PoliceDog')
AddEventHandler('PoliceDog', function(x, y, z)

	-- PED variables
    local ped = GetPlayerPed(-1) -- Define Player
    local heading = GetEntityHeading(ped) -- Gets the players looking direction
    local user_id = vRP.getUserId(source) -- get the Players ID
    local plyPos = GetEntityCoords(ped(1),  true) -- Define Player location


    -- K9 Variables
    local modelName = "A_C_Husky" -- Police K9 PED model
    local modelHash = (GetHashKey(modelName)) -- Gets the models HashKey

    RequestModel(modelHash) -- NOT SURE TBH

    local K9 = CreatePed(modelHash, plyPos.x + (frontx * 10), plyPos.y + (fronty * 10)-- Creates The K9 entity variable
    local K9Spawned = false -- is K9 spawned boolean

    Citizen.Wait(0) -- Prevents crashing
	if user_id ~= nil and vRP.hasPermission(user_id,"police.paycheck") then -- Check if player exists and is on duty as cop
		Citizen.Wait(0) -- Prevents crashing
		if IsControlJustPressed(3,288) and (K9Spawned = false) then -- F1 Key pressed do...
			

			-- Spawn in K9
			CreatePed(modelHash, plyPos.x + (frontx * 10), plyPos.y + (fronty * 10)
			TaskGoToEntity(K9, ped, -1, 1.0, 10.0, 1073741824.0, 0) -- Get K9 to follow player
			SetPedKeepTask(K9, true) -- Make sure the K9 doesnt run away cause his life is bad...

			K9Spawned = true
		else
			if IsControlJustPressed(3,288) and (K9Spawned = true) then
				DeletePed(K9)
				K9Spawned = false
			end
		end
	elseif user_id ~= nil and (ped ~= vRP.hasPermission(user_id,"police.paycheck")) then -- Prevents Cives without police.paycheck from spawning K9
		if IsControlJustPressed(3,288) then -- F1 Key pressed do...
			SetNotificationTextEntry("STRING") -- Makes the notification a string
			AddTextComponentString("~b~You are not on duty as a Police Officer.") -- Notification Text
			DrawNotification(true, false) -- Spawn notification
		end
	end    
end)

That’s all done on the client side. When i press the key the dog doesn’t spawn. Is the key wrong or is there something wring with the script.

Any ideas?

Regards
Catraz

Any errors? Have you put in print messages to see how far in the script its going in or even if its triggering at all?

Whats this supposed to mean?

https://runtime.fivem.net/doc/reference.html#_0xD49F9B0955C367DE

Try spawning it right on the player location and get your parameters correct. That will help, rest looks ok.

Just keep looking at other (released) scripts to see how it’s done.

Ok will try that now.

Thanks for the reply

Ok heres wht ive got.

RegisterNetEvent('PoliceDog')
AddEventHandler('PoliceDog', function(x, y, z)
	Citizen.Trace("Catraz_PoliceDogsDebug: Event Handler Added")

	-- PED variables
    local ped = GetPlayerPed(-1) -- Define Player
    local heading = GetEntityHeading(ped) -- Gets the players looking direction
    local user_id = vRP.getUserId(source) -- get the Players ID
    local plyPos = GetEntityCoords(ped(1),  true) -- Define Player location
    local rotation = 120.01 --Must have a decimal i think
    local NetworkSync= true -- shared with other players (they can see him, shoot him, kill him, etc...)



    -- K9 Variables
    local modelName = "A_C_Husky" -- Police K9 PED model
    local modelHash = (GetHashKey(modelName)) -- Gets the models HashKey

    RequestModel(modelHash) -- NOT SURE TBH

    local K9 = CreatePed(28, modelHash , plyPos.x, plyPos.y, plyPos.z, rotation, NetworkSync, NetworkSync)-- Creates The K9 entity variable
    local K9Spawned = false -- is K9 spawned boolean

	Citizen.Trace("Catraz_PoliceDogsDebug: Local Variables Defined")

    Citizen.Wait(0) -- Prevents crashing
	if (user_id ~= nil) and (vRP.hasPermission(user_id,"police.paycheck")) then -- Check if player exists and is on duty as cop
		Citizen.Wait(0) -- Prevents crashing
		if IsControlJustPressed(3,288) and (K9Spawned = false) then -- F1 Key pressed do...
			Citizen.Trace("Catraz_PoliceDogsDebug: F1 Key Pressed")	

			-- Spawn in K9
			CreatePed(28, modelHash , plyPos.x, plyPos.y, plyPos.z, rotation, NetworkSync, NetworkSync)
			Citizen.Trace("Catraz_PoliceDogsDebug: K9 Created")
			TaskGoToEntity(K9, ped, -1, 1.0, 10.0, 1073741824.0, 0) -- Get K9 to follow player
			SetPedKeepTask(K9, true) -- Make sure the K9 doesnt run away cause his life is bad...

			K9Spawned = true
		else
			if IsControlJustPressed(3,288) and (K9Spawned = true) then
				DeletePed(K9)
				K9Spawned = false
				Citizen.Trace("Catraz_PoliceDogsDebug: K9 Deleted")
			end
		end
	elseif (user_id ~= nil) and (ped ~= vRP.hasPermission(user_id,"police.paycheck")) then -- Prevents Cives without police.paycheck from spawning K9
		if IsControlJustPressed(3,288) then -- F1 Key pressed do...
			SetNotificationTextEntry("STRING") -- Makes the notification a string
			AddTextComponentString("~b~You are not on duty as a Police Officer.") -- Notification Text
			DrawNotification(true, false) -- Spawn notification
			Citizen.Trace("Catraz_PoliceDogsDebug: Spawn Request Denied (You are not an Officer)")
		end
	end    
end)

i get the error “:29: ‘)’ expected near ‘=’”

The line in question is

if IsControlJustPressed(3,288) and (K9Spawned = false) then

I couldn’t see the issue.

Therefore the traces don’t show

Can u?

It should be == not = if you are checking if K9Spawned is equal to false. However it would be even better if you just did if not K9Spawned then so it should look like this:

if IsControlJustPressed(3, 288) and not K9Spawned then

Or you could do:

if IsControlJustPressed(3, 288) and K9Spawned == false then

ok when i press the F1 nothing happens. None of the traces work or nothing.

I changed the bits you recommended and still not working.

The K9 script is a tough script that took me months to get where I have mine now. Just figured I should give you a heads up. :smile:

@xander1998 ok might just use yours then!

Could you help on this topic?

I mean what is it. Just tracking on duty police? Could be done all client or you can store on-duty police on server tavle and pass police to the clients that are also on-duty.

The script aims to have a dog spawn on the F1 key. The cop should be able to spawn the dog and then the dog follows him until the cop presses f1 again to despawn it.

Talking about the other one you just tagged me in.

Sorry yes. I want it to make a police blip on the map for on-duty police. However i only want the blips to show to other officers not civilians.

Can i use it do be a dog?

This script used to work really well, then it just randomly stopped working. No errors or anything - Is this a known issue?