Skip to content

Commit 0a0c477

Browse files
authored
Merge pull request Blumlaut#905 from Gravxd/master
fix: Spectating multiple players can cause error + validate targets of spectate/tp better
2 parents 44eef4e + e40da21 commit 0a0c477

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

client/admin_client.lua

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,25 @@ RegisterNetEvent("EasyAdmin:requestSpectate", function(playerServerId, playerDat
189189
local tgtCoords = playerData.coords
190190

191191
if ((not tgtCoords) or (tgtCoords.z == 0.0)) then tgtCoords = GetEntityCoords(GetPlayerPed(GetPlayerFromServerId(playerServerId))) end
192-
if playerServerId == GetPlayerServerId(PlayerId()) then
193-
if oldCoords then
194-
RequestCollisionAtCoord(oldCoords.x, oldCoords.y, oldCoords.z)
195-
Wait(500)
196-
SetEntityCoords(playerPed, oldCoords.x, oldCoords.y, oldCoords.z, 0, 0, 0, false)
197-
oldCoords=nil
198-
end
199-
spectatePlayer(localPlayerPed,GetPlayerFromServerId(PlayerId()),GetPlayerName(PlayerId()))
200-
frozen = false
201-
FreezeMyself(false)
202-
return
203-
else
204-
if not oldCoords then
205-
oldCoords = GetEntityCoords(PlayerPedId())
206-
end
192+
193+
if not IsSpectating then
194+
oldCoords = GetEntityCoords(PlayerPedId())
207195
end
196+
208197
SetEntityCoords(localPlayerPed, tgtCoords.x, tgtCoords.y, tgtCoords.z - 10.0, 0, 0, 0, false)
209198
frozen = true
210199
FreezeMyself(true)
211200
stopSpectateUpdate = true
212201
local playerId = GetPlayerFromServerId(playerServerId)
202+
local timer = 0
213203
repeat
214204
Wait(200)
215205
playerId = GetPlayerFromServerId(playerServerId)
216-
until ((GetPlayerPed(playerId) > 0) and (playerId ~= -1))
206+
timer = timer + 1
207+
until ( (GetPlayerPed(playerId) > 0) and (playerId ~= -1) or timer > 25)
208+
if timer >= 25 then
209+
return
210+
end
217211
spectatePlayer(GetPlayerPed(playerId),playerId,GetPlayerName(playerId))
218212
stopSpectateUpdate = false
219213
end)
@@ -388,7 +382,7 @@ function spectatePlayer(targetPed,target,name)
388382
SetEntityCollision(playerPed, false, false)
389383
SetEntityInvincible(playerPed, true)
390384
NetworkSetEntityInvisibleToNetwork(playerPed, true)
391-
Citizen.Wait(200) -- to prevent target player seeing you
385+
Wait(200) -- to prevent target player seeing you
392386
if targetPed == playerPed then
393387
Wait(500)
394388
targetPed = GetPlayerPed(target)
@@ -404,14 +398,13 @@ function spectatePlayer(targetPed,target,name)
404398
RequestCollisionAtCoord(oldCoords.x, oldCoords.y, oldCoords.z)
405399
Wait(500)
406400
SetEntityCoords(playerPed, oldCoords.x, oldCoords.y, oldCoords.z, 0, 0, 0, false)
407-
oldCoords=nil
408401
end
409402
NetworkSetInSpectatorMode(false, targetPed)
410403
StopDrawPlayerInfo()
411404
TriggerEvent("EasyAdmin:showNotification", GetLocalisedText("stoppedSpectating"))
412405
frozen = false
413406
FreezeMyself(false)
414-
Citizen.Wait(200) -- to prevent staying invisible
407+
Wait(200) -- to prevent staying invisible
415408
SetEntityVisible(playerPed, true, 0)
416409
SetEntityCollision(playerPed, true, true)
417410
SetEntityInvincible(playerPed, false)

server/admin_server.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,24 @@ Citizen.CreateThread(function()
323323
if DoesPlayerHavePermission(source, "player.spectate") and CheckAdminCooldown(source, "spectate") then
324324
SetAdminCooldown(source, "spectate")
325325
PrintDebugMessage("Player "..getName(source,true).." Requested Spectate to "..getName(playerId,true), 3)
326-
local tgtCoords = GetEntityCoords(GetPlayerPed(playerId))
326+
local tgtPed = GetPlayerPed(playerId)
327+
if tgtPed == 0 then
328+
-- ped does not exist left or not loaded in yet
329+
TriggerClientEvent("EasyAdmin:showNotification", source, GetLocalisedText("playernotfound"))
330+
return
331+
end
332+
333+
if playerId == source then
334+
return
335+
end
336+
337+
local tgtCoords = GetEntityCoords(tgtPed)
338+
if tgtCoords.x == 0.00 and tgtCoords.y == 0.00 then
339+
-- loaded in but still spawning
340+
TriggerClientEvent("EasyAdmin:showNotification", source, GetLocalisedText("playernotfound"))
341+
return
342+
end
343+
327344
local playerBucket = GetPlayerRoutingBucket(playerId)
328345
local sourceBucket = GetPlayerRoutingBucket(source)
329346
if sourceBucket ~= playerBucket then
@@ -484,11 +501,25 @@ Citizen.CreateThread(function()
484501
if not CachedPlayers[id].dropped and DoesPlayerHavePermission(source, "player.teleport.single") and CheckAdminCooldown(source, "teleport") then
485502
SetAdminCooldown(source, "teleport")
486503
local tgtPed = GetPlayerPed(id)
504+
if tgtPed == 0 then
505+
-- ped does not exist left or not loaded in yet
506+
TriggerClientEvent("EasyAdmin:showNotification", source, GetLocalisedText("playernotfound"))
507+
return
508+
end
509+
if id == source then
510+
return
511+
end
487512
local tgtCoords = GetEntityCoords(tgtPed)
513+
if tgtCoords.x == 0.00 and tgtCoords.y == 0.00 then
514+
-- loaded in but still spawning
515+
TriggerClientEvent("EasyAdmin:showNotification", source, GetLocalisedText("playernotfound"))
516+
return
517+
end
488518
local preferredWebhook = detailNotification ~= "false" and detailNotification or moderationNotification
489519
SendWebhookMessage(preferredWebhook,string.format(GetLocalisedText("teleportedtoplayer"), getName(source, false, true), getName(id, true, true)), "teleport", 16777214)
490520
TriggerClientEvent('EasyAdmin:TeleportRequest', source, id,tgtCoords)
491521
else
522+
TriggerClientEvent("EasyAdmin:showNotification", source, GetLocalisedText("playernotfound"))
492523
PrintDebugMessage('EASYADMIN FAILED TO TELEPORT'..source..' TO ID: '..id, 2)
493524
end
494525
end)

0 commit comments

Comments
 (0)