Jump to content

Send data from server to client?


Recommended Posts

SERVER:

  
triggerClientEvent(player1, "send_Data", root, "fooPlayer0", fooPlayer0) 
  

CLIENT:

  
  
function recData(varname, value) 
    _G[varname] = value 
   outputChatbox("fooPlayer1 value is "..fooPlayer1.." ! Cool, right :D") 
end 
addEvent("send_Data", true) 
addEventHandler("send_Data", root, recData) 
  

Probably wouuld work.

Link to comment

When your sending data to server or to client, add the thing you want to send at the end of triggerServerEvent or triggerClientEvent, and when it is triggered you have to add the thing you want in the (IN HERE) by the function.

EXAMPLE:

  
-- Server-side 
function pedVehicle() 
  vehicle = getPedOccupiedVehicle(client) 
end 
triggerClientEvent(client, "theVehicle", getRootElement(), vehicle) 
  
--Client-side 
addEvent("theVehicle",true) 
addEventHandler("theVehicle", getRootElement(),  
function(vehicle) 
 outputChatBox("THESE IS A"..vehicle.. ".") 
end) 
  

Edited by Guest
Link to comment
and he wanted to make a variable of the same name and enable a variable on that side with the same name

he can edit very simply.

Not exactly. I want to send the CONTENTS of a server side variable to a specific client. It doesn't have anything to do with the variables name on either side. I simply just want to send the contents of a variable to a specific client.

A better explanation:

Player 1 on the server has 5 credits.

Player 2 needs to know how many credits Player 1 has for whatever reason.

Link to comment
--client side 
  
function getPlayerCredits(cmd, who) 
local player = getPlayerFromName(who) 
if not player then outputChatBox("Player not found.",255,0,0) return end 
triggerServerEvent("getPlayerCredits",getLocalPlayer(),player) 
end 
addCommandHandler("getc",getPlayerCredits) 
  
--server side 
  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (player) 
outputChatBox(getPlayerName(player) .." has 50000 credits.",root,0,255,0) 
end) 

So for example you type: /getc [sANL]Castillo and it should output that text (if you wrote the name right of course).

Link to comment
--client side 
  
function getPlayerCredits(cmd, who) 
local player = getPlayerFromName(who) 
if not player then outputChatBox("Player not found.",255,0,0) return end 
triggerServerEvent("getPlayerCredits",getLocalPlayer(),player) 
end 
addCommandHandler("getc",getPlayerCredits) 
  
--server side 
  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (player) 
outputChatBox(getPlayerName(player) .." has 50000 credits.",root,0,255,0) 
end) 

So for example you type: /getc [sANL]Castillo and it should output that text (if you wrote the name right of course).

Hei castillo could you explain me something , on that function u write?

On server side function couldnt u receive the source of the event that would be that exactly player ?

I mean u could use source variable instead of using the player one, right?

I say this because u trigger the server event "getPlayerCredits" for the element getLocalPlayer(), so the source will be this local player right?

And u could have used it, like this:

  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (source) 
outputChatBox(getPlayerName(source) .." has 50000 credits.",root,0,255,0) 
end) 

Right bro? just wanna be clear, so I can understand Lua more :)

Thanks!!

Link to comment
--client side 
  
function getPlayerCredits(cmd, who) 
local player = getPlayerFromName(who) 
if not player then outputChatBox("Player not found.",255,0,0) return end 
triggerServerEvent("getPlayerCredits",getLocalPlayer(),player) 
end 
addCommandHandler("getc",getPlayerCredits) 
  
--server side 
  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (player) 
outputChatBox(getPlayerName(player) .." has 50000 credits.",root,0,255,0) 
end) 

So for example you type: /getc [sANL]Castillo and it should output that text (if you wrote the name right of course).

Hei castillo could you explain me something , on that function u write?

On server side function couldnt u receive the source of the event that would be that exactly player ?

I mean u could use source variable instead of using the player one, right?

I say this because u trigger the server event "getPlayerCredits" for the element getLocalPlayer(), so the source will be this local player right?

And u could have used it, like this:

  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (source) 
outputChatBox(getPlayerName(source) .." has 50000 credits.",root,0,255,0) 
end) 

Right bro? just wanna be clear, so I can understand Lua more :)

Thanks!!

Well, here's an small explanation:

source = the player who used the command (getLocalPlayer(), in this case).

If you trigger getLocalPlayer() twice, it will send a player element that can be used in your function, ex: function o_o(player) player will be the one who triggered it, if you didn't pass it twice, source will work as that player.

(Sorry, I'm not good explaining things :oops: .)

Link to comment

Triggering getLocalPlayer() twice:

triggerServerEvent("getPlayerCredits", getLocalPlayer(), getLocalPlayer()) 
  
-- in this way server side should be something like 
addEvent("getPlayerCredits", true) 
addEventHandler("getPlayerCredits", root, 
function(player) 
--here player == source because getLocalPlayer() is triggered twice 

And let's say:

--client side 
  
function getPlayerCredits(cmd, who) 
local player = getPlayerFromName(who) 
if not player then outputChatBox("Player not found.",255,0,0) return end 
triggerServerEvent("getPlayerCredits",getLocalPlayer(),player) 
end 
addCommandHandler("getc",getPlayerCredits) 
  
--server side 
  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (player) 
outputChatBox(getPlayerName(player) .." has 50000 credits.",root,0,255,0) 
end) 

Here player ~= source because in triggerServerEvent

addCommandHandler("getc", 
function() 
triggerServerEvent("getPlayerCredits", getLocalPlayer() --[[ the source server side]], --[[ more possible params like:]] getPlayerFromName("DarkLink")) 
end 

Server side you will have

addEvent("getPlayerCredits", true) 
addEventHandler("getPlayerCredits", root, 
function(DarkLink) 
outputChatBox("DarkLink has 50000 credits.",root,0,255,0) 
end) 

Server side:

"source" and that's the player who typed the /getc it's the getLocalPlayer() in triggerServerEvent

"DarkLink" and that's the getPlayerFromName in triggerServerEvent.

I'm not good in explaining either, but i thought this might help.

Link to comment

Thanks guys, but my question is, couldnt he just trigger the event like:

  
triggerServerEvent("getPlayerCredits", getLocalPlayer()) 
  

and then on server side, use:

  
addEvent("getPlayerCredits",true) 
addEventHandler("getPlayerCredits",root, 
function (source) 
outputChatBox(getPlayerName(source) .." has 50000 credits.",root,0,255,0) 
end) 
  

??

It would work too and no need of player variable?

Thanks!!

Link to comment

1. Your example is wrong because source will be already defined, so you can't put it in the function()

2. No, because /getc player he wants to check for another player using your example he will check for himself.

Link to comment
1. Your example is wrong because source will be already defined, so you can't put it in the function()

2. No, because /getc player he wants to check for another player using your example he will check for himself.

AHHH I got the 2º !! Because he want the "who" argument he type on /getc command !!! I GET IT! Thanks alot Jacob ;D

By the way, I didnt get the first, why cant I use ? the function called by the addEventHandler wont have that source ? the source from the trigger? I mean the getLocalPlayer() ?

Thanks alot, this is important to me :)

Link to comment
No problem.

What i mean, that it's wrong to do this: function(source) because source is always defined and you can't put it in function.

Hmm I see, so if I want to use the source of the trigger on the function that is called by the event handler, I cant? Need an extra argument for that source? right?

Thanks.

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