Created
December 2, 2022 09:31
-
-
Save feelfreetofee/238f7cfca3c8199a335b91b37431d8ff to your computer and use it in GitHub Desktop.
FiveM Particles Sync Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Client | |
| clientParticles = {} | |
| RegisterNetEvent('ClientParticle') | |
| AddEventHandler('ClientParticle', function(player, name, dictionary, pos, rot, bone, scale) | |
| if pos then | |
| local ped = GetPlayerPed(GetPlayerFromServerId(player)) | |
| RequestNamedPtfxAsset(dictionary) | |
| while not HasNamedPtfxAssetLoaded(dictionary) do Wait(0) end | |
| SetPtfxAssetNextCall(dictionary) | |
| if not clientParticles[player] then | |
| clientParticles[player] = {} | |
| end | |
| clientParticles[player][dictionary .. ':' .. name] = StartParticleFxLoopedOnPedBone(name, ped, pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, GetPedBoneIndex(ped, bone), scale) | |
| elseif clientParticles[player] and clientParticles[player][dictionary .. ':' .. name] then | |
| StopParticleFxLooped(clientParticles[player][dictionary .. ':' .. name]) | |
| end | |
| end) | |
| -- Server | |
| serverParticles = {} | |
| function ServerParticle(src, name, dictionary, pos, rot, bone, scale, time) | |
| if serverParticles[src] and serverParticles[src][dictionary .. ':' .. name] and serverParticles[src][dictionary .. ':' .. name].time > 0 then | |
| serverParticles[src][dictionary .. ':' .. name].time += time | |
| else | |
| if not serverParticles[src] then | |
| serverParticles[src] = {} | |
| end | |
| if not serverParticles[src][dictionary .. ':' .. name] then | |
| serverParticles[src][dictionary .. ':' .. name] = {} | |
| serverParticles[src][dictionary .. ':' .. name].time = 0 | |
| serverParticles[src][dictionary .. ':' .. name].players = {} | |
| end | |
| local pedpos = GetEntityCoords(GetPlayerPed(src)) | |
| for _,player in pairs(GetPlayers()) do | |
| if #(GetEntityCoords(GetPlayerPed(player)) - pedpos) < 50.0 then | |
| table.insert(serverParticles[src][dictionary .. ':' .. name].players, player) | |
| TriggerClientEvent('ClientParticle', player, src, name, dictionary, pos, rot, bone, scale) | |
| end | |
| end | |
| serverParticles[src][dictionary .. ':' .. name].time = time | |
| local timer = GetGameTimer() | |
| while GetGameTimer() - timer < serverParticles[src][dictionary .. ':' .. name].time do Wait(100) end | |
| serverParticles[src][dictionary .. ':' .. name].time = 0 | |
| for _,player in pairs(serverParticles[src][dictionary .. ':' .. name].players) do | |
| TriggerClientEvent('ClientParticle', player, src, name, dictionary) | |
| end | |
| serverParticles[src][dictionary .. ':' .. name].players = {} | |
| end | |
| end | |
| RegisterNetEvent('ServerParticle') | |
| AddEventHandler('ServerParticle', function(name, dictionary, pos, rot, bone, scale, time) | |
| ServerParticle(source, name, dictionary, pos, rot, bone, scale, time) | |
| end) | |
| -- End of code -- | |
| -- Example Usage | |
| local name = "ent_amb_peeing" | |
| local dictionary = "core" | |
| local pos = {x = 0.0, y = 0.1, z = -0.1} | |
| local rot = {x = -140.0, y = 0.0, z = 0.0} | |
| local bone = 11816 | |
| local scale = 1.0 | |
| local time = 1000 | |
| TriggerServerEvent('ServerParticle', name, dictionary, pos, rot, bone, scale, time) | |
| -- Native Comparisson | |
| local ped = PlayerPedId() | |
| RequestNamedPtfxAsset(dictionary) | |
| while not HasNamedPtfxAssetLoaded(dictionary) do Wait(0) end | |
| SetPtfxAssetNextCall(dictionary) | |
| local particle = StartParticleFxLoopedOnPedBone(name, ped, pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, GetPedBoneIndex(ped, bone), scale) | |
| Wait(time) | |
| StopParticleFxLooped(particle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment