Jump to content

[QUESTION] setVehiclePlateText() doubt


koragg

Recommended Posts

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

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

 

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
  • Moderators

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

It just refused to work without a slight delay after the map has started :D 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 xD will just put licence plate function in server side.

Edited by koragg
Link to comment

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