[Solved] Returning values from serverevent?


#1

Hi there, I have an issue with returning what i need.
I don’t know, that in the hell is wrong with this.

Every time it returns some random numbers.
While server side it prints right.

RegisterServerEvent('gpdata')
AddEventHandler('gpdata',function(data)
	TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
	xPlayer = ESX.GetPlayerFromId(source)
	
	local rtrn
	
	if (data == 'money') then
		rtrn = 'lol'
		print(rtrn)
	end 
	
	return rtrn
end)

client

function callbackExemple(data)
  Citizen.CreateThread(function()
  
	if (data['text'] == "nowanted") then
		
		local money = TriggerServerEvent('gpdata','money')
		print(money)
		
	
	end

  end)
end

#2

Now that i have figured out how to use triggerClientEvent… I still need to figure out how to set global variable.
I have pdata = 0 at start of the file.

Later i set it to 20, but still, it remains 0… How do i set it?


#3

Nevermind. I got it working.


#4

I would post your solution here so others can see it if they need it :slight_smile:


#5

Good Idea indeed :).

My problem was, that i couldn’t get triggers working.
If you want TriggerClientEvent to work, you need to add ‘source’, or player ID after event name, then specify event data. I did not write source or ID there, so that was the problem.
Also Triggers does not return data, so you need to trigger event which sets your variable right. Keep in mind that it must be accessible (global).

Client

function sd(data)
	_pdata = data
end

RegisterNetEvent('MENU:setData')
AddEventHandler('MENU:setData', function(data)
	Citizen.CreateThread(function()
		sd(data)
	end)
end)

Server:

RegisterServerEvent('gpdata')
AddEventHandler('gpdata',function(data,data2)
	TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
	xPlayer = ESX.GetPlayerFromId(source)
	
	if (data == 'money') then
		local money = xPlayer.getMoney()
		TriggerClientEvent('MENU:setData',source,money)
	end 
	
	if (data == 'removeMoney') then
		xPlayer.removeMoney(data2)
	end
	
	
end)

Used for menu made with ft_menuBuilder.