Jump to content

How to get element from server side?


Recommended Posts

Posted

The question is the following needs to get an item from the server side to the client side, how can I do it? (Elements such as markers, vehicles etc.)

Posted (edited)

Can I send it using trigger client event? e.g. the variable responsible for the vehicle? and if so how can I do it so that I can understand how it works ...

Edited by redditing
  • Like 1
  • Moderators
Posted (edited)
19 minutes ago, redditing said:

Can I send it using trigger client event? e.g. the variable responsible for the vehicle? and if so how can I do it so that I can understand how it works ...

All serverside elements are already available on clientside. The only problem is: "how to get the right one?" There are multi ways to get those, including triggerClientEvent as @Patrick said.

 

Server

local receiver

receiver = getRandomPlayer() -- a random player
receiver = root -- all players

triggerClientEvent(receiver, "eventName_SEND_THING_OVER", resourceRoot, element)

 

Client

addEvent("eventName_SEND_THING_OVER", true) -- enable the event: string eventName, boolean true = able to receive events from serverside


addEventHandler("eventName_SEND_THING_OVER", resourceRoot, -- add a handler, that will call the function when the event is triggered
function (element) 
  
end, false) -- disable propagation

 


 

There is also a method which I like to use to keep a dynamic list of my elements:

 

 

Server

local collectionElement = createElement("collection", "myVehicles")

for i=1, 10 do
	local vehicle = createVehicle(432, i * 10, 0, 50)
	setElementParent(vehicle, collectionElement) -- (Note: setElementParent, doesn't work across resources)
end

 

Client

function getMyVehicles (theType)
	local collectionElement = getElementByID("myVehicles")
	if collectionElement then
		return getElementChildren(collectionElement, theType) -- theType is optional, could be "vehicle"
	end
	return false
end

 

 

 

 

 

 

 

 

Edited by IIYAMA
  • Like 2
Posted
8 minutes ago, IIYAMA said:

All serverside elements are already available on clientside. The only problem is: "how to get the right one?" There are multi ways to get those, including triggerClientEvent as @Patrick said.

 

Server


local receiver

receiver = getRandomPlayer() -- a random player
receiver = root -- all players

triggerClientEvent(receiver, "eventName_SEND_THING_OVER", resourceRoot, element)

 

Client


addEvent("eventName_SEND_THING_OVER", true) -- enable the event: string eventName, boolean true = able to receive events from serverside


addEventHandler("eventName_SEND_THING_OVER", resourceRoot, -- add a handler, that will call the function when the event is triggered
function (element) 
  
end, false) -- disable propagation

 


 

There is also a method which I like to use to keep a dynamic list of my elements:

 

 

Server


local collectionElement = createElement("collection", "myVehicles")

for i=1, 10 do
	local vehicle = createVehicle(432, i * 10, 0, 50)
	setElementParent(vehicle, collectionElement) -- (Note: setElementParent, doesn't work across resources)
end

 

Client


function getMyVehicles (theType)
	local collectionElement = getElementByID("myVehicles")
	if collectionElement then
		return getElementChildren(collectionElement, theType) -- theType is optional, could be "vehicle"
	end
	return false
end

 

 

 

 

 

 

 

 

I need to send the element to the client, because I want to write some text "dxDrawTextOnElement" on this element, that's why I want to know what I need to do to return the element from the server to the client

  • Moderators
Posted
1 minute ago, redditing said:

I need to send the element to the client, because I want to write some text "dxDrawTextOnElement" on this element, that's why I want to know what I need to do to return the element from the server to the client

  • If it is a player, use Patrick his method.
  • If it is an object/ped/vehicle created by a single resource, stick with the second method.

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