[HELP] TaskPlayAnim with npc

for i=1, #ShopClerk do
			local distance = GetDistanceBetweenCoords(ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, GetEntityCoords(GetPlayerPed(-1)))
			if distance < 6 then
				if (PlayingAnim == false) then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true)
					PlayingAnim = true
					Citizen.Wait(4000)
				else
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
				end
			else
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
                --PlayingAnim = false
			end
		end

animation stop but i can’t replay animation when i’m going away and coming again :confused: (because playinganims not set to false)

When I put it on false, this made a loop.

yes, animation stop when i’m further way, When the duration is set to 0, the animation is not played.
ClearPedTasks() does absolutly nothing :confused:

Your PlayingAnim = false is inside the < 6 check, false it outside of it so it has to make a new check

How could I do that?
I tried to set PlayingAnim = false; After the for, it doesn’t work

if distance > 7 then
PlayingAnim = false
end
directly below the end of if distance < 6 then

doesn’t work :frowning: npc loop his animation

All I can think if is ClearPedTasks() after the greeting anim, and forcing PlayingAnim to only become false when player is not near.
Try an elseif below the if PlayingAnim == false

elseif PlayingAnim ~= false then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)

nothing… :frowning:
I can’t find the solution to stop the animation of the npc
we must find a way to put PlayingAnim=false without affecting the distance by verifying that the player is out of the area.

Everything i try end in failure.
Whatever I do, the animation is played in loop.

please, help me :frowning:

You could do something like

for i=1, #ShopClerk do
	local PlayingAnim = false
	local distance = GetDistanceBetweenCoords(ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, GetEntityCoords(GetPlayerPed(-1)))
			if distance < 6 then
				if (PlayingAnim == false) then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true)
					PlayingAnim = not PlayingAnim
					Citizen.Wait(4000)
				else
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
				end
			elseif distance > 7 then
				ClearPedTasksImmediately(ShopClerk[i].id) 
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
                                 local PlayingAnim = false
			end
		end

I have already tried, it doesn’t work…
NPC does _greeting animation in loop, Try by yourself, you will see. :confused:

Unfortunately I’m not at my pc and I am not running that on my server…

Hmm so even if you exceed distance 7 then it still plays that animation?

Have you tried putting the clear pedal task immediately after the citizen. Wait and then adding task play animation of the idle animation. So basically when you get close enough It trigger the first animation waits for a complete cycle then clears that and plays the idle animation?

Are you running this for loop in a thread or are you only running it once?

Maybe throw some citizen.traces in there to debug what’s happening in the logic. When I wake up tomorrow I can work through it a bit more.

The animation is played loop when I am close to the citizen.(_greeting)
When he have to say hello, he says hello in loop.

same when i am not close to the citizen, but other animation (_idle_b).

Yeah if you paste the script here that you’re using to spawn the npcs and stuff I’ll test some stuff first thing in the morning and hopefully have some good news for ya.

okay thank you

https://pastebin.com/NFC5q1Pm

The only way I can stop the loop is by doing this:

		for i=1, #ShopClerk do
			distance = GetDistanceBetweenCoords(ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, GetEntityCoords(GetPlayerPed(-1)))
			if distance < 6 and PlayingAnim ~= true then
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true)
				PlayingAnim = true
				Citizen.Wait(4000)
				if PlayingAnim == true then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
				end
			else
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
			end
		end

Stops the loop after 1 greeting, but setting PlayingAnim = false will trigger a loop again…
Even tried a break, but no other result s:

EDIT: This seems to “fix” it. (Change distance < 6 to distance < 5.5)
https://pastebin.com/6xHNsnsG

1 Like
local shops = {
	{ ['x'] = -2508.97607421875, ['y'] = 3614.5859375, ['z'] = 13.7586479187012},
	--{ ['x'] = , ['y'] = , ['z'] = },
}
Citizen.CreateThread(function() -- This Sortoff fixes it, not the cleanest fix but "working".
    while true do
        Citizen.Wait(0)
        for _, Door in pairs(shops) do
            DrawMarker(1, Door.x, Door.y, Door.z, 0, 0, 0, 0, 0, 0, 1.001, 1.0001, 0.5001, 0, 255, 0, 255, 0, 0, 0, 0)
            local luld = GetDistanceBetweenCoords(Door.x, Door.y, Door.z, GetEntityCoords(GetPlayerPed(-1)))
            if luld > 6 then
                PlayingAnim = false
            end
        end
    end
end)

i have modified this part (a little bit).
I use markers for the interaction shop (gb foodshop).

When there are several positions it no longer works

1 Like

When there is only one marker, it works, when there is more than 1 markers, it doesn’t work any more, the animation does not block.

The same problem as at the beginning with ShopClerk

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		local Door = {
		{x = 29.054, y = -1350.059, z = 29.33},
		{x = -53.229, y = -1756.919, z = 29.420,},
		{x = -2504.132, y = 3615.676, z = 13.951},
		}
		for _, Door in pairs(Door) do
			DrawMarker(1, Door.x, Door.y, Door.z, 0, 0, 0, 0, 0, 0, 1.001, 1.0001, 0.5001, 0, 255, 0, 255, 0, 0, 0, 0)
			local luld = GetDistanceBetweenCoords(Door.x, Door.y, Door.z, GetEntityCoords(GetPlayerPed(-1)))
			if luld < 1 then
				PlayingAnim = false
			end
		end 
	end
end)

Works perfectly for me, Sure you are not setting the “Door.x Door.y Door.z” on the ped itself?

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
	
	if (not generalLoaded) then
	  
	  for i=1, #ShopClerk do
        RequestModel(GetHashKey(ShopClerk[i].modelHash))
        while not HasModelLoaded(GetHashKey(ShopClerk[i].modelHash)) do
          Wait(1)
        end
		
        ShopClerk[i].id = CreatePed(2, ShopClerk[i].modelHash, ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, ShopClerk[i].heading, false, true)
        SetPedFleeAttributes(ShopClerk[i].id, 0, 0)
		
      end
      generalLoaded = true
		
    end
	
		RequestAnimDict("random@shop_gunstore")
		while (not HasAnimDictLoaded("random@shop_gunstore")) do 
			Citizen.Wait(0) 
		end
		
		local ply = GetPlayerPed(-1)
		local plyCoords = GetEntityCoords(ply, 0)
		for i=1, #ShopClerk do
			distance = GetDistanceBetweenCoords(ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, GetEntityCoords(GetPlayerPed(-1)))
			if distance < 5.5 and PlayingAnim ~= true then
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true)
				PlayingAnim = true
				Citizen.Wait(4000)
				if PlayingAnim == true then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
				end
			else
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
			end
		end
		
  end
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		local Door = {
		--{x = 29.054, y = -1350.059, z = 29.33},
		--{x = -53.229, y = -1756.919, z = 29.420,},
		{x = -2504.132, y = 3615.676, z = 13.951},
-- only one position work, when i have 3 position for example, npc make a loop for saying hello
		}
		for _, Door in pairs(Door) do
			DrawMarker(1, Door.x, Door.y, Door.z, 0, 0, 0, 0, 0, 0, 1.001, 1.0001, 0.5001, 0, 255, 0, 255, 0, 0, 0, 0)
			local luld = GetDistanceBetweenCoords(Door.x, Door.y, Door.z, GetEntityCoords(GetPlayerPed(-1)))
			if luld > 6 then
				PlayingAnim = false
			end
		end 
	end
end)

nope :confused:
When I have a single position everything works, when there is more than 1, it makes the loop

1 Like

You have luld > 6 which means it’s always within the range of distance, leave it at < 1