Jump to content

Timers for individual players


Lergen

Recommended Posts

I have this script here that lets a player spawn a vehicle. I tried adding a timer to it so that there's a two minute cooldown before spawning another vehicle, it's supposed to be on a player-by-player basis but it's still global. Can someone tell me what I'm doing wrong? I'd really appreciate it.

local spawnDelayTime = 120000 -- delay time in ms
local spawnTimer = { } 
function carSpawn ()
    if isTimer(spawnTimer[source]) then 
        local timeleft = math.ceil( getTimerDetails(spawnTimer[source])/1000 ) -- Get seconds left from timer
        
        if timeleft then 
            local secondString = " second" 
            if timeleft > 1 then secondString = " seconds"
			end
            outputChatBox("You may spawn your vehicle again in "..tostring(timeleft)..secondString) 
            return false 
        end 
    end 
  if not (isGuestAccount (getPlayerAccount (source))) and not (isPedInVehicle(source)) then
    spawnTimer[source] = setTimer(function() spawnTimer[source] = false end,spawnDelayTime,1) 

 

Link to comment
local spawnDelayTime = 120000 -- delay time in ms
local spawnTimer = { } 
function carSpawn ()
    if isTimer(spawnTimer[source]) then 
        local timeleft = math.ceil( getTimerDetails(spawnTimer[source])/1000 ) -- Get seconds left from timer
        
        if timeleft then 
            local secondString = " second" 
            if timeleft > 1 then secondString = " seconds"
            end
            outputChatBox("You may spawn your vehicle again in "..tostring(timeleft)..secondString) 
            return false 
        end 
    else
        if not (isGuestAccount (getPlayerAccount (source))) and not (isPedInVehicle(source)) then
            spawnTimer[source] = setTimer(function(source) spawnTimer[source] = false end,spawnDelayTime,1,source) 
        end
    end 
end

 

  • Thanks 1
Link to comment
local spawnDelayTime = 120000 -- delay time in ms
local spawnTimers = { } 
function carSpawn ()
    if spawnTimer[source] then
		local timePassed = tonumber(getTickCount() - spawnTimer[source])
		if timePassed < spawnDelayTime then
			 local timeleft = math.ceil(timePassed/1000 ) -- Get seconds left from timer
             local secondString = " second" 
             if timeleft > 1 then secondString = " seconds"
            outputChatBox("You may spawn your vehicle again in "..tostring(timeleft)..secondString) 
            return false 
        end 
    end 
  if not (isGuestAccount (getPlayerAccount (source))) and not (isPedInVehicle(source)) then
	 spawnTimer[source] = getTickCount()
 end

This is more resource-efficient as it doesn't create timers for every player on your server. Unfortunately, I haven't tested it but it should work, in theory.

  • Thanks 1
Link to comment
On 06/06/2019 at 05:55, Shux said:

local spawnDelayTime = 120000 -- delay time in ms
local spawnTimer = { } 
function carSpawn ()
    if isTimer(spawnTimer[source]) then 
        local timeleft = math.ceil( getTimerDetails(spawnTimer[source])/1000 ) -- Get seconds left from timer
        
        if timeleft then 
            local secondString = " second" 
            if timeleft > 1 then secondString = " seconds"
            end
            outputChatBox("You may spawn your vehicle again in "..tostring(timeleft)..secondString) 
            return false 
        end 
    else
        if not (isGuestAccount (getPlayerAccount (source))) and not (isPedInVehicle(source)) then
            spawnTimer[source] = setTimer(function(source) spawnTimer[source] = false end,spawnDelayTime,1,source) 
        end
    end 
end

 

That did the trick, thanks!

 

On 06/06/2019 at 10:16, Aimcac said:

This is more resource-efficient as it doesn't create timers for every player on your server. Unfortunately, I haven't tested it but it should work, in theory.

I tried this out, but I couldn't get it working. I'll try tinkering with it though. What's the difference between TickCount and Timers that make it more optimized?

Link to comment
On 09/06/2019 at 07:45, Lergen said:

That did the trick, thanks!

 

I tried this out, but I couldn't get it working. I'll try tinkering with it though. What's the difference between TickCount and Timers that make it more optimized?

Timers create a timer object which horde, albeit a minute amount, resources. Tick counts on the other hand just check the time since your computer has been running, comparisons can give you differences in time, just like a timer.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...