Yoga Script help

I am brand new to coding and just trying to learn as much as I can, I am just working on this simple yoga script I dont have the emote part completely in but I cant even get the blip to show up on the map or anything else even the Help Display. The resource starts perfectly and does not give me any error msgs in the console so Im not sure why it’s not doing anything. Any help would be great thank you.

local showblips = true

local yogamats = {

{name="Yogamat", id=197, x=-1491.39, y=830.42, z=181.62},

}

Citizen.CreateThread(function()

while true do 

    Wait(0)

if nearYoga() then 

    DisplayHelpText("Press ~INPUT_PICKUP~ to start yoga ~g~")

end 

end)

–Blips

Citizen.CreateThread(function()

if showblips then

  for k,v in ipairs(yogamats)do

    local blip = AddBlipForCoord(v.x, v.y, v.z)

    SetBlipSprite(blip, v.id)

    SetBlipDisplay(blip, 4)

    SetBlipScale  (blip, 0.9)

    SetBlipColour (blip, 2)

    SetBlipAsShortRange(blip, true)

    BeginTextCommandSetBlipName("STRING")

    AddTextComponentString(tostring(v.name))

    EndTextCommandSetBlipName(blip)

  end

end

end)

–Capture Distance From Yoga Mats

function nearYoga()

local player = GetPlayerPed(-1)

local playerloc = GetEntityCoords(player, 0)

for _, search in pairs(yogamats) do

    local distance = GetDistanceBetweenCoords(search.x, search.y, search.z, playerloc['x'], playerloc['y'], playerloc['z'], true)

    if distance <= 3 then

        return true

    end 

end 

end

function DisplayHelpText(str)

SetTextComponentFormat("STRING")

AddTextComponentString(str)

DisplayHelpTextFromStringLabel(0, 0, 1, -1)

end

Hey ! :slightly_smiling_face:

I took some time to tweak your code, I did some testing with what I wrote and it’s now working.
Here is the code :

--Decalre an ESX varible
ESX = nil


--Wait to get the ESX Object
Citizen.CreateThread(function()

    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        Citizen.Wait(100)
    end
	
end)


showblips = true

yogamats = {}
yogamats[0] = {name="Yogamat", id=197, x=212.68, y=-84.1, z=69.22}


--Thread that will display the blips at resource start
Citizen.CreateThread(function()

	--Wait for 1 second
	Citizen.Wait(1000)
	
	--If [showblips] is true
	if showblips then
		for i,v in pairs(yogamats) do
		
			--Define coords for the Blip
			local blip = AddBlipForCoord(v.x, v.y, v.z)

			--Set the sprite for the Blip
			SetBlipSprite(blip, v.id)

			--Type of display (map + gps)
			SetBlipDisplay(blip, 2)

			--scale of the Blip
			SetBlipScale(blip, 0.9)
			
			--Color of the Blip(green)
			SetBlipColour(blip, 2)
			
			
			SetBlipAlpha(BlipsGoFast, 200)

			--Blip only show at short range
			SetBlipAsShortRange(blip, true)

			--Set the text of the BLip
			AddTextEntry('MYBLIP', v.name)
			BeginTextCommandSetBlipName('MYBLIP')

			EndTextCommandSetBlipName(blip)
			
			PulseBlip(blip)

		end
	end

end)


--Main Thread that checks every second if the player is near the Yogamat position
Citizen.CreateThread(function()

	while true do 

		Citizen.Wait(1000)
	
		--If the player is near the yogamat coords
		if( nearYoga() )  then 

			--Show an help notification to Press E to start (showed for 31 seconds)
			ESX.ShowHelpNotification("Press ~INPUT_PICKUP~ to start yoga ~g~" , false, true, 31000)
			
			--Wait until the actual HelpNotification desepear before displaying another one if the player is still their (otherwise it would blink)
			Citizen.Wait(35000)
			
		end 
	
	end

end)


--Function to test if the player is near the coordonates of the Yogamat pos
function nearYoga()

	local player = PlayerPedId()
	
	local playerloc = GetEntityCoords(player, 0)
	
	for _, coords in pairs(yogamats) do
		
		--Last arg is set to false otherwise we have a weird result for the return value if we set the 3D mode
		local distance = GetDistanceBetweenCoords(coords.x, coords.y, coords.y, playerloc.x, playerloc.y, playerloc.z, false)
	
		if( distance <= 3 ) then
		
			return true
		
		end
	
	end

end

It needs ESX, I dont know if you wanted it to be able to work without ESX, but the function [ ShowHelpNotification ] need ESX. I looked for a native function like the one you tried to use [ DisplayHelpText ] but doesn’t seem to exist according to the doc.

Peace !

Okay first off you are an absolute gentleman thank you for this. For some reason I cant get the script to do anything still. I’m wondering if its something with my fxmanifest?

No prob :slightly_smiling_face: .
It might be your fxmanifest indeed, if you don’t mind sharing it, I’ll take a look :wink:

Edit: If you could post a screen of the structure of your resource files&folders, it would help (as the paths must be indicate in the fxmanifest)

1 Like

Disregard. It is working I get the prompt and the blip is now on the screen!! Awesome and thank you for the comments above each part that will for sure help me in the future. Now onto getting the emote and everything going. For the emote part would i be fine just adding that to the bottom of what is already there or do I need to add it to the check if key is pressed portion.

I’m not sure what you mean by

But I did undersdand that you need to capture the Input if the [ E ] key is pressed and do something in that case. I tried to implement the [ IsControlJustPressed() ] function in the code that I sent you, but I realize that it wouldn’t work very well, or that we would need to run the main Thread every 10ms (wich isn’t good on the performance side).

So I modified the code once again, to include the player input capture while still keeping our main Thread running every 1000ms (at maximum). To do so I transfered part of our code into a new function [ displayNotification() ].

Here is portion of the code that I modified :

--Main Thread that checks every second if the player is near the Yogamat position
Citizen.CreateThread(function()

	while true do 

		Citizen.Wait(1000)
	
		--If the player is near the yogamat coords
		if( nearYoga() )  then 

			--Call the function [ displayNotification() ] 
			displayNotification()
			
		end 
	
	end

end)


--Function that will display the HelpNotification (for 31 seconds) and will capture the key press
function displayNotification()

	local loopTimer = 0
	
	--The loop will run for 31 seconds (310 times 10 milliseconds) 
	while loopTimer < 31000 do
	
		Citizen.Wait(10)
		ESX.ShowHelpNotification("Press ~INPUT_PICKUP~ to start yoga ~g~" , true, true, 31000)
					
		--If the [ E ] key is pressed then do somthing
		if IsControlJustPressed(0, 38) then
			--Here you can do what you want when the player press [ E ] (the print is just for testing)
			print("JUST PRESSED [ E ]!!!")
		end
			
		loopTimer = loopTimer + 10
		
	end
	

end

I might still not be the most optimize thing ever as I’m not well versed in Lua and just starting fiddling with CitizenFx. But it’s working :wink: