Jump to content

[ADVICE] Is this bad or it's ok?


koragg

Recommended Posts

It works as it should. I have constant 50 FPS (server's max) and don't get lags...but I still wanted to ask - is the below implementation badly done or it's ok to leave it like it is?

function onResStart()
	setTimer(updatePJs, 300, 0)
end
addEventHandler("onResourceStart", resourceRoot, onResStart)

function updatePJs()
	for i, player in ipairs(getElementsByType("player")) do
		local account = getPlayerAccount(player)
		if account and isGuestAccount(account) then
			if getElementData(player, "paintjob") then
				triggerClientEvent(root, "unloadVehiclePaintjob", resourceRoot, vehicle, tostring(getElementData(player, "paintjob")))
			end
			return
		end
		if account and not isGuestAccount(account) then
			local vehicle = getPedOccupiedVehicle(player)
			local paintjob = getAccountData(account, "paintjob") or "NoPaintjob"
			setElementData(player, "paintjob", paintjob)
			if vehicle and paintjob then
				triggerClientEvent(root, "loadVehiclePaintjob", resourceRoot, vehicle, tostring(paintjob))
			end
		end
	end
end

I can't make it work without the infinite timer but, still, I want to see what more experienced devs than me think about it :)

Edited by koragg
Link to comment

In some scenarios you have no option but to use something like that. Although I see you're using elementData to check whether you'd need to load or unload the paintsjob. Element data somehow needs to be added / removed, right? When you add the elementData, why can't you just use the triggerClient/ServerEvent w/e side it is in and then load/unload?

Link to comment
14 minutes ago, pa3ck said:

In some scenarios you have no option but to use something like that. Although I see you're using elementData to check whether you'd need to load or unload the paintsjob. Element data somehow needs to be added / removed, right? When you add the elementData, why can't you just use the triggerClient/ServerEvent w/e side it is in and then load/unload?

This function actually just makes sure that every player's vehicle is wearing the paintjob saved to that player's account, hence the infinite low timer. I load/unload the paintjob the way you mentioned in your 2nd question whenever a player clicks on some GUI stuff to select/change their PJ. But I couldn't figure out another way to keep all paintjobs synced with everyone else other than this one.

The elementData is only used to transfer the account data to the client side. The check you're refering to is to see if a logged out player still has a paintjob and if so to remove it from their vehicle.

Edited by koragg
Link to comment

Okay, so if I understand correctly, when a player logs in you can add an elementData from their accountData to save their paintjob. When somebody joins you can loop through all players and load the paintjobs client side based on their elementData which you have saved when they logged in. Use the event onPlayerLogout to remove elementData and trigger to every client to remove the paintjob. I guess that kinda does the same thing? 

Link to comment
2 minutes ago, pa3ck said:

Okay, so if I understand correctly, when a player logs in you can add an elementData from their accountData to save their paintjob. When somebody joins you can loop through all players and load the paintjobs client side based on their elementData which you have saved when they logged in. Use the event onPlayerLogout to remove elementData and trigger to every client to remove the paintjob. I guess that kinda does the same thing? 

Yeah, I was wondering if I should use 'onPlayerJoin' yesterday. Guess it will be more efficient that way.

Link to comment
1 hour ago, Fist said:

or just use "onPlayerLogin"

Won't do because that would mean that paintjobs only get updated when someone logs in. What if i join alone and two guests join after me. They won't see my paintjob as the function wouldn't have been triggered because there was no login to trigger 'onPlayerLogin'.

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