Jump to content

GetElementData server/client


Recommended Posts

I would like to know how I could get ElementData from server to client and vice versa

 

-- server side
marker = createMarker(0, 0, 1.5, "cylinder", 2, 255, 0, 0)

setElementData(marker, "Item", 5)

addEvent("GetItem", true)

addEventHandler("GetItem", root, function()
  	ClientMarker = marker
    
    ItemOfClientMarker = getElementData(marker, "Item")
  end)

addEventHandler("onMarkerHit", marker, function(hp)
    triggerClientEvent("Element", resourceRoot)
    
  	setElementData(hp, "ItemTwo", ElementVariable+getElementData(marker, "Item"))
  end)

-- client side
function dxDrawTextOnElement(TheElement,text,height,distance,R,G,B,alpha,size,font,...)
	local x, y, z = getElementPosition(TheElement)
	local x2, y2, z2 = getCameraMatrix()
	local distance = distance or 20
	local height = height or 1

	if (isLineOfSightClear(x, y, z+2, x2, y2, z2, ...)) then
		local sx, sy = getScreenFromWorldPosition(x, y, z+height)
		if(sx) and (sy) then
			local distanceBetweenPoints = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
			if(distanceBetweenPoints < distance) then
				dxDrawText(text, sx+2, sy+2, sx, sy, tocolor(R or 255, G or 255, B or 255, alpha or 255), (size or 1)-(distanceBetweenPoints / distance), font or "arial", "center", "center")
			end
		end
	end
end

setElementData(getLocalPlayer(), "ItemTwo", 0)

addEvent("Element", true)

addEventHandler("Element", root, function()
  	ElementVariable = getElementData(getLocalPlayer() , "ItemTwo")
  end)

addEventHandler("onClientRender", root, function()
    triggerServerEvent("GetItem", root)
    
	dxDrawTextOnElement(ClientMarker, tostring(ItemOfClientMarker), 2, 50, 255, 255, 0, 3, "arial")
  end)

Does it have a chance to work? because that's something I imagine...

Link to comment
-- those functions are shared so you can set and get data from server or client to other 
setElementData 
getElementData 

-- about trigger server from client and client from server 
-- i see that the example in wiki is very easy and enough to teach anyone 
triggerServerEvent 
triggerClientEvent 

 

Link to comment
1 minute ago, Sisqo0 said:

-- those functions are shared so you can set and get data from server or client to other 
setElementData 
getElementData 

-- about trigger server from client and client from server 
-- i see that the example in wiki is very easy and enough to teach anyone 
triggerServerEvent 
triggerClientEvent 

 

I only know about this, when I try to get an item from the server to the client or vice versa, getElementData () returns false

Link to comment

i see that you are trying to receive information from client by triggerClientEvent and you did it too in server. triggerServerEvent or triggerClientEvent are used to pass information or give an order not to receive. i will fix your code but you should learn from it not just copy and paste. 

-- server side
marker = createMarker(0, 0, 1.5, "cylinder", 2, 255, 0, 0)
setElementData(marker, "Item", 5)

addEvent("GetItem", true)
addEventHandler("GetItem", root, function() 
      local ItemOfClientMarker = getElementData(marker, "Item")
      triggerClientEvent("DrawText", resourceRoot, marker, tostring(ItemOfClientMarker), 2, 50, 255, 255, 0, 3, "arial")-- i pass info from server to event 'DrawText' in client [ marker, tostring.., "arial") 
end)

addEventHandler("onMarkerHit", marker, function(hp)
local ElementVariable = ( getElementData(hp, "ItemTwo") or 0) --getplayer 'itemtwo' or get "0" if he doesn't have "ItemTwo" data
setElementData(hp, "ItemTwo", ElementVariable+getElementData(marker, "Item"))
end)

-- client side
function dxDrawTextOnElement(TheElement,text,height,distance,R,G,B,alpha,size,font,...)
local x, y, z = getElementPosition(TheElement)
local x2, y2, z2 = getCameraMatrix()
local distance = distance or 20
local height = height or 1

if (isLineOfSightClear(x, y, z+2, x2, y2, z2, ...)) then
local sx, sy = getScreenFromWorldPosition(x, y, z+height)
if(sx) and (sy) then
local distanceBetweenPoints = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
if(distanceBetweenPoints < distance) then
dxDrawText(text, sx+2, sy+2, sx, sy, tocolor(R or 255, G or 255, B or 255, alpha or 255), (size or 1)-(distanceBetweenPoints / distance), font or "arial", "center", "center")
end
end
end
end
addEvent("DrawText", true)
addEventHandler("DrawText", root, dxDrawTextOnElement) 


addEventHandler("onClientRender", root, function()
triggerServerEvent("GetItem", root)--give an order to make this event "getItem" run in server per frame
end)

 

Edited by Sisqo0
  • Thanks 1
Link to comment
  • Scripting Moderators
17 hours ago, redditing said:

I would like to know how I could get ElementData from server to client and vice versa

 


-- server side
marker = createMarker(0, 0, 1.5, "cylinder", 2, 255, 0, 0)

setElementData(marker, "Item", 5)

addEvent("GetItem", true)

addEventHandler("GetItem", root, function()
  	ClientMarker = marker
    
    ItemOfClientMarker = getElementData(marker, "Item")
  end)

addEventHandler("onMarkerHit", marker, function(hp)
    triggerClientEvent("Element", resourceRoot)
    
  	setElementData(hp, "ItemTwo", ElementVariable+getElementData(marker, "Item"))
  end)

-- client side
function dxDrawTextOnElement(TheElement,text,height,distance,R,G,B,alpha,size,font,...)
	local x, y, z = getElementPosition(TheElement)
	local x2, y2, z2 = getCameraMatrix()
	local distance = distance or 20
	local height = height or 1

	if (isLineOfSightClear(x, y, z+2, x2, y2, z2, ...)) then
		local sx, sy = getScreenFromWorldPosition(x, y, z+height)
		if(sx) and (sy) then
			local distanceBetweenPoints = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
			if(distanceBetweenPoints < distance) then
				dxDrawText(text, sx+2, sy+2, sx, sy, tocolor(R or 255, G or 255, B or 255, alpha or 255), (size or 1)-(distanceBetweenPoints / distance), font or "arial", "center", "center")
			end
		end
	end
end

setElementData(getLocalPlayer(), "ItemTwo", 0)

addEvent("Element", true)

addEventHandler("Element", root, function()
  	ElementVariable = getElementData(getLocalPlayer() , "ItemTwo")
  end)

addEventHandler("onClientRender", root, function()
    triggerServerEvent("GetItem", root)
    
	dxDrawTextOnElement(ClientMarker, tostring(ItemOfClientMarker), 2, 50, 255, 255, 0, 3, "arial")
  end)

Does it have a chance to work? because that's something I imagine...

You should fix it, before your server will die out of endless triggering.

addEventHandler("onClientRender", root, function()
triggerServerEvent("GetItem", root)--give an order to make this event "getItem" run in server per frame
end)

Trigger client/server even should be well maintained. Right now, you call this function every frame, what if more players will join?

Link to comment
1 hour ago, majqq said:

You should fix it, before your server will die out of endless triggering.


addEventHandler("onClientRender", root, function()
triggerServerEvent("GetItem", root)--give an order to make this event "getItem" run in server per frame
end)

Trigger client/server even should be well maintained. Right now, you call this function every frame, what if more players will join?

I dont use it bcs i know that broken script, but how i can return marker from server side to client side?

 

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