Jump to content

How to read a variable?


hemen

Recommended Posts

Posted

How to read variable from server-side script in client-side script?

I tryed to write variable in file, and read by triggerServerEvent, buy it gave me any warnings.

--server-side
function fileOpenFunc(file,dir)
file = fileOpen(dir)
end
addEvent("a_fileOpen",true)
addEvent("a_fileOpen",getRootElement(),fileOpenFunc)
function fileReadFunc(file,text)
text=fileRead(file,15)
end
addEvent("a_fileRead",true)
addEvent("a_fileRead",getRootElement(),fileReadFunc)
function fileCloseFunc(file)
fileClose(file)
end
addEvent("a_fileClose",true)
addEvent("a_fileClose",getRootElement(),fileWriteFunc)

--client-side
local file = nil
local text = nil
triggerServerEvent("a_fileOpen",file,"/properties/"..row..".txt")
triggerServerEvent("a_fileRead",file,text)
triggerServerEvent("a_fileClose",file)

I tryed return variable by triggerServerEvent, but it also gave me any warnings.

Posted

To exchange data between the server and client, I advise you'd use getElementData and setElementData.

triggerServerEvent or triggerClientEvent don't return a value from "the other end", because there is no direct connection to a function returning a value, the event, to which the function was bound and the triggering from the other side.

Also, you added the events twice. I guess you forgot the "-Handler" for example at this point:

addEvent("a_fileOpen",getRootElement(),fileOpenFunc)

Posted

Or you can trigger another client event from server which would contain the 'return' value. But element data is still easier and more efficient way.

Posted

It's only more efficient if you intend every client to get the element data. Otherwise, events can be more efficient as they're directed at single clients. Returning data to the server with element data sends that data to all the other clients too, which is probably not what you want here.

So, I'd use events generally. Use element data for what it's intended for - synchronising the state of elements.

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