Jump to content

Dx-Timer


SkiZo

Recommended Posts

Hello .. So i'm trying to add timer .. dx timer .. 

it's suppost to be like this 

#Server Side.

addEvent("onBotWasted",true)
addEventHandler("onBotWasted",getRootElement(),
function (killer)
	if (source == nemesilv) then
		destroyElement(BlipNemesislv)
		startCase = setTimer (createBag, (60*1000*20), 1 )
--      startdxtimer = ( i don't know what suppost to be exactly ... but it's will be triggleevent ( dx in client side always ) 
      end
end)
#Client Side.

--i have no idea .. )= Help please !! 
--it should be 20Min counting down .. 

Sorry but i'm so bad at Dx and specially timer :'( 

Link to comment
11 minutes ago, Legend<3 said:

Hello .. So i'm trying to add timer .. dx timer .. 

it's suppost to be like this 


#Server Side.

addEvent("onBotWasted",true)
addEventHandler("onBotWasted",getRootElement(),
function (killer)
	if (source == nemesilv) then
		destroyElement(BlipNemesislv)
		startCase = setTimer (createBag, (60*1000*20), 1 )
--      startdxtimer = ( i don't know what suppost to be exactly ... but it's will be triggleevent ( dx in client side always ) 
      end
end)

#Client Side.

--i have no idea .. )= Help please !! 
--it should be 20Min counting down .. 

Sorry but i'm so bad at Dx and specially timer :'( 

 

If you're trying to run the timer for a client-side function, you can't add it server-side. The timer itself can't be cross executed like that, however, what you can do triggering it via a trigger

bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] ) 
bool addEventHandler ( string eventName, element attachedTo, function handlerFunction, [ bool getPropagated = true, string priority = "normal" ] )    
bool triggerClientEvent ( [table/element sendTo=getRootElement()], string name, element sourceElement, [arguments...] )

If you need a full example, you need to tell me more info about the script, how it should work, what it should do and so on.

Link to comment
Just now, Ben_Sherman said:

 

If you're trying to run the timer for a client-side function, you can't add it server-side. The timer itself can't be cross executed like that, however, what you can do triggering it via a trigger


bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] ) 
bool addEventHandler ( string eventName, element attachedTo, function handlerFunction, [ bool getPropagated = true, string priority = "normal" ] )    
bool triggerClientEvent ( [table/element sendTo=getRootElement()], string name, element sourceElement, [arguments...] )

If you need a full example, you need to tell me more info about the script, how it should work, what it should do and so on.

Thanks .. that was helpful .. the timer should be in Client Side ... Because only Client side accept DX functions .. 

i will give you an example ( photo ) 

Spoiler

69uCXMX3S2WBk5SOIeqXGg.png

Thanks <3 :) 

Link to comment
16 minutes ago, Legend<3 said:

Thanks .. that was helpful .. the timer should be in Client Side ... Because only Client side accept DX functions .. 

i will give you an example ( photo ) 

  Hide contents

69uCXMX3S2WBk5SOIeqXGg.png

Thanks <3 :) 

 

Haven't tested it to see if it works, but the general concept should give you some clues on how it's supposed to be done, hope you understand the overall concept. The rest is up to you, enjoy!

-- Server Side
addEvent("onBotWasted",true)
addEventHandler("onBotWasted",getRootElement(),
function (killer)
	if (source == nemesilv) then
		destroyElement(BlipNemesislv)
		-- You pick one that suits you the best
		triggerClientEvent(killer, "createBag", killer) -- Trigger is sent to ONLY the killer
		triggerClientEvent(getRootElement(), "createBag", killer) -- Trigger is sent to ALL players
    end
end)


-- Client Side
function createBag()
	local minute, seconds = 20, 60
	setTimer ( function()
		-- Your code, remember instead of making it count one time after 20 minutes this section here is executed every second for 20 minutes
		-- One example would be using the GUI that you entered as example
		
		if seconds > 0 then 
			seconds = seconds - 1 
		else 
			seconds = 60 
			minute = minute - 1
		end
		
		if minute == 0 and seconds == 0 then
			-- What to trigger when timer hits 0
		end
		
		--Updates the GUI text
		guiSetText(guiElement, ("%02d"):format(tonumber(minute))..":"..("%02d"):format(tonumber(seconds)))
	end, 1000, 60*20 )
end
addEvent("createBag", true)
addEventHandler("createBag", resourceRoot, createBag)

 

Link to comment
  • Administrators

FYI; you should create a serverside timer when you trigger the createBag event, that serverside timer will be used to create a new bag (or whatever you want to do when 20 minutes is up) as it is more consistent. The client shouldn't handle that.

