Jump to content

A better way of checking time rather than making an unlimited timer?


Bean666

Recommended Posts

Is there anyway i can check time rather than making an unlimited timer? which I think would case a lil bit of a lag?( i think )

nighttime = false;

function change() 
    local timehour, timeminute = getTime()
    if timehour == 21 or timehour == 22 or timehour == 23 or timehour == 0 or timehour == 1 or timehour == 2 or timehour == 3 or timehour == 4 then
if nighttime == true then return end
    nighttime = true;
    chaseanim = "sprint_panic"
     outputChatBox("Night", root, 255, 0, 0)
else
if nighttime == false then return end
    chaseanim = "run_1armed"
   nighttime = false;
   outputChatBox("Day", root, 0, 255, 0)
    end
end
setTimer(change, 1000, 0)

 

Link to post

I don't know if there is a better way to do it but you could put a longer period between the checks like 5-10 seconds cuz this 1 second is quite frequently (in my opinion).
Other than that it shouldn't cause lag as it's not a complex function with many operations.

Edited by SpecT
Link to post
56 minutes ago, SpecT said:

I don't know if there is a better way to do it but you could put a longer period between the checks like 5-10 seconds cuz this 1 second is quite frequently (in my opinion).
Other than that it shouldn't cause lag as it's not a complex function with many operations.

this is a different topic but I wonder if you could help me with these. Basically I want the "showheli" event triggered if there's no Blue Team player present in the colshape and "showheli" will not be triggered if there's a blueteam player present in the colshape, but on the first code the showheli event doesnt trigger even tho theres no people with blue team on the colshape, on the second code, even if a player in blue team is in colshape it triggers, so how do I make it work? I've been struggling with this "getElementsWithinColShape" thing.

function stage100()
setTimer(stage110, 10000, 1)
for i, c in ipairs(alliedteams) do 
for k, v in ipairs(getPlayersInTeam(getTeamFromName((c)[1]))) do
if isElementWithinColShape(v, rc) then 
local theVehicle = getPedOccupiedVehicle ( v )
   if theVehicle then
    outputChatBox("#54F764[Rescue-Mission]: #FF0000Warning! You are in a vehicle, get off your vehicle to earn progress!", v, 255, 0, 0, true)
else
triggerClientEvent("draw100", v)
end
end
end
end
for i, r in ipairs(getPlayersInTeam(getTeamFromName("Blue Team"))) do 
if isElementWithinColShape(r, rc) then 
outputChatBox("Can't trigger event, blue team is in colshape", root) return end 
local players = getElementsWithinColShape(rc, "player")
if #players >= 1 then
triggerClientEvent("showheli", root, hiddenmarker)
helicopter = true;
end
end 
end

 

second:

function stage100()
setTimer(stage110, 10000, 1)
for i, c in ipairs(alliedteams) do 
for k, v in ipairs(getPlayersInTeam(getTeamFromName((c)[1]))) do
if isElementWithinColShape(v, rc) then 
local theVehicle = getPedOccupiedVehicle ( v )
   if theVehicle then
    outputChatBox("#54F764[Rescue-Mission]: #FF0000Warning! You are in a vehicle, get off your vehicle to earn progress!", v, 255, 0, 0, true)
else
triggerClientEvent("draw100", v)
end
end
end
end
local players = getElementsWithinColShape(rc, "player")
if ( players and getTeamName ( players ) == "Blue Team" ) then outputChatBox("Can't trigger event, blue team is in colshape", root) return end 
if #players >= 1 then
triggerClientEvent("showheli", root, hiddenmarker)
helicopter = true;
end
end

 

Link to post

Okay, so I think the second one is better BUT you forgot that getElementsWithinColShape returns a table
So you can't just use getTeamName by passing it the whole table 😄
It should be something like this:

function stage100()
	setTimer(stage110, 10000, 1)
	for i, c in ipairs(alliedteams) do 
		for k, v in ipairs(getPlayersInTeam(getTeamFromName((c)[1]))) do
			if isElementWithinColShape(v, rc) then 
				local theVehicle = getPedOccupiedVehicle ( v )
				if theVehicle then
					outputChatBox("#54F764[Rescue-Mission]: #FF0000Warning! You are in a vehicle, get off your vehicle to earn progress!", v, 255, 0, 0, true)
				else
					triggerClientEvent("draw100", v)
				end
			end
		end
	end
	local playersInCol = getElementsWithinColShape(rc, "player")
	if #playersInCol > 0 then
		for i, player in ipairs(playersInCol) do
			if getTeamName ( player ) == "Blue Team" then
				outputChatBox("Can't trigger event, blue team is in colshape", root) 
				return
			end
		end
		triggerClientEvent("showheli", root, hiddenmarker)
		helicopter = true;
	end 
