JAVAAA Posted May 9, 2017 Share Posted May 9, 2017 (edited) Hello, I am asking about how to read a file in the server and get the data from it into client? I tried a lot of times but nothing happens at all. Server : function getUpdates() local updates local file = "UpdatesLog.txt" local size = fileGetSize(file) local openFile = fileOpen(file) if openFile then while not fileIsEOF(openFile) do updates = fileRead(openFile, size) end fileClose(openFile) end triggerClientEvent(client, "getUpdates", client, updates) end Client : local key = "F1" updatesPanel = guiCreateWindow(195, 64, 986, 579, "Updates Changelog", false) guiWindowSetSizable(updatesPanel, false) updatesMemo = guiCreateMemo(10, 26, 966, 539, "", false, updatesPanel) guiSetVisible(updatesPanel, false) guiSetInputMode("no_binds_when_editing") function showAndCloseWindow(updates) if not guiGetVisible(updatesPanel) then guiSetVisible(updatesPanel, true) showCursor(true) guiSetText(updatesMemo, updates) else guiSetVisible(updatesPanel, false) showCursor(false) end end bindKey(key, "down", showAndCloseWindow) addEvent("getUpdates", true) addEventHandler("getUpdates", root, showAndCloseWindow) Edited May 9, 2017 by JAVAAA Link to comment
Avagard Posted May 9, 2017 Share Posted May 9, 2017 fileRead is a Shared function... why don't you use it on client? Link to comment
Addlibs Posted May 9, 2017 Share Posted May 9, 2017 (edited) function getUpdates() local file = "UpdatesLog.txt" local openFile = fileOpen(file) if not openFile then outputChatBox("Error loading file") return false end local size = fileGetSize(openFile) local updates = fileRead(openFile, size) fileClose(openFile) triggerClientEvent(client, "getUpdates", client, updates) end 2 minutes ago, Avagard said: fileRead is a Shared function... why don't you use it on client? Perhaps the file he wants to read is stored on the server? Edited May 9, 2017 by MrTasty Swapped lines 4 and 5 Link to comment
JAVAAA Posted May 9, 2017 Author Share Posted May 9, 2017 Just now, MrTasty said: function getUpdates() local file = "UpdatesLog.txt" local openFile = fileOpen(file) local size = fileGetSize(openFile) if not openFile then outputChatBox("Error loading file") return false end local updates = fileRead(openFile, size) fileClose(openFile) triggerClientEvent(client, "getUpdates", client, updates) end Perhaps the file he wants to read is stored on the server? Yeah exactly. Link to comment
Addlibs Posted May 9, 2017 Share Posted May 9, 2017 I've made a quick correction (checking whether the file is open before reading its size) by swapping line 4 and 5 on the original reply, so just check the code again. Link to comment
JAVAAA Posted May 9, 2017 Author Share Posted May 9, 2017 Now, the memo box's showing "F1" only while there is no text called "F1" in my .txt file. Link to comment
Gordon_G Posted May 10, 2017 Share Posted May 10, 2017 (edited) function getUpdates() local file = "UpdatesLog.txt" local openFile = fileOpen(file) local size = fileGetSize(openFile) if not openFile then outputChatBox("Error loading file") return false end local updates = fileRead(openFile, size+500) fileClose(openFile) triggerClientEvent(client, "getUpdates", client, updates) end It works fine for me when I create logs file for exemple. Edited May 10, 2017 by Gordon_G Link to comment
Addlibs Posted May 10, 2017 Share Posted May 10, 2017 16 hours ago, JAVAAA said: Now, the memo box's showing "F1" only while there is no text called "F1" in my .txt file. Perhaps you set as text the 'key' variable instead of 'updates' Link to comment
JAVAAA Posted May 10, 2017 Author Share Posted May 10, 2017 Didn't work yet, here's my code now: Server: function sayUpdates() local localTime = getRealTime(_,true) local day = localTime.monthday local month = localTime.month + 1 local year = localTime.year + 1900 outputChatBox("#F2D200["..year..":"..month..":"..day.."]NEW UPDATES! CHECK F1 TO SEE LATEST UPDATES!", getRootElement(), 255, 255, 255, true) end addEventHandler("onResourceStart", getResourceRootElement ( getThisResource() ), sayUpdates) function getUpdates() local file = "UpdatesLog.txt" local openFile = fileOpen(file) local size = fileGetSize(openFile) if not openFile then outputChatBox("Error loading file") return false end local updates = fileRead(openFile, size+500) fileClose(openFile) triggerClientEvent(client, "getUpdates", client, updates) end Client : --[[------------------------------------------------- Notes: > This code is using a relative image filepath. This will only work as long as the location it is from always exists, and the resource it is part of is running. To ensure it does not break, it is highly encouraged to move images into your local resource and reference them there. --]]------------------------------------------------- GUIEditor = { label = {} } --local key = "F1" updatesPanel = guiCreateWindow(195, 64, 986, 579, "Updates Changelog", false) guiWindowSetSizable(updatesPanel, false) updatesMemo = guiCreateMemo(10, 26, 966, 539, "", false, updatesPanel) guiSetVisible(updatesPanel, false) guiSetInputMode("no_binds_when_editing") function showAndCloseWindow(command, state) if not guiGetVisible(updatesPanel) then guiSetVisible(updatesPanel, true) showCursor(true) getUpdates() else guiSetVisible(updatesPanel, false) showCursor(false) end end bindKey("F1", "down", showAndCloseWindow) function getUpdates(updates) guiSetText(updatesMemo, updates) end addEvent("getUpdates", true) addEventHandler("getUpdates", root, getUpdates) Gives me an error saying "Bad argument guiSetText, argument 2 expected string got nil" Link to comment
Gordon_G Posted May 10, 2017 Share Posted May 10, 2017 (edited) You never call getUpdates() Edited May 10, 2017 by Gordon_G Link to comment
JAVAAA Posted May 10, 2017 Author Share Posted May 10, 2017 10 minutes ago, Gordon_G said: You never call getUpdates() function showAndCloseWindow(command, state) if not guiGetVisible(updatesPanel) then guiSetVisible(updatesPanel, true) showCursor(true) getUpdates() else guiSetVisible(updatesPanel, false) showCursor(false) end end bindKey("F1", "down", showAndCloseWindow) function getUpdates(updates) guiSetText(updatesMemo, updates) end addEvent("getUpdates", true) addEventHandler("getUpdates", root, getUpdates) .... Link to comment
Addlibs Posted May 10, 2017 Share Posted May 10, 2017 (edited) --client -- [...] function showAndCloseWindow(command, state) if not guiGetVisible(updatesPanel) then guiSetVisible(updatesPanel, true) showCursor(true) requestUpdates() else guiSetVisible(updatesPanel, false) showCursor(false) end end bindKey("F1", "down", showAndCloseWindow) function requestUpdates() -- inquire for the updates text from the server triggerServerEvent("requestUpdates", localPlayer) end function getUpdates(updates) guiSetText(updatesMemo, updates) end addEvent("getUpdates", true) addEventHandler("getUpdates", root, getUpdates) -- [...] --server -- [...] function getUpdates() local file = "UpdatesLog.txt" local openFile = fileOpen(file) if not openFile then outputChatBox("Error loading file") return false end local size = fileGetSize(openFile) local updates = fileRead(openFile, size+500) fileClose(openFile) triggerClientEvent(client, "getUpdates", client, updates) end addEvent("requestUpdates", true) addEventHandler("requestUpdates", root, getUpdates) -- [...] Edited May 10, 2017 by MrTasty Link to comment
JAVAAA Posted May 10, 2017 Author Share Posted May 10, 2017 Worked, thanks. I wonder how it didn't work with me while your code is completely similar to my previous code ^^ Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now