BurakAKSAKAL Posted July 10, 2023 Share Posted July 10, 2023 (edited) Hello, I would like to set the game time of the user to 5 when the "/night" command is used, using the setTime function on the client-side. Similarly, when the "/mor" command is used, I want to set the game time of the user to 12. local t = { ['night'] = {5, 0}, ['mor'] = {12, 0}, } However, every time a new map starts, these settings are reset, and users have to enter these commands again. I am saving this information to a file called "times.json". But as I mentioned, every time a new map starts, these settings are lost. However, when I restart the script, these settings are restored, and the selected /night or /mor setting becomes active. Here are my current commands: client.lua: local t = { ['night'] = {5, 0}, ['mor'] = {12, 0}, } addEvent("loadTimeSettings", true) addEventHandler("loadTimeSettings", resourceRoot, function() loadTimeSettings() end) addEventHandler('onClientResourceStart', resourceRoot, function() for i, v in pairs(t) do addCommandHandler(i, function() setTime(unpack(v)) triggerServerEvent("saveTimeSettings", resourceRoot, t) end) end triggerServerEvent("loadTimeSettings", resourceRoot) end) function loadTimeSettings() local file = fileOpen("times.json") if file then local json = fileRead(file, fileGetSize(file)) local savedSettings = fromJSON(json) if savedSettings then for i, v in pairs(savedSettings) do setTime(unpack(v)) end end fileClose(file) end end server.lua: addEvent("saveTimeSettings", true) addEventHandler("saveTimeSettings", root, function(t) local file = fileCreate("times.json") if file then local json = toJSON(t) fileWrite(file, json) fileClose(file) outputDebugString("Succesful saved.: "..json) else outputDebugString("warning") end end) addEventHandler('onResourceStart', resourceRoot, function() loadTimeSettings() end) function loadTimeSettings() local file = fileOpen("times.json") if file then local json = fileRead(file, fileGetSize(file)) local savedSettings = fromJSON(json) if savedSettings then for i, v in pairs(savedSettings) do setTime(unpack(v)) end end fileClose(file) end end Thanks! Edited July 10, 2023 by BurakAKSAKAL Link to comment
DiSaMe Posted July 11, 2023 Share Posted July 11, 2023 From what I understand, the time setting is a part of the map, used by mapmanager resource to change the time. Nothing to do with your script, it's just that they both call setTime, and each call overrides whatever the time was set to previously. If you want to override it from your script, you can try using mapmanager's event onGamemodeMapStart for that - so whenever a map starts and the mapmanager changes the time, your script would change it back. According to the code of mapmanager, the map settings are applied after onGamemodeMapStart is triggered so your overriding code should have a delay in order to set time after the map's time setting is applied, not before. setTimer with 0 milliseconds will probably do the thing. I'm not familiar with manager resources so I don't know if there's a cleaner way to do it. Link to comment
ExMohmD Posted August 4, 2023 Share Posted August 4, 2023 It seems like you have implemented most of the functionality correctly. However, the issue lies in how you are saving the time settings. Currently, you are overwriting the "times.json" file every time the "/night" or "/mor" command is used. Instead, you should be updating the values in the file rather than recreating it every time. To achieve this, you can modify the server.lua file as follows: server.lua: local filename = "times.json" function saveTimeSettings(t) local file = fileCreate(filename) if file then local json = toJSON(t) fileWrite(file, json) fileClose(file) outputDebugString("Successfully saved: " .. json) else outputDebugString("Failed to save time settings.") end end function loadTimeSettings() local file = fileOpen(filename) if file then local json = fileRead(file, fileGetSize(file)) local savedSettings = fromJSON(json) if savedSettings then for i, v in pairs(savedSettings) do setTime(unpack(v)) end end fileClose(file) end end addEvent("saveTimeSettings", true) addEventHandler("saveTimeSettings", root, function(t) saveTimeSettings(t) end) addEventHandler('onResourceStart', resourceRoot, function() loadTimeSettings() end) Now, you need to modify the client.lua file to only trigger the server event when the settings change, instead of doing it every time the command is used: client.lua: local t = { ['night'] = {5, 0}, ['mor'] = {12, 0}, } addEvent("loadTimeSettings", true) addEventHandler("loadTimeSettings", resourceRoot, function() loadTimeSettings() end) addEventHandler('onClientResourceStart', resourceRoot, function() for i, v in pairs(t) do addCommandHandler(i, function() setTime(unpack(v)) triggerServerEvent("saveTimeSettings", resourceRoot, t) end) end triggerServerEvent("loadTimeSettings", resourceRoot) end) function loadTimeSettings() local file = fileOpen("times.json") if file then local json = fileRead(file, fileGetSize(file)) local savedSettings = fromJSON(json) if savedSettings then for i, v in pairs(savedSettings) do setTime(unpack(v)) end end fileClose(file) end end With these changes, the time settings should now persist between map changes and script restarts. The time settings will be saved to "times.json" and loaded back when the script starts or when a new map is loaded. 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