Edited by LopSided_
Link to comment
26 minutes ago, Ben_Sherman said:

 

Haven't tested it to see if it works, but the general concept should give you some clues on how it's supposed to be done, hope you understand the overall concept. The rest is up to you, enjoy!


-- Server Side
addEvent("onBotWasted",true)
addEventHandler("onBotWasted",getRootElement(),
function (killer)
	if (source == nemesilv) then
		destroyElement(BlipNemesislv)
		-- You pick one that suits you the best
		triggerClientEvent(killer, "createBag", killer) -- Trigger is sent to ONLY the killer
		triggerClientEvent(getRootElement(), "createBag", killer) -- Trigger is sent to ALL players
    end
end)


-- Client Side
function createBag()
	local minute, seconds = 20, 60
	setTimer ( function()
		-- Your code, remember instead of making it count one time after 20 minutes this section here is executed every second for 20 minutes
		-- One example would be using the GUI that you entered as example
		
		if seconds > 0 then 
			seconds = seconds - 1 
		else 
			seconds = 60 
			minute = minute - 1
		end
		
		if minute == 0 and seconds == 0 then
			-- What to trigger when timer hits 0
		end
		
		--Updates the GUI text
		guiSetText(guiElement, ("%02d"):format(tonumber(minute))..":"..("%02d"):format(tonumber(seconds)))
	end, 1000, 60*20 )
end
addEvent("createBag", true)
addEventHandler("createBag", resourceRoot, createBag)

 

Thanks .. that did not work actually :/ .. :S 

 

8 minutes ago, LopSided_ said:

FYI; you should create a serverside timer when you trigger the createBag event, that serverside timer will be used to detect when the timer hits 0 (as seen on line 28 of bens example) as it is more consistent. The client shouldn't handle that.

Thanks <3 .. i noticed that .. 

maybe triggerServerEvent will solv that .. or two timers ... one to start next event ( will be in server side . ) the other to start DX timer .. 
------- *

:S 

Link to comment
  • Administrators

I made something simple for you, play around with it and it should help you to understand :)

Server:

local bag = {
	locations = { --Store your bag locations here
		{x = 1547.68, y = -1348.78, z = 329.46}, --Tallest building
		{x = 2495.12, y = -1689.36, z = 14.38} --CJs house
	}
}

function bag.spawn(x,y,z)
	x,y,z = x or 0, y or 0, z or 0
	bag.pickup = createPickup( x, y, z, 3, 1550 )
	setElementCollisionsEnabled( bag.pickup, false )
end

function bag.init(minutes) --bag.create(int minutes)
	if not minutes or isTimer(bag.timer) or isElement(bag.pickup) then return false end --If there is currently a timer, or a bag spawned on the map, then we can't create a new timer/bag
	bag.timer = setTimer (function()
		local loc = bag.locations
		local i = math.random(1,#loc) --Pick a random bag location from bag.locations
		bag.spawn(loc[i].x,loc[i].y,loc[i].z) --Spawns the bag. Here we're using a random bag location but you can just define your own x,y,z positions. Random location is optional
	end, (60*1000*minutes), 1)
	triggerClientEvent(getRootElement(), "createBagTimer", getRootElement(), minutes) --Setup new bag timer for all clients
end

addEvent("onBotWasted",true)
addEventHandler("onBotWasted", root,
function (killer)
	if (source == nemesilv) then
		destroyElement(BlipNemesislv)
		bag.init(20) --Makes a bag spawn after 20 minutes and sets everything up.
      end
end)

addEventHandler("onPickupHit", root, --A player picked up the bag
function(player)
	destroyElement(bag.pickup) --Destroy bag pickup
	local name = getPlayerName(player)
	outputChatBox(name.." picked up the bag!")
	-- do whatever you want here
end)

 

Client:

local sX,sY = guiGetScreenSize()
local timerW, timerH = 150,100
local timerX, timerY = (sX / 2) - (timerW / 2), (sY*0.9) - (timerH / 2) --Position of the timer on screen
local bag = {}


function bag.dx()
	if not isTimer(bag.timer) then removeEventHandler("onClientRender", root, bag.dx); return end
	local clock = convertToClock(bag.timer) --Get the timer remaining in clock format
	dxDrawText("Next Bag In: "..clock, timerX, timerY, timerX+timerW, timerY+timerH, tocolor(255,0,0,225), 2, "default-bold", "center", "center")
end


function convertToClock(theTimer) --Returns timer in a clock format (e.g: "19:57"), returns false if timer doesn't exist
	if not isTimer(theTimer) then return false end
	local remaining = getTimerDetails(theTimer) -- Get the timers remaining (ms)
	local s = remaining/1000 --seconds from ms
	return string.format("%.2d:%.2d", s/60%60, s%60) --convert to clock format and return
end

function bag.setupTimer(minutes)
	bag.timer = setTimer(function()
		bag.timer = nil
	end, (60*1000*minutes), 1)	
	addEventHandler("onClientRender", root, bag.dx) --Start drawing the timer on the clients screen
end
addEvent("createBagTimer", true)
addEventHandler("createBagTimer", root, bag.setupTimer)

 

Any questions, shoot!

Edited by LopSided_
  • Thanks 1
Link to comment
3 hours ago, LopSided_ said:

I made something simple for you, play around with it and it should help you to understand :)

