Player become a spectator


#1

Hi,

I try to make players can spectate a random player

There i my code:

plyNumber = GetNumberOfPlayers()
local function spectatePlayer()
	local players = {}

	for i = 0, 31 do
        if NetworkIsPlayerActive( i ) then
            table.insert( players, i )
        end
    end
    for k, v in pairs( players ) do
    	local aPlayer = GetRandomIntInRange(v, plyNumber)
    	if IsPedSittingInAnyVehicle(GetPlayerPed(aPlayer)) then
    		NetworkSetInSpectatorMode(true, GetPlayerPed(aPlayer))
    	end
	end
end

But that not work, when I call the function, nothing happen :confused:

Thanks!


#2

Here is some C++ code you can use as reference:

else if (GetMenuEntry() == 3) // spectate player
		{
			if (!NETWORK::NETWORK_IS_PLAYER_CONNECTED(g_online_selectedPlayer))
			{
				DrawToast("Player isn't connected.");
				g_isSpectating = false;
			}

			if (g_isSpectating)
			{
				if (!CAM::IS_SCREEN_FADED_OUT())
				{
					if (!CAM::IS_SCREEN_FADING_OUT())
					{
						CAM::DO_SCREEN_FADE_OUT(1000);

						while (!CAM::IS_SCREEN_FADED_OUT()) WAIT(0);

						STREAMING::REQUEST_COLLISION_AT_COORD(targetPos.x, targetPos.y, targetPos.z);
						NETWORK::NETWORK_SET_IN_SPECTATOR_MODE(1, g_online_selectedPlayer);

						if (CAM::IS_SCREEN_FADED_OUT())
						{
							CAM::DO_SCREEN_FADE_IN(1000);
						}
					}
				}

				DrawToast((char*)va("Spectating ~b~<C>%s</C>.", PLAYER::GET_PLAYER_NAME(g_online_selectedPlayer)));
			}
			else
			{
				if (!CAM::IS_SCREEN_FADED_OUT())
				{
					if (!CAM::IS_SCREEN_FADING_OUT())
					{
						CAM::DO_SCREEN_FADE_OUT(1000);

						while (!CAM::IS_SCREEN_FADED_OUT()) WAIT(0);

						STREAMING::REQUEST_COLLISION_AT_COORD(playerPos.x, playerPos.y, playerPos.z);
						NETWORK::NETWORK_SET_IN_SPECTATOR_MODE(0, g_online_selectedPlayer);

						if (CAM::IS_SCREEN_FADED_OUT())
						{
							CAM::DO_SCREEN_FADE_IN(1000);
						}
					}
				}
			}

How to make a 'spectate a player' function in scripthook?
#3

Thank you!

I don’t know so much in C++, So, I tied to convert your code, and I have this:

local g_isSpectating = GetPlayerPed(g_online_selectedPlayer)
local players = {}

function ()
	for i = 0, 31 do
        if NetworkIsPlayerActive( i ) then
            table.insert( players, i )
            local g_online_selectedPlayer = NetworkIsPlayerActive( i )
        end
	end
	if not NetworkIsPlayerConnected(g_online_selectedPlayer) then
		print("Player isn't connected.")
		g_isSpectating = false;
	end
	if (g_isSpectating) then
		if not (IsScreenFadedOut()) then
			if not (IsScreenFadingOut()) then
					DoScreenFadeOut(1000);
						while not (IsScreenFadedOut()) then 
							Wait(0);

							RequestCollisionAtCoord(GetEntityCoords(GetPlayerPed(g_online_selectedPlayer), 1))
							NetworkSetInSpectatorMode(1,  g_online_selectedPlayer)
							print("Spectating ~b~"..GetPlayerName(g_online_selectedPlayer))
								if (IsScreenFadedOut())
									DoScreenFadeIn(1000)
								end
						end
			end
		end
				
	else
		if not (IsScreenFadedOut()) then
			if not (IsScreenFadingOut()) then
				DoScreenFadeOut()(1000)
					while not (IsScreenFadedOut()) then
						Wait(0);

						RequestCollisionAtCoord(GetEntityCoords(GetPlayerPed(g_online_selectedPlayer), 1))
						NetworkSetInSpectatorMode(0,  g_online_selectedPlayer)
							if (IsScreenFadedOut()) then
								DoScreenFadeIn(1000);
							end
					end
			end
		end
	end
end

Look good for you? I’m a noob in coding, so…

Anyway, it’s helpfull, thank you


#4

so did you manage to get it work ?


#5

I will not explain how all that work because that will be too long, it’s only for learning:

local function spectatePlayer()
    endScreen = false
    spectate = true
    FreezeEntityPosition(GetPlayerPed(-1),  true)
    SetPlayerWantedLevel(PlayerId(), 0, false)
    SetPlayerWantedLevelNow(PlayerId(), false)
    RequestCollisionAtCoord(GetEntityCoords(GetPlayerPed(plyToSpec), 1))
    NetworkSetInSpectatorMode(1, GetPlayerPed(plyToSpec))
    print("Spectating ~b~"..GetPlayerName(plyToSpec))
    while true do
        Citizen.Wait(0)
        if spectate then
            if IsControlJustPressed(1,190) and plyRun < #players then --Right Arrow
                FadingOut(500)
                plyRun = plyRun + 1 
                plyToSpec = players[plyRun]
                if IsPedSittingInAnyVehicle(GetPlayerPed(plyToSpec)) then 
                    FreezeEntityPosition(GetPlayerPed(-1),  true)
                    SetPlayerWantedLevel(PlayerId(), 0, false)
                    SetPlayerWantedLevelNow(PlayerId(), false)
                    RequestCollisionAtCoord(GetEntityCoords(GetPlayerPed(plyToSpec), 1))
                    NetworkSetInSpectatorMode(1, GetPlayerPed(plyToSpec))
                    DrawMissionText("Spectating ~b~"..GetPlayerName(plyToSpec), 10000)
                end
                FadingIn(500)
            end

            if IsControlJustPressed(1,189) and plyRun > 0 then --Left Arrow
                FadingOut(500)
                plyRun = plyRun - 1 
                plyToSpec = players[plyRun]
                if IsPedSittingInAnyVehicle(GetPlayerPed(plyToSpec)) then 
                    FreezeEntityPosition(GetPlayerPed(-1),  true)
                    SetPlayerWantedLevel(PlayerId(), 0, false)
                    SetPlayerWantedLevelNow(PlayerId(), false)
                    RequestCollisionAtCoord(GetEntityCoords(GetPlayerPed(plyToSpec), 1))
                    NetworkSetInSpectatorMode(1, GetPlayerPed(plyToSpec))
                    DrawMissionText("Spectating ~b~"..GetPlayerName(plyToSpec), 10000)
                end
                FadingIn(500)
            end
            if not runInProgress then
                FreezeEntityPosition(GetPlayerPed(-1),  false)
                SetPlayerWantedLevel(PlayerId(), 0, false)
                SetPlayerWantedLevelNow(PlayerId(), false)
                RequestCollisionAtCoord(GetEntityCoords(GetPlayerPed(-1), 1))
                NetworkSetInSpectatorMode(0, GetPlayerPed(-1))
                spectate = false
            end
        end
    end
end

as you can see, that freeze player and RequestCollisionAtCoord() let active player view another player. This code have a bug because I write it before understand that plyerID always change and not follow each other… (I let you see that)

If you have question, I probably ask them, but not quickly wiil depend the question


#6

I have a question, how do you activate the spec mod ? and how do you deactivate it ?


#7

this would be a great feature to implement permissions, perfect for servers that have scripthook disabled like mine… I really need this, maybe someone will make an admin menu, that is set with permissions!


#8

It’s a function, so, I call it when I need it :wink: (in a menu for exemple, or by an event)

In this case, I juste have to change “runInProgress” to true and the player become the player egain (it’s a variable in the script)

@NYKILLA1127 I have not anought time than I have past weeks, so, I can’t do it, you have to do it by yourself :wink:


#9

i’m working on it if i do it i’m gonna share this one too =)