[HELP] My script only works sometimes

Hey, so I’m trying to create a treasure hunt script where when the player starts the hunt, a random waypoint is placed for them to travel to. This part was working fine before but now sometimes when I start the hunt, the waypoint is not set but if I try to start another hunt it says that a hunt is already started.

I think as I added more features I broke my script and in future I will probably make a backup each time I try to add new things.

This is where I call the code that creates a random way point:

RegisterNetEvent('cypher-treasure:client:randomLocation', function()
  if isHuntActive == false then
    RandomLocation()
    CheckDistance()
  else
    ShowMessage('You Are Already Hunting!', 'error')
  end
end)

And this is the code for creating a random location:

function RandomLocation()
  isHuntActive = true
  bool = true
  for i = 1, #Config.Locations do
    local randomNum = math.random(1, #Config.Locations)
      if i == randomNum then
        for k,v in pairs(Config.Locations[i]) do
          val = Config.Locations[i]
          SetNewWaypoint(val.coords)
      end
    end
  end
end

If more info is needed please just let me know and I’ll provide it for you, thanks for the help as always! :smiley:

I think the problem comes from your RandomLocation() function

From what I see, you have an iteration on your locations.
In each iteration you choose a random
If your random is good you do a new loop (useless)
And you set your point

Except that it is possible that your random will never be equal to the I of the 1st loop

Try this code

function RandomLocation()
    local location = Config.locations[math.random(1, #Config.Locations)]
    if location ~= nil then
        SetNewWaypoint(location.coords)
        isHuntActive = true
        bool = true
    end
end
1 Like

Hey @Derass thanks for the reply! I cant test out the code you gave me because now I get this error:

I cant test because since I get this error, I dont get the option to search for the treasure and end the hunt so when I go back to see if I can start another hunt, it still thinks im doing one.

Maybe you can help me solve this issue? The function that is throwing this error is here:

  function CheckDistance()
    CreateThread(function()
      while bool do
        Citizen.Wait(500)
        local playerCoords = GetEntityCoords(playerId)
        local distance = #(playerCoords - val.coords)
        if distance < 5 then
          bool = false
          DisplayActionMessage('Press E to search for treasure')
          ShowMessage('X Marks The Spot!', 'success')
          WaitForButton()
          isHuntActive = false
        end
      end
      print(tostring(isHuntActive)..' end')
    end)
  end

I understand there might be some random bs in my code but I am just learning lol, thanks for your help!

Edit: the calculation in the distance variable was working fine before.

Try somethink like this

RegisterNetEvent('cypher-treasure:client:randomLocation', function()
  if isHuntActive == false then
    RandomLocation()
  else
    ShowMessage('You Are Already Hunting!', 'error')
  end
end)
function RandomLocation()
    local location = Config.locations[math.random(1, #Config.Locations)]
    if location ~= nil then
        SetNewWaypoint(location.coords)
        CheckDistance(location)   -- Start your check distance here when the location is define and pass the location in param
        isHuntActive = true
        bool = true
    end
end

function CheckDistance(location)
CreateThread(function()
    while bool do
        Wait(500)
        local playerCoords = GetEntityCoords(playerId)
        local distance = #(playerCoords - location.coords)
        if distance < 5 then
          bool = false
          DisplayActionMessage('Press E to search for treasure')
          ShowMessage('X Marks The Spot!', 'success')
          WaitForButton()
          isHuntActive = false
        end
    end
end)
end