Jump to content

Server variable on client side not becoming global


Bilal135

Recommended Posts

I created a ped in server side (variable name: tarek), and exported it to client side via triggers, because i wanted to make a name tag specifically for this ped. I was able to successfully export the variable to client side, but the problem is, that variable is only recognized within that function and outside that function it isn't being recognized. I tried to make it global but it didn't work. Please see the code below. Any help would be appreciated.

 

-- server

addEvent("serverCallback", true)
function getClientTarekElement_func_s()
    local playerAsking = source
    local s_tarek = tarek
    triggerClientEvent(playerAsking, "getClientTarekElement", playerAsking, s_tarek)
    end
addEventHandler("serverCallback", root, getClientTarekElement_func_s)

-- client

addEvent("getClientTarekElement", true)
function getClientTarekElement_func(s_tarek)
    c_tarek = s_tarek
end
addEventHandler("getClientTarekElement", root, getClientTarekElement_func)

addEventHandler("onClientResourceStart", resourceRoot, function()
    triggerEvent("s_callback", localPlayer)
end)

addEvent("s_callback", true)
addEventHandler("s_callback", root, function()
    triggerServerEvent("serverCallback", localPlayer, s_tarek)
end)

-- nametag for ped

addEventHandler( "onClientRender",root,
function( )
    local px, py, pz, tx, ty, tz, dist
    px, py, pz = getCameraMatrix( )
    tx, ty, tz = getElementPosition( c_tarek )
    dist = math.sqrt( ( px - tx ) ^ 2 + ( py - ty ) ^ 2 + ( pz - tz ) ^ 2 )
    if dist < 30.0 then
    if isLineOfSightClear( px, py, pz, tx, ty, tz, true, false, false, true, false, false, false,localPlayer ) then
    local sx, sy, sz = getPedBonePosition( c_tarek, 5 )
    local x,y = getScreenFromWorldPosition( sx, sy, sz + 0.4 )
    if x then
    dxDrawText( "Tarek", x, y, x, y, tocolor(255, 188, 116), 1.0 + ( 15 - dist ) * 0.02, "clear-normal", "center", "center" )
end
end
end
end)

 

Link to comment
  • Moderators

First of all, where you define tarek at server side?

I think you didn't send the variable to the client side, because s_tarek in triggerClientEvent is nil.

 

Here is your fixed code:

-- SERVER 

local tarek = createPed(0, 0, 0, 3)

addEvent("serverCallback", true)
function getClientTarekElement_func_s()
	local playerAsking = source
	local s_tarek = tarek
	triggerClientEvent(playerAsking, "getClientTarekElement", playerAsking, s_tarek)
end
addEventHandler("serverCallback", root, getClientTarekElement_func_s)


-- CLIENT

addEvent("getClientTarekElement", true)
function getClientTarekElement_func(s_tarek)
    local c_tarek = s_tarek
	
	-- start render nametag for ped AFTER you received s_tarek from server side!
	addEventHandler( "onClientRender",root,
	function( )
		local px, py, pz, tx, ty, tz, dist
		px, py, pz = getCameraMatrix( )
		tx, ty, tz = getElementPosition( c_tarek )
		dist = math.sqrt( ( px - tx ) ^ 2 + ( py - ty ) ^ 2 + ( pz - tz ) ^ 2 )
		if dist < 30.0 then
			if isLineOfSightClear( px, py, pz, tx, ty, tz, true, false, false, true, false, false, false,localPlayer ) then
				local sx, sy, sz = getPedBonePosition( c_tarek, 5 )
				local x,y = getScreenFromWorldPosition( sx, sy, sz + 0.4 )
				if x then
					dxDrawText( "Tarek", x, y, x, y, tocolor(255, 188, 116), 1.0 + ( 15 - dist ) * 0.02, "clear-normal", "center", "center" )
				end
			end
		end
	end)
end
addEventHandler("getClientTarekElement", root, getClientTarekElement_func)

addEventHandler("onClientResourceStart", resourceRoot, function()
    triggerEvent("s_callback", localPlayer)
end)

addEvent("s_callback", true)
addEventHandler("s_callback", root, function()
    triggerServerEvent("serverCallback", localPlayer, s_tarek)
end)

 

 

But here is a much simpler solution:

 

-- SERVER

local ped = createPed(0, 0, 0, 3)
setElementData(ped, "custom_name", "Tarek")


-- CLIENT

addEventHandler("onClientHUDRender", root, function()
	local px, py, pz = getCameraMatrix()
	
	for _, ped_element in ipairs(getElementsByType("ped", root, true)) do -- get all synced ped
		local customname = getElementData(ped_element, "custom_name")
		
		if customname then
			local tx, ty, tz = getElementPosition(ped_element)
			
			if isLineOfSightClear(px, py, pz, tx, ty, tz, true, false, false, true, false, false, false, localPlayer) then
				local dist = math.sqrt((px - tx)^2 + (py - ty)^2 + (pz - tz)^ 2)
				
				if dist < 30 then
					local headx, heady, headz = getPedBonePosition(ped_element, 5)
					local sx, sy = getScreenFromWorldPosition(headx, heady, headz + 0.4)
					
					if sx and sy then
						dxDrawText(customname, sx, sy, sx, sy, tocolor(255, 188, 116), 1.0 + (15 - dist)*0.02, "clear-normal", "center", "center")
					end
				end
			end
		end
	end
end)

 

Link to comment
  • Moderators
26 minutes ago, Lynch said:

that variable is only recognized within that function and outside that function it isn't being recognized

You are using the variable in onClientRender, before it even has a value assigned to it.

 

Link to comment

Thank you @stPatrick. The first method did work, but the method you said was much more simpler, did not work. No errors in debug.

Thanks @IIYAMA. But I thought I had assigned a value to it here,

addEvent("getClientTarekElement", true)
function getClientTarekElement_func(s_tarek)
    c_tarek = s_tarek
end
addEventHandler("getClientTarekElement", root, getClientTarekElement_func)

I still don't understand why c_tarek would not be recognised in any other function.

Link to comment
  • Moderators
17 minutes ago, Lynch said:

Thank you @stPatrick. The first method did work, but the method you said was much more simpler, did not work. No errors in debug.

Thanks @IIYAMA. But I thought I had assigned a value to it here,


addEvent("getClientTarekElement", true)
function getClientTarekElement_func(s_tarek)
    c_tarek = s_tarek
end
addEventHandler("getClientTarekElement", root, getClientTarekElement_func)

I still don't understand why c_tarek would not be recognised in any other function.

It has to work.

Link to comment
  • Moderators
1 hour ago, Lynch said:

I still don't understand why c_tarek would not be recognised in any other function.

I think the problem lies somewhere else. Try to debug your code with debug functions.

 

Also check this:

Clientside:

addEvent("s_callback", true)

addEventHandler("s_callback", root, function()

         triggerServerEvent("serverCallback", localPlayer, s_tarek) -- ????

end)

 

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