Jump to content

ClientEvent not added client-side error (but event is there)


koragg

Recommended Posts

Last night in 2 AM a brillian idea came to mind, why not make a script which displays to all players that join when a certain player has a birthday on screen with dxDrawText? :) I know that for that to work I need events as dxDrawText is a client function only. But even though I added the event to my client-side the server still doesn't see it. Can anyone help with this?

Client-Side

local screenW, screenH = guiGetScreenSize()
local px,py = 1600,900
local x,y =  (screenW/px), (screenH/py)

addEvent("drawBday", true)
function dxDrawBday(nick, age)
	dxDrawText(nick.." #FFFFFFturned "..age.." today!", screenW * 0.0000, screenH * 0.1648, screenW * 1.0000, screenH * 0.3861, tocolor(255, 255, 255, 255), 1*y, "bankgothic", "center", "center", false, false, false, true, false)
	if isEventHandlerAdded("onClientRender", root, dxDrawBday) == false then
		addEventHandler("onClientRender", root, dxDrawBday)
	end
	setTimer(stopDrawingBday,5000,1)
end
addEventHandler("drawBday", root, dxDrawBday)
-------------------------------------------------------------------------------------------------------------------------
function stopDrawingBday()
removeEventHandler("onClientRender", root, dxDrawBday)
end
-------------------------------------------------------------------------------------------------------------------------
function isEventHandlerAdded(sEventName, pElementAttachedTo, func)
 if type(sEventName) == 'string' and 
  isElement(pElementAttachedTo) and 
  type(func) == 'function' 
 then
  local aAttachedFunctions = getEventHandlers(sEventName, pElementAttachedTo)
  if type(aAttachedFunctions) == 'table' and #aAttachedFunctions > 0 then
   for i, v in ipairs(aAttachedFunctions) do
    if v == func then
     return true
    end
   end
  end
 end
 return false
end

Server-Side

local time = getRealTime()
local bday = false
local age = false
local nick = false

function BdayText()
	if time.monthday == 17 and time.month+1 == 12 then
		bday = { day = 17, month = 12, year = 1997 }
		age = time.year+1900 - bday.year
		nick = "#00FF00[SiK]#FEFE22Megas"
	end
	
	
	if time.monthday == bday.day and time.month+1 == bday.month then
		--outputChatBox(nick.." #FFFFFFturned "..age.." today!", source, 255, 255, 255, true)
		triggerClientEvent(source, "drawBday", source, nick, age)
	end
end
addEventHandler("onPlayerJoin", root, BdayText)

 

Link to comment
local screenW, screenH = guiGetScreenSize()
local px,py = 1600,900
local x,y =  (screenW/px), (screenH/py)

function dxDrawBday(nick, age)
	dxDrawText(nick.." #FFFFFFturned "..age.." today!", screenW * 0.0000, screenH * 0.1648, screenW * 1.0000, screenH * 0.3861, tocolor(255, 255, 255, 255), 1*y, "bankgothic", "center", "center", false, false, false, true, false)	
end

addEvent("drawBday", true)
addEventHandler("drawBday", root, function()
  addEventHandler("onClientRender", root, dxDrawBday)
  setTimer(function()
    removeEventHandler("onClientRender", root, dxDrawBday)
  end,5000,1)
end)

This is all needed client side (not tested)

Link to comment

I tried your code but still got the same "server triggered clientside event drawBday but event is not added clientside". I worked on it a bit and made everything be client side.

local screenW, screenH = guiGetScreenSize()
local px,py = 1600,900
local x,y =  (screenW/px), (screenH/py)
-------------------------------------------------------------------------------------------------------------------------
local time = getRealTime()
local bday = false
local age = false
local nick = false
-------------------------------------------------------------------------------------------------------------------------
function dxDrawBday()
	if time.monthday == bday.day and time.month+1 == bday.month then
		dxDrawText(nick:gsub('#%x%x%x%x%x%x', '').." turned "..age.." today!", screenW * 0.0000+1, screenH * 0.1648+1, screenW * 1.0000+1, screenH * 0.3861+1, tocolor(0, 0, 0, 255), 1*y, "bankgothic", "center", "center", false, false, false, false, false)
		dxDrawText(nick.." #FFFFFFturned "..age.." today!", screenW * 0.0000, screenH * 0.1648, screenW * 1.0000, screenH * 0.3861, tocolor(255, 255, 255, 255), 1*y, "bankgothic", "center", "center", false, false, false, true, false)
	end
	setTimer(function() removeEventHandler("onClientRender", root, dxDrawBday) end, 23000, 1)
end
addEventHandler("onClientRender", root, dxDrawBday)
-------------------------------------------------------------------------------------------------------------------------
function addDates()
	if time.monthday == 11 and time.month+1 == 2 then
		bday = { day = time.monthday, month = time.month+1, year = 1997 }
		age = time.year+1900 - bday.year
		nick = "#00FF00[SiK]#FEFE22Megas"
	else
		bday = { day = 0, month = 0, year = 0 }
		age = 0
		nick = "0"
	end
end
addEventHandler("onClientResourceStart", root, addDates)

But there was a time when I kept randomly losing my FPS (from 60 to 50, even to 42 once!) and I'm not sure if it's because of my code^ or just because I haven't restarted MTA for a long time (sometimes I lag because of this). When I restarted my server and my MTA it seems like it's fixed but I still wanted to ask, is anything in my code above maybe responsible for making those FPS drops or it's just because I haven't restarted my game after all those resource restarts and reconnects.

Link to comment

You're doing the same thing as before tho, onClientRender is every frame so dxDrawBday is called again and again so will setTimer be as well.

Don't put the setTimer inside the render you're creating multiple timers this could be the lagg problem

18 minutes ago, koragg said:

setTimer(function() removeEventHandler("onClientRender", root, dxDrawBday) end, 23000, 1)

 

18 minutes ago, koragg said:

addEventHandler("onClientRender", root, dxDrawBday)

 

  • Like 1
Link to comment

Well today there was no lag but I put the setTimer in the "addDates" function which is tied to the "onClientResourceStart" event handler just in case. This way I think the timer will get set just once, on client player join.

Edited by koragg
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...