Server:


local bag = {
	locations = { --Store your bag locations here
		{x = 1547.68, y = -1348.78, z = 329.46}, --Tallest building
		{x = 2495.12, y = -1689.36, z = 14.38} --CJs house
	}
}

function bag.spawn(x,y,z)
	x,y,z = x or 0, y or 0, z or 0
	bag.pickup = createPickup( x, y, z, 3, 1550 )
	setElementCollisionsEnabled( bag.pickup, false )
end

function bag.init(minutes) --bag.create(int minutes)
	if not minutes or isTimer(bag.timer) or isElement(bag.pickup) then return false end --If there is currently a timer, or a bag spawned on the map, then we can't create a new timer/bag
	bag.timer = setTimer (function()
		local loc = bag.locations
		local i = math.random(1,#loc) --Pick a random bag location from bag.locations
		bag.spawn(loc[i].x,loc[i].y,loc[i].z) --Spawns the bag. Here we're using a random bag location but you can just define your own x,y,z positions. Random location is optional
	end, (60*1000*minutes), 1)
	triggerClientEvent(getRootElement(), "createBagTimer", getRootElement(), minutes) --Setup new bag timer for all clients
end

addEvent("onBotWasted",true)
addEventHandler("onBotWasted", root,
function (killer)
	if (source == nemesilv) then
		destroyElement(BlipNemesislv)
		bag.init(20) --Makes a bag spawn after 20 minutes and sets everything up.
      end
end)

addEventHandler("onPickupHit", root, --A player picked up the bag
function(player)
	destroyElement(bag.pickup) --Destroy bag pickup
	local name = getPlayerName(player)
	outputChatBox(name.." picked up the bag!")
	-- do whatever you want here
end)

 

Client:


local sX,sY = guiGetScreenSize()
local timerW, timerH = 150,100
local timerX, timerY = (sX / 2) - (timerW / 2), (sY*0.9) - (timerH / 2) --Position of the timer on screen
local bag = {}


function bag.dx()
	if not isTimer(bag.timer) then removeEventHandler("onClientRender", root, bag.dx); return end
	local clock = convertToClock(bag.timer) --Get the timer remaining in clock format
	dxDrawText("Next Bag In: "..clock, timerX, timerY, timerX+timerW, timerY+timerH, tocolor(255,0,0,225), 2, "default-bold", "center", "center")
end


function convertToClock(theTimer) --Returns timer in a clock format (e.g: "19:57"), returns false if timer doesn't exist
	if not isTimer(theTimer) then return false end
	local remaining = getTimerDetails(theTimer) -- Get the timers remaining (ms)
	local s = remaining/1000 --seconds from ms
	return string.format("%.2d:%.2d", s/60%60, s%60) --convert to clock format and return
end

function bag.setupTimer(minutes)
	bag.timer = setTimer(function()
		bag.timer = nil
	end, (60*1000*minutes), 1)	
	addEventHandler("onClientRender", root, bag.dx) --Start drawing the timer on the clients screen
end
addEvent("createBagTimer", true)
addEventHandler("createBagTimer", root, bag.setupTimer)

 

Any questions, shoot!

This is Awsome .. Actually i used only Client side .. and change line 24 from minutes to 20 

in server side i used only 

triggerClientEvent(getRootElement(), "createBagTimer", getRootElement(), minutes)

Thank You A lot Bro <3 :):D 

 

-- SOLVED 

  • Thanks 1
Link to comment
  • Administrators

No problem. By the way, the better way would be to leave line 24 untouched - "(60*1000*minutes)" - and specify the minutes here instead:

triggerClientEvent(getRootElement(), "createBagTimer", getRootElement(), 20)

 

Link to comment
8 minutes ago, LopSided_ said:

No problem. By the way, the better way would be to leave line 24 untouched - "(60*1000*minutes)" - and specify the minutes here instead:


triggerClientEvent(getRootElement(), "createBagTimer", getRootElement(), 20)

 

Thanks You Helped me a lot :) <3 :$ 

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