Cooldown for robbery script

Server.lua

RegisterServerEvent("24robbery")
AddEventHandler("24robbery", function(amount)
    local s = source
	TriggerClientEvent("chatMessage", s, tostring("^1NEWS:^3 Robbery in progress at the ^524/7^3!"))
end)

Client.lua

local cooldown = false

local table = {
    {x = 1961.58, y = 3749.25, z = 31.343738555908} -- Enter the coords of the maker here --
}

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        for k in pairs(table) do
            -- Draw Marker Here --
            DrawMarker(1, table[k].x, table[k].y, table[k].z, 0, 0, 0, 0, 0, 0, 1.001, 1.0001, 0.5001, 0, 0, 255, 200, 0, 0, 0, 0)
        end
    end
end)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        for k in pairs(table) do

            local plyCoords = GetEntityCoords(GetPlayerPed(-1), false)
            local dist = Vdist(plyCoords.x, plyCoords.y, plyCoords.z, table[k].x, table[k].y, table[k].z)

            if dist <= 1.2 then
				DrawTxt("Press E To Rob The 247")
				if IsControlJustPressed(1,51) then 
					if cooldown == false then
					TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
					TriggerServerEvent("24robbery", 2)
					cooldown = true
					Citizen.Wait(10000)
					cooldown = false
					elseif cooldown == true then
					TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " This store has recently been robbed! Please wait 10 seconds")
					end
				end
            end
        end
    end
end)

function DrawTxt(text)
  SetTextFont(0)
  SetTextProportional(1)
  SetTextScale(0.0, 0.45)
  SetTextDropshadow(1, 0, 0, 0, 255)
  SetTextEdge(1, 0, 0, 0, 255)

  SetTextDropShadow()
  SetTextOutline()
  SetTextEntry("STRING")
  AddTextComponentString(text)
  DrawText(0.174, 0.855)
end

So i have problem with this script at the moment, My problem is that it doesnt print the chatmessage under the “elseif cooldown == true then” and i have no idea why, Could i possibly get some help on this?

			if IsControlJustPressed(1,51) then 
					if cooldown == false then
					TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
					TriggerServerEvent("24robbery", 2)
					cooldown = true
					Citizen.Wait(10000)
					cooldown = false
					elseif cooldown == true then
					TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " This store has recently been robbed! Please wait 10 seconds")
					end
1 Like

Bump

Still need help

Bump

I really can’t figure this one out

The Citizen.Wait(10000) call means that this thread isn’t going to do anything for 10 seconds. That means that, during that time, the loop that checks position and input is not going to be checking whether the E key has been pressed, so that code will never get hit.

A simple way to fix that would be to do something like the following, to create a second thread to expire the cooldown after 10 seconds. The second thread won’t block the loop, so the loop will still able to respond to the user’s keypress/location as expected: (relevant part is handleCooldown(), but I included some minor performance/readability changes as well and left comments on them)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0) -- this probably does not need to be every tick. try higher numbers 

        local playerCoords = GetEntityCoords(GetPlayerPed(-1), false) -- moved this outside of the loop, since you shouldn't need to look up the player's position for each location

        for k, coords in pairs(table) do -- the second value returned by pairs is the value, so you don't need to do table[k], you can just access the value directly
            local dist = Vdist(playerCoords.x, playerCoords.y, playerCoords.z, coords.x, coords.y, coords.z)

            if dist <= 1.2 then
                DrawTxt("Press E To Rob The 247")
                if IsControlJustPressed(1,51) then 
                    if not cooldown then -- cooldown is a boolean, so you can just say 'not cooldown' to mean 'do this if cooldown isn't true'
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
                        TriggerServerEvent("24robbery", 2)
                        handleCooldown()
                    else -- since cooldown is a boolean and you've already checked its only other possible value, you just need to say 'else' to mean 'otherwise, if cooldown is true'
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " This store has recently been robbed! Please wait 10 seconds")
                    end
                end
            end
        end
    end
end)

function handleCooldown()
    cooldown = true
    Citizen.CreateThread(function()
        Citizen.Wait(10000)
        cooldown = false
    end)
end
2 Likes

Thank you @blarglebottoms , This was very helpful altough one more thing

Is it possible to make the Cooldown function into a int so i can have “Please try again in X minutes”?

