koragg Posted September 22, 2017 Share Posted September 22, 2017 I found this function on the wiki and my question is: If I set my licence plate client-side from a GUI text box, would it show updated for other players spectating me or not? https://wiki.multitheftauto.com/wiki/SetVehiclePlateText Link to comment
Addlibs Posted September 22, 2017 Share Posted September 22, 2017 If you do it from clientside, it will only update on the screen of the player on whose client you called the function. Link to comment
koragg Posted September 22, 2017 Author Share Posted September 22, 2017 46 minutes ago, MrTasty said: If you do it from clientside, it will only update on the screen of the player on whose client you called the function. Bummer, that's what I thought too. Will be harder to sync than I though Link to comment
raynner Posted September 23, 2017 Share Posted September 23, 2017 in fact you just need to send a triggerServerEvent ("Event", localPlayer, Vehicle, Text) ... on the client whose call was made to the function. Then on the server use a for loop for all players and then a triggerClientEvent for all players and reuse the setVehiclePlateText (vehicle, Text) it is worth remembering that you will have to somehow store this and so that in the future with onPlayerConnect or onPlayerLogin run again so that this is inserted for new players! Sorry for the bad english. I hope to help! Link to comment
koragg Posted September 23, 2017 Author Share Posted September 23, 2017 Here goes. So I wasted ~ 1 hour and did the following things: Cliend-side stuff: visual = { -- Standard Settings, 0 = off -- ["plate"] = string.sub(getPlayerName(localPlayer):gsub('#%x%x%x%x%x%x', ''), 1, 8), } ------------------------------------------------------------------------------------------------------------------------- function visual_ButtonHandler() if source == GUIEditor.button["plate"] then local nr = guiGetText(GUIEditor.edit["plate"]) if not nr then outputChatBox("Please insert a string to use as your licence plate before clicking ok.") return end if nr == nil or nr == "" or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " then outputChatBox("Your licence plate cannot be empty. Changed to default one. ") nr = string.sub(getPlayerName(localPlayer):gsub('#%x%x%x%x%x%x', ''), 1, 8) end local veh = getPedOccupiedVehicle(localPlayer) if veh then if getElementData(localPlayer, "LoggedIn") ~= true then outputChatBox("You need to register and login in order to set a custom licence plate!", 255, 153, 0, true) return end setVehiclePlateText(veh, nr) setElementData(localPlayer, "savedPlate", nr) setElementData(localPlayer, "plate", nr) visual["plate"] = nr v_setSaveTimer() end end end end addEventHandler("onClientGUIClick", resourceRoot, visual_ButtonHandler) ------------------------------------------------------------------------------------------------------------------------- function visual_LoadSettings() for w, c in pairs(visual) do -- Move settings to visual table -- local theChild = xmlFindChild( v_loadXML, w, 0 ) if not theChild then local theChild = xmlCreateChild( v_loadXML, w ) xmlNodeSetValue( theChild, c ) end if w ~= "nitrocolor" and w ~= "plate" then -- nitrocolor is not a number value local val = xmlNodeGetValue( theChild ) visual[w] = tonumber(val) else local val = xmlNodeGetValue( theChild ) visual[w] = val end if w == "plate" then local veh = getPedOccupiedVehicle(localPlayer) setElementData(localPlayer, "savedPlate", c) setElementData(localPlayer, "plate", c) local pltTxt = c if getElementData(localPlayer, "LoggedIn") ~= true then pltTxt = string.sub(getPlayerName(localPlayer):gsub('#%x%x%x%x%x%x', ''), 1, 8) end if veh then setVehiclePlateText(veh, pltTxt) end end end setVisualGUI() end addEventHandler("onClientResourceStart", resourceRoot, visual_LoadSettings) ------------------------------------------------------------------------------------------------------------------------- addEvent("changePlateOnLogout", true) function changePlateOnLogout(veh, pltTxt) if veh and pltTxt then setVehiclePlateText(veh, pltTxt) end end addEventHandler("changePlateOnLogout", root, changePlateOnLogout) ------------------------------------------------------------------------------------------------------------------------- addEvent("changePlateOnLogin", true) function changePlateOnLogin(veh, pltTxt) if veh and pltTxt then setVehiclePlateText(veh, pltTxt) end end addEventHandler("changePlateOnLogin", root, changePlateOnLogin) ------------------------------------------------------------------------------------------------------------------------- addEvent("changePlateOnNickChange", true) function changePlateOnNickChange(veh, pltTxt) if veh and pltTxt then setVehiclePlateText(veh, pltTxt) end end addEventHandler("changePlateOnNickChange", root, changePlateOnNickChange) ------------------------------------------------------------------------------------------------------------------------- function setVisualGUI() for f, u in pairs(visual) do if f == "plate" then if u ~= nil or u ~= "" or u ~= " " or u ~= " " or u ~= " " or u ~= " " or u ~= " " or u ~= " " or u ~= " " or u ~= " " then guiSetText(GUIEditor.edit["plate"], u) local veh = getPedOccupiedVehicle(localPlayer) if veh then if getElementData(localPlayer, "LoggedIn") ~= true then outputChatBox("You need to register and login in order to set a custom licence plate!", 255, 153, 0, true) return end setVehiclePlateText(veh, u) setElementData(localPlayer, "savedPlate", u) setElementData(localPlayer, "plate", u) visual["plate"] = u end else local plate local veh = getPedOccupiedVehicle(localPlayer) if veh then plate = getVehiclePlateText(veh) end if plate then guiSetText(GUIEditor.edit["plate"], plate) end end end end end Server-side stuff: addEvent("onMapStarting", true) function setLicencePlate() setTimer(function() for k,v in ipairs(getElementsByType("player")) do local account = getPlayerAccount(v) if account and isGuestAccount(account) then return end local veh = getPedOccupiedVehicle(v) local plateText = getElementData(v, "plate") if veh and plateText and plateText ~= nil then setVehiclePlateText(veh, plateText) end end end, 50, 1) end addEventHandler("onMapStarting", root, setLicencePlate) addEventHandler("onPlayerSpawn", root, setLicencePlate) addEventHandler("onPlayerJoin", root, setLicencePlate) ------------------------------------------------------------------------------------------------------------------------- function onPlayerLogout() local vehicle = getPedOccupiedVehicle(source) local newPlate = string.sub(getPlayerName(source):gsub('#%x%x%x%x%x%x', ''), 1, 8) triggerClientEvent(root, "changePlateOnLogout", source, vehicle, newPlate) end addEventHandler("onPlayerLogout", root, onPlayerLogout) ------------------------------------------------------------------------------------------------------------------------- function onPlayerLogin() local newPlate local vehicle = getPedOccupiedVehicle(source) local savedPlate = getElementData(source, "savedPlate") if savedPlate then newPlate = string.sub(savedPlate:gsub('#%x%x%x%x%x%x', ''), 1, 8) else newPlate = string.sub(getPlayerName(source):gsub('#%x%x%x%x%x%x', ''), 1, 8) end triggerClientEvent(root, "changePlateOnLogin", source, vehicle, newPlate) end addEventHandler("onPlayerLogin", root, onPlayerLogin) ------------------------------------------------------------------------------------------------------------------------- function onPlayerChangeNick(oldNick, newNick) local account = getPlayerAccount(source) local savedPlate = getElementData(source, "savedPlate") if savedPlate and account and not isGuestAccount(account) then return end local vehicle = getPedOccupiedVehicle(source) local newPlate = string.sub(newNick:gsub('#%x%x%x%x%x%x', ''), 1, 8) triggerClientEvent(root, "changePlateOnNickChange", source, vehicle, newPlate) end addEventHandler("onPlayerChangeNick", root, onPlayerChangeNick) ------------------------------------------------------------------------------------------------------------------------- function onPlayerLogin() local playeraccount = getPlayerAccount(source) if playeraccount then if not isGuestAccount(playeraccount) then setElementData(source, "LoggedIn", true) else setElementData(source, "LoggedIn", false) end end end addEventHandler("onPlayerLogin", root, onPlayerLogin) Obviosly this is just the part for the plates (there is a :~load of code for other settings in the resource). For me - it works. No bugs, no problems. But would it be synced to all other players is my question? Like, when I change my licence plate to a custom one from the GUI, would a player next to me or spectating me see the updated custom plate? If anyone can take a look at the above lines and tell me, I'd appreciate it. Link to comment
Moderators IIYAMA Posted September 23, 2017 Moderators Share Posted September 23, 2017 52 minutes ago, koragg said: Server-side stuff: addEvent("onMapStarting", true) function setLicencePlate() setTimer(function() for k,v in ipairs(getElementsByType("player")) do local account = getPlayerAccount(v) if account and isGuestAccount(account) then return end local veh = getPedOccupiedVehicle(v) local plateText = getElementData(v, "plate") if veh and plateText and plateText ~= nil then setVehiclePlateText(veh, plateText) end end end, 50, 1) end addEventHandler("onMapStarting", root, setLicencePlate) addEventHandler("onPlayerSpawn", root, setLicencePlate) addEventHandler("onPlayerJoin", root, setLicencePlate) Will kill your performance and with a lot of players even your server. With this you create infinity infinity-timers. This might work better.(serverside) But you have to test if it also works the first time you set the elementdata. addEventHandler("onElementDataChange", root, function (dataName,oldValue) if dataName == "plate" and getElementType(source) == "player" then local account = getPlayerAccount(source) if account and not isGuestAccount(account) then local vehicle = getPedOccupiedVehicle (source) if vehicle then local newPlate = getElementData(source, "plate") or math.random(4000,5000) setVehiclePlateText(vehicle, newPlate) iprint("onElementDataChange, vehicle:", vehicle, ", player:", source, ", new plate:", newPlate) end end end end) --[[ Also: addEventHandler("onPlayerLogin" > set plate text ]] Else you also have to change this clientside: function visual_ButtonHandler() if source == GUIEditor.button["plate"] then local nr = guiGetText(GUIEditor.edit["plate"]) if not nr then outputChatBox("Please insert a string to use as your licence plate before clicking ok.") return end if nr == nil or nr == "" or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " or nr == " " then outputChatBox("Your licence plate cannot be empty. Changed to default one. ") nr = string.sub(getPlayerName(localPlayer):gsub('#%x%x%x%x%x%x', ''), 1, 8) end local veh = getPedOccupiedVehicle(localPlayer) if veh then if getElementData(localPlayer, "LoggedIn") ~= true then outputChatBox("You need to register and login in order to set a custom licence plate!", 255, 153, 0, true) return end setVehiclePlateText(veh, nr) -- change -- if getElementData(localPlayer, "plate") == nil then setElementData(localPlayer, "plate", false) end -- setElementData(localPlayer, "savedPlate", nr) setElementData(localPlayer, "plate", nr) visual["plate"] = nr v_setSaveTimer() end end end end addEventHandler("onClientGUIClick", resourceRoot, visual_ButtonHandler) Link to comment
koragg Posted September 23, 2017 Author Share Posted September 23, 2017 Thanks for your input @IIYAMA but setTimer says: timesToExecute: The number of times you want the timer to execute, or 0 for infinite repetitions. And I set it to '1' so that it executes just once on every map start. Am I missing something? Shouldn't it be '0' to be an infinite timer? Link to comment
Moderators IIYAMA Posted September 23, 2017 Moderators Share Posted September 23, 2017 Oh ops, misread that. What is the timer for? If you want to use the method, I would recommend you to add this: addEvent("onMapStarting", true) function setLicencePlate() setTimer(function() for k,v in ipairs(getElementsByType("player")) do local account = getPlayerAccount(v) if account and isGuestAccount(account) then return end local veh = getPedOccupiedVehicle(v) local plateText = getElementData(v, "plate") if veh and plateText and plateText ~= nil then -- if getVehiclePlateText ( v ) ~= plateText then setVehiclePlateText(veh, plateText) end -- end end end, 50, 1) end addEventHandler("onMapStarting", root, setLicencePlate) addEventHandler("onPlayerSpawn", root, setLicencePlate) addEventHandler("onPlayerJoin", root, setLicencePlate) It will save you MAX 100% network bandwidth.(that's when no plates have been changed) Even if the plate is the same as before, the server will send a message to all the clients that the value has changed. This due the fact that clients can modify this value and serverside has to overwrite it. You should compare it. Even so, it is not an efficient method > performance. Link to comment
koragg Posted September 23, 2017 Author Share Posted September 23, 2017 (edited) It just refused to work without a slight delay after the map has started OK, I'll try to fix it up a bit after dinner and report back soon. PS: i just saw there's no need to trigger client event on login logout and namechange will just put licence plate function in server side. Edited September 23, 2017 by koragg Link to comment
koragg Posted September 23, 2017 Author Share Posted September 23, 2017 (edited) Edited a bit and tested with a real-life player (woah ). Everything works perfect, also optimized a bit as you suggested @IIYAMA. Edited September 23, 2017 by koragg Mentions '@' are broken :( 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