end

 

OFF-TOPIC: It's a good practice to format your code in a way that is easy to read and track if there are any obvous mistakes.
Especially if you are going to keep growing into the coding sphere. 🙂

Edited by SpecT
Link to post
50 minutes ago, SpecT said:

Okay, so I think the second one is better BUT you forgot that getElementsWithinColShape returns a table
So you can't just use getTeamName by passing it the whole table 😄
It should be something like this:



function stage100()
	setTimer(stage110, 10000, 1)
	for i, c in ipairs(alliedteams) do 
		for k, v in ipairs(getPlayersInTeam(getTeamFromName((c)[1]))) do
			if isElementWithinColShape(v, rc) then 
				local theVehicle = getPedOccupiedVehicle ( v )
				if theVehicle then
					outputChatBox("#54F764[Rescue-Mission]: #FF0000Warning! You are in a vehicle, get off your vehicle to earn progress!", v, 255, 0, 0, true)
				else
					triggerClientEvent("draw100", v)
				end
			end
		end
	end
	local playersInCol = getElementsWithinColShape(rc, "player")
	if #playersInCol > 0 then
		for i, player in ipairs(playersInCol) do
			if getTeamName ( player ) == "Blue Team" then
				outputChatBox("Can't trigger event, blue team is in colshape", root) 
				return
			end
		end
		triggerClientEvent("showheli", root, hiddenmarker)
		helicopter = true;
	end 
end

 

OFF-TOPIC: It's a good practice to format your code in a way that is easy to read and track if there are any obvous mistakes.
Especially if you are going to keep growing into the coding sphere. 🙂

the event "showheli" is still being triggered despite someone in the blueteam is in the colshape and the outputChatBox doesnt output as well.

Edited by Bean666
Link to post

Oh, my bad...

getTeamName gets the name of a team element not player's team name. 🤦‍♂️

function stage100()
	setTimer(stage110, 10000, 1)
	for i, c in ipairs(alliedteams) do 
		for k, v in ipairs(getPlayersInTeam(getTeamFromName((c)[1]))) do
			if isElementWithinColShape(v, rc) then 
				local theVehicle = getPedOccupiedVehicle ( v )
				if theVehicle then
					outputChatBox("#54F764[Rescue-Mission]: #FF0000Warning! You are in a vehicle, get off your vehicle to earn progress!", v, 255, 0, 0, true)
				else
					triggerClientEvent("draw100", v)
				end
			end
		end
	end
	local playersInCol = getElementsWithinColShape(rc, "player")
	if #playersInCol > 0 then
		for i, player in ipairs(playersInCol) do
			local playerTeam = getPlayerTeam(player)
			if playerTeam and getTeamName (playerTeam) == "Blue Team" then
				outputChatBox("Can't trigger event, blue team is in colshape", root) 
				return
			end
		end
		triggerClientEvent("showheli", root, hiddenmarker)
		helicopter = true;
	end 
end

 

Edited by SpecT
  • Like 1
Link to post
37 minutes ago, SpecT said:

Oh, my bad...

getTeamName gets the name of a team element not player's team name. 🤦‍♂️


function stage100()
	setTimer(stage110, 10000, 1)
	for i, c in ipairs(alliedteams) do 
		for k, v in ipairs(getPlayersInTeam(getTeamFromName((c)[1]))) do
			if isElementWithinColShape(v, rc) then 
				local theVehicle = getPedOccupiedVehicle ( v )
				if theVehicle then
					outputChatBox("#54F764[Rescue-Mission]: #FF0000Warning! You are in a vehicle, get off your vehicle to earn progress!", v, 255, 0, 0, true)
				else
					triggerClientEvent("draw100", v)
				end
			end
		end
	end
	local playersInCol = getElementsWithinColShape(rc, "player")
	if #playersInCol > 0 then
		for i, player in ipairs(playersInCol) do
			local playerTeam = getPlayerTeam(player)
			if playerTeam and getTeamName (playerTeam) == "Blue Team" then
				outputChatBox("Can't trigger event, blue team is in colshape", root) 
				return
			end
		end
		triggerClientEvent("showheli", root, hiddenmarker)
		helicopter = true;
	end 
end

 

thankyou somuch.

  • Like 1
Link to post

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...