And how would i then convert the timer value into minutes since i’m gonna change 10 seconds to like 30 minutes later.

No need to be rude, This is my first script why do you expect me to know everything?

Just because you don’t know a few things doesn’t mean that you should stop scripting because it “isn’t for you”.

Everyone’s gotta start somewhere.

Where you’re declaring the cooldown variable at the top, you can just set it to 0. Might be nice to put the initial cooldown length at the top (or even in a config file) so it’s not buried in the code when you decide to change it, too.

local initialCooldownSeconds = 60
local cooldownSecondsRemaining = 0

In handleCooldown, you could then set cooldownSecondsRemaining to the initialCooldownSeconds and make a loop in a thread that waits 1 second at a time and decrements cooldownSecondsRemaining. (I would not be surprised if there are better ways of doing this)

function handleCooldown()
    cooldownSecondsRemaining = initialCooldownSeconds
    Citizen.CreateThread(function()
        while cooldownSecondsRemaining > 0 do
            Citizen.Wait(1000)
            cooldownSecondsRemaining = cooldownSecondsRemaining - 1
        end
    end)
end

And then instead of ‘if not cooldown’, you’d just have to check if cooldownSecondsRemaining is <= 0. And if that isn’t true, you can use the cooldownSecondsRemaining to get the number of minutes/seconds left for the cooldown message.

                    if cooldownSecondsRemaining <= 0 then
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
                        TriggerServerEvent("24robbery", 2)
                        handleCooldown()
                    else
                        local minutes = math.floor(cooldownSecondsRemaining / 60) -- divide the total seconds remaining by 60 to get minutes, pass it to math.floor to strip off the decimals
                        local seconds = cooldownSecondsRemaining - minutes * 60 -- get the seconds left that don't make up a full minute
                        local cooldownMessage = string.format(" This store has recently been robbed! Please wait %dm, %ds", minutes, seconds)
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, cooldownMessage)
                    end
2 Likes

Thank’s a lot bro/sis

it was rude, and I’m sorry.

Best of luck

But also no need to pollute other threads, you coulda just DM’d me.

How would i create another marker and where you can press E and it does stuff?

I have tried messing around with the code for about 4-5 hours now but i never seem to get the script to know when i’m 1.2 feet away from it,

I have tried copying the local table table and renaming it to table2 and then copying over this code and changing table to table2

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(5) -- this probably does not need to be every tick. try higher numbers 
		

        local playerCoords = GetEntityCoords(GetPlayerPed(-1), false) -- moved this outside of the loop, since you shouldn't need to look up the player's position for each location

        for k, coords in pairs(table) do -- the second value returned by pairs is the value, so you don't need to do table[k], you can just access the value directly
            local dist = Vdist(playerCoords.x, playerCoords.y, playerCoords.z, coords.x, coords.y, coords.z)

            if dist <= 1.2 then
                DrawTxt("Press E To Rob The 24/7")
                if IsControlJustPressed(1,51) then 
                    if cooldownSecondsRemaining <= 0 then
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
                        TriggerServerEvent("24robbery", 2)
                        handleCooldown()
                    else
                        local minutes = math.floor(cooldownSecondsRemaining / 60) -- divide the total seconds remaining by 60 to get minutes, pass it to math.floor to strip off the decimals
                        local seconds = cooldownSecondsRemaining - minutes * 60 -- get the seconds left that don't make up a full minute
                        local cooldownMessage = string.format(" This store has recently been robbed! Please wait %dm, %ds", minutes, seconds)
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, cooldownMessage)
                    end
                end
            end
        end
    end
end)

I have also tried renaming stuff like playerCoords, coords and dist
to playerCoords2, coords2 and dist2
but it just doesnt work.
I’m really not sure how to add another location to it and would appreciate any help

full client code (without any code trying to add a second location, altough the second marker code is still there at the top):

local initialCooldownSeconds = 60
local cooldownSecondsRemaining = 0

local table = {
    {x = 1961.58, y = 3749.25, z = 31.343738555908} -- Enter the coords of the maker here --
}

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(5)
        for k in pairs(table) do
            -- Draw Marker Here --
            DrawMarker(1, table[k].x, table[k].y, table[k].z, 0, 0, 0, 0, 0, 0, 1.001, 1.0001, 0.5001, 0, 0, 255, 200, 0, 0, 0, 0)
        end
		DrawMarker(1, 1982.8, 3053.48, 46.26, 0, 0, 0, 0, 0, 0, 1.001, 1.0001, 0.5001, 0, 0, 255, 200, 0, 0, 0, 0)
    end
end)
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(5) -- this probably does not need to be every tick. try higher numbers 
		

        local playerCoords = GetEntityCoords(GetPlayerPed(-1), false) -- moved this outside of the loop, since you shouldn't need to look up the player's position for each location

        for k, coords in pairs(table) do -- the second value returned by pairs is the value, so you don't need to do table[k], you can just access the value directly
            local dist = Vdist(playerCoords.x, playerCoords.y, playerCoords.z, coords.x, coords.y, coords.z)

            if dist <= 1.2 then
                DrawTxt("Press E To Rob The 24/7")
                if IsControlJustPressed(1,51) then 
                    if cooldownSecondsRemaining <= 0 then
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
                        TriggerServerEvent("24robbery", 2)
                        handleCooldown()
                    else
                        local minutes = math.floor(cooldownSecondsRemaining / 60) -- divide the total seconds remaining by 60 to get minutes, pass it to math.floor to strip off the decimals
                        local seconds = cooldownSecondsRemaining - minutes * 60 -- get the seconds left that don't make up a full minute
                        local cooldownMessage = string.format(" This store has recently been robbed! Please wait %dm, %ds", minutes, seconds)
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, cooldownMessage)
                    end
                end
            end
        end
    end
end)
Citizen.CreateThread(function()
	while true do
	citizen.wait(5)
	
	        local playerCoords = GetEntityCoords(GetPlayerPed(-1), false) -- moved this outside of the loop, since you shouldn't need to look up the player's position for each location

        for k, coords in pairs(table) do -- the second value returned by pairs is the value, so you don't need to do table[k], you can just access the value directly
            local dist = Vdist(playerCoords.x, playerCoords.y, playerCoords.z, 1982.8, 3053.48, 46.26)

            if dist <= 1.2 then
                DrawTxt("Press E To Rob The Yellow Jack")
                if IsControlJustPressed(1,51) then 
                    if cooldownSecondsRemaining <= 0 then
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, " Robbery has been started and alarm has been triggered.")
                        TriggerServerEvent("24robbery", 2)
                        handleCooldown()
                    else
                        local minutes = math.floor(cooldownSecondsRemaining / 60) -- divide the total seconds remaining by 60 to get minutes, pass it to math.floor to strip off the decimals
                        local seconds = cooldownSecondsRemaining - minutes * 60 -- get the seconds left that don't make up a full minute
                        local cooldownMessage = string.format(" This store has recently been robbed! Please wait %dm, %ds", minutes, seconds)
                        TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, cooldownMessage)
                    end
                end
            end
        end
	
	
	end
end)

Citizen.CreateThread(function()
	while true do
	Citizen.Wait(1000)

		if cooldownSecondsRemaining == 1 then
			TriggerEvent('chatMessage', 'Robbery', {255, 0, 0}, "Robbery cooldown is now over.")
		end
	end
end)

function handleCooldown()
    cooldownSecondsRemaining = initialCooldownSeconds
    Citizen.CreateThread(function()
        while cooldownSecondsRemaining > 0 do
            Citizen.Wait(1000)
            cooldownSecondsRemaining = cooldownSecondsRemaining - 1
        end
    end)
end

function DrawTxt(text)
  SetTextFont(0)
  SetTextProportional(1)
  SetTextScale(0.0, 0.45)
  SetTextDropshadow(1, 0, 0, 0, 255)
  SetTextEdge(1, 0, 0, 0, 255)

  SetTextDropShadow()
  SetTextOutline()
  SetTextEntry("STRING")
  AddTextComponentString(text)
  DrawText(0.174, 0.855)
end

Don’t add a new DrawMarker call, you should be able to just add more coordinates to the table, like so:

local table = {  -- Enter the cords of the maker(s) here --
    {x = 1961.58, y = 3749.25, z = 31.343738555908},
    {x = 1982.8, y = 3053.48, z = 46.26}
}
1 Like

How would i then access it since the first row uses table[k].x etc

http://lua-users.org/wiki/TablesTutorial