[NON-ESX] Trying to make a spectate script.

Hello! I’m here banging my head to my desk, just because I can’t seem to figure out, what is acting up.
So basically I’m calling the script from a command called “/spec”.
I want to have my camera anchored to the player, but it’s not working.

I tried a camera solution, but when I attach it to the player, it’s not attaching.
Now I am trying the NetworkSetInSpectatorMode() function, but it’s not working.

Can anybody help me?

Not sure how you are attaching the cam to the entity , the terrain will not load on high poly unless your ped is closer to it , so try getting the target player coords and then teleport yourself to the target coords and set yourself invisible and then attach the cam to his entity by getting his ped by server ID and position the cam accordingly … and by the way can i know for what you are trying to do this , so that i can suggest if there is any alternative way ?

You can do something like this:

-- Server

local spectating = {}

RegisterCommand("spectate", function(source, args, rawCommand)
	local target = args[1]
	local type = "1"
	if spectating[source] then type = "0" end
	TriggerEvent('erp_adminmenu:spectate', target, type == "1", source)
end)

AddEventHandler('erp_adminmenu:spectate', function(target, on, source)
	if IsPlayerAceAllowed(source, 'echorp.mod') and target then
		local tPed = GetPlayerPed(target)
		if DoesEntityExist(tPed) then
			if not on then
				TriggerClientEvent('erp_adminmenu:cancelSpectate', source)
				spectating[source] = false
				FreezeEntityPosition(GetPlayerPed(source), false)
			elseif on then
				TriggerClientEvent('erp_adminmenu:requestSpectate', source, NetworkGetNetworkIdFromEntity(tPed), target, GetPlayerName(target))
				spectating[source] = true
			end
		end
	end
end)

RegisterNetEvent('erp_adminmenu:spectate:teleport', function(target)
	local source = source
	if IsPlayerAceAllowed(source, 'echorp.mod') and spectating[source] then
		local ped = GetPlayerPed(target)
		if DoesEntityExist(ped) then
			local targetCoords = GetEntityCoords(ped)
			SetEntityCoords(GetPlayerPed(source), targetCoords.x, targetCoords.y, targetCoords.z - 10)
			FreezeEntityPosition(GetPlayerPed(source), true)
		end
	end
end)

-- Client

local oldPos = nil
local spectateInfo = { toggled = false, target = 0, targetPed = 0 }

RegisterNetEvent('erp_adminmenu:requestSpectate', function(targetPed, target, name)
  oldPos = GetEntityCoords(PlayerPedId())
  spectateInfo = {
    toggled = true,
    target = target,
    targetPed = targetPed
  }  
end)

RegisterNetEvent('erp_adminmenu:cancelSpectate', function()
  if NetworkIsInSpectatorMode() then
    NetworkSetInSpectatorMode(false, spectateInfo['targetPed'])
  end
  if not Cloack and not yayeetActive then
    SetEntityVisible(PlayerPedId(), true, 0)
  end
  spectateInfo = { toggled = false, target = 0, targetPed = 0 }
  RequestCollisionAtCoord(oldPos)
  SetEntityCoords(PlayerPedId(), oldPos)
  oldPos = nil;
end)

CreateThread(function()
  while true do 
      Wait(0)
      if spectateInfo['toggled'] then
          local text = {}
          local targetPed = NetworkGetEntityFromNetworkId(spectateInfo.targetPed)
          if DoesEntityExist(targetPed) then
            SetEntityVisible(PlayerPedId(), false, 0)
            if not NetworkIsInSpectatorMode() then
              RequestCollisionAtCoord(GetEntityCoords(targetPed))
              NetworkSetInSpectatorMode(true, targetPed)
            end
          else
            TriggerServerEvent('erp_adminmenu:spectate:teleport', spectateInfo['target'])
            while not DoesEntityExist(NetworkGetEntityFromNetworkId(spectateInfo.targetPed)) do Wait(100) end
          end
      else
          Wait(500)
      end
  end
end)

I’ll try this, imma be back in about an hour.

1 Like