Jump to content

Getting Global Tables


shaio

Recommended Posts

Posted

I made a table on server side global. I want to access that table on client side.. How do I do this? Please help me.

 

meta.xml -

<meta>
	<script src="server.lua" type="server"/>
	<script src="client.lua" type="client"/>
</meta>


server.lua - 

root = getRootElement()

rotators = {}
rotatorSet = createObject(13646,0,0,-10,0,0,0)
index = 0

addEventHandler("onResourceStart",root,function()
    setTimer(rotateSetObject,30000,0)
    setTimer(rotateSecond,30000,0)
end)

function rotateSetObject()
    local px,py,pz = getElementPosition(rotatorSet)
    local prx,pry,prz = getElementRotation(rotatorSet)
    moveObject(rotatorSet,30000,px,py,pz,0,0,prz+180)
end

function rotateFirst()
    for i,u in pairs(rotators) do
        local x,y,z = getElementPosition(u.object)
        local rx,ry,rz = getElementRotation(rotatorSet)
        moveObject(u.object,10,x,y,z,0,0,rz)
        setObjectScale(u.object,1.1)
        attachElements(u.vehicle,u.object,0,0,1.5,0,0,0)
    end
end

function rotateSecond()
    for i,v in pairs(rotators) do
        local sx,sy,sz = getElementPosition(v.object)
        local prx,pry,prz = getElementRotation(rotatorSet)
        moveObject(v.object,30000,sx,sy,sz,0,0,prz+180)
    end
end

function createRotator(player,cmd,id)
    if not isPedInVehicle(player) then
        local x,y,z = getElementPosition(player)
        table.insert(rotators,{id = index+1,object = createObject(13646,x,y,z-1.4,0,0,0),vehicle = createVehicle(id,x,y,z+2)})
        for i,v in pairs(rotators) do
            attachElements(v.vehicle,v.object,0,0,1.5,0,0,0)
            setObjectScale(v.object,1.1)
			setElementData(v.vehicle,"rotateCar",true)
        end
        setElementPosition(player,x,y,z+1.2)
		setTimer(rotateFirst,30000,1)
		index = index+1
    end
end
addCommandHandler("crotate",createRotator)

addEventHandler("onVehicleStartEnter",root,function(player,seat)
	if (getElementData(source,"rotateCar") == true) then
		cancelEvent()
		outputChatBox("You may not enter this vehicle!",player,255,0,0,true)
	end
end)

addCommandHandler("clearrotators",function()
	setTimer(function()
		for i,p in pairs(rotators) do
			destroyElement(p.vehicle)
			destroyElement(p.object)
		end
		for _,plr in ipairs(getElementsByType("player")) do
			outputChatBox("All rotators have been destroyed!",plr,255,0,0,true)
		end
	end,10000,1)
end)

function update(player,cmd)
	local rx,ry,rz = getElementRotation(rotatorSet)
	for i,c in ipairs(rotators) do
		setElementRotation(c.object,0,0,rz)
	end
end
addCommandHandler("updater",update)

 

client.lua -

localPlayer = getLocalPlayer()

function createText()
	for i,o in pairs(rotators) do
		local x,y,z = getElementPosition(o.vehicle)
		local sx,sy = getScreenFromWorldPosition(x,y,z+1)
		dxDrawText("ID: "..tostring(o.id),sx/2,sy/2,sx,sy,tocolor(255,255,255,255),1,"pricedown")
	end
end
 
function renderText()
    addEventHandler("onClientRender",root,createText)
end
addEventHandler("onClientResourceStart",root,renderText)

 

Posted

Global doesn't get shared across server/client. You'd need to send the data over to the client. Then each change to said table, sync to the client. The better question, why are you don't this? What's your end goal?

Posted
------------ server
function sendDataToClient()
  	triggerClientEvent(getRootElement(), "rotators.data", getRootElement(), rotators)
end
addEvent("rotators.send", true)
addEventHandler("roatators.send", getRootElement(), sendDataToClient)


------ client
rotators = {}
function recieveDataFromServer(data)
	for i,v in pairs(data) do
    	rotators[i] = v
    end
end
-- in the function u r drawing the text add this to it
triggerServerEvent("rotators.send", getLocalPlayer())

well not tested

 

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