[Help] If statement check only once?

-- Enter / Exit marker events
Citizen.CreateThread(function()

while true do

	Wait(0)

        local myId = PlayerId()
  	    local pid = GetPlayerFromServerId(id)
		local playerPed      = GetPlayerPed(-1)
		local coords         = GetEntityCoords(playerPed)
		local isInMarker     = false
		local currentStation = nil
		local currentPart    = nil
		local currentPartNum = nil
		local alreadyTalked  = false

		if GetDistanceBetweenCoords(coords, 2447.4636230469, 4964.5888671875, 46.571613311768 - 0.99,  true) < 1.5 then
			isInMarker     = true
			currentStation = FrankGreen
			currentPart    = 'FrankGreen'
			currentPartNum = i
			if GetDistanceBetweenCoords(GetEntityCoords(GetPlayerPed(myId)), GetEntityCoords(GetPlayerPed(pid)), true) < 19.999 then
				if not alreadyTalked then
				alreadyTalked = true
				TriggerEvent('chat:addMessage', {
      	        template = '<div style="padding: 0.5vw; margin: 0.5vw; background-color: rgba(0, 95, 0, 0.6); border-radius: 3px;"><i class="fas fa-child"></i> Frank Green: Witaj! Mam mały problem z drewnem na opał. Mógłbyś podskoczyć do pobliskiego lasu i przynieść mi z 10 sztuk drewna?</div>',
      	        args = { name, message }
				})
				end
			end		
		end
the rest of the code ...

Hello. This is my code and I want to show chat message only once when player enters the marker instead of keep spamming the chat with it when player stands inside the marker. This is why I created alreadyTalked boolean but it seems it’s only checking it once.

You’re recreating the alreadyTalked variable on every tick. Put the declaration of this variable outside of the while loop and it should work.

Might also want to add

if GetDistanceBetweenCoords(GetEntityCoords(GetPlayerPed(myId)), GetEntityCoords(GetPlayerPed(pid)), true) < 19.999 then
    if not alreadyTalked then
         alreadyTalked = true
	 TriggerEvent('chat:addMessage', {
      	        template = '<div style="padding: 0.5vw; margin: 0.5vw; background-color: rgba(0, 95, 0, 0.6); border-radius: 3px;"><i class="fas fa-child"></i> Frank Green: Witaj! Mam mały problem z drewnem na opał. Mógłbyś podskoczyć do pobliskiego lasu i przynieść mi z 10 sztuk drewna?</div>',
      	        args = { name, message }
				})
    end
else
    alreadyTalked = false
end	
1 Like

Wonderful. Putting variable outside of the while loop made it work. Thanks :slight_smile:

1 Like