Jump to content

Porque o erro só ocorre em host?


Recommended Posts

  • Other Languages Moderators

Olá, tenho este script que funciona perfeitamente em server local, sem bugs nem erros e sem lag.

Spoiler

--Original file by: LordHenry

theTimer = 60000 * tonumber (get("*respawnTimer"))
vehicles = getElementsByType ( "vehicle" )

timerGlobal = setTimer (function ()
	inicialTimer = getTickCount ()
	for k,v in ipairs(vehicles) do --Recebe todos os veículos do server, incluindo os veículos criados por mapas.
		local ocupante0 = getVehicleOccupant (v, 0) --Recebe o ocupante do assento 0 do veículo. (será false se não tiver ninguém ou se o assento não existir)
		local ocupante1 = getVehicleOccupant (v, 1) --Faz o mesmo com os outros.
		local ocupante2 = getVehicleOccupant (v, 2)
		local ocupante3 = getVehicleOccupant (v, 3)
		local ocupante4 = getVehicleOccupant (v, 4)
		local ocupante5 = getVehicleOccupant (v, 5)
		local ocupante6 = getVehicleOccupant (v, 6)
		local ocupante7 = getVehicleOccupant (v, 7)
		local ocupante8 = getVehicleOccupant (v, 8) --O ônibus tem 9 assentos, por isso q coloquei até 8. (0 até 8 = 9)
		if ocupante0 == false and ocupante1 == false and ocupante2 == false and ocupante3 == false and ocupante4 == false and ocupante5 == false and ocupante6 == false and ocupante7 == false and ocupante8 == false then
			if isVehicleStatic(v) then --Se for um veículo criado por mapa, então:
				setVehicleDoorState(v,0,0)
				setVehicleDoorState(v,1,0)
				setVehicleDoorState(v,2,0) --Fecha todas as portas do veículo.
				setVehicleDoorState(v,3,0)
				setVehicleDoorState(v,4,0)
				setVehicleDoorState(v,5,0)
				respawnVehicle (v) --Respawna o veículo para a posição em que foi criado no mapa.
			else --Se for um veículo comum, então:
				destroyElement (v) --Destroi o veículo.
			end
		end --Se tiver alguém dentro do veículo em qualquer assento, nada acontece com ele.
	end
	outputChatBox ("Todos os veículos não ocupados foram destruídos. Próxima limpeza em "..tostring (get("*respawnTimer")).." minutos.", root, 255, 150, 0)
end, theTimer, 0) --1 hora = 3600000 | Por padrão executa isso a cada 1 hora, isso pode ser alterado nas settings do resource.

timerVerifier = setTimer (function () --Isso só serve pra avisar os jogadores 1 minuto antes de limpar o mapa.
	i1, i2, i3 = getTimerDetails (timerGlobal)
	if i1 <= 60000 and i1 >= 59000 then
		outputChatBox (" ")
		outputChatBox ("Todos os veículos não ocupados serão destruídos em 1 minuto.", root, 255, 150, 0)
		outputChatBox (" ")
	end
end, 1000, 0)

function onVehicleExplodeHandler() --Respawna ou destroi o veículo depois de 10 segundos da sua explosão.
	if isElement (source) then
		if isVehicleStatic(source) then --Se o veículo for de mapa, respawna ele.
			setTimer (setVehicleDoorState, 9900, 1, source, 0, 0) --Arruma o estado das portas instantes antes de respawná-lo para que ele não respawne com as portas abertas.
			setTimer (setVehicleDoorState, 9900, 1, source, 1, 0)
			setTimer (setVehicleDoorState, 9900, 1, source, 2, 0)
			setTimer (setVehicleDoorState, 9900, 1, source, 3, 0)
			setTimer (setVehicleDoorState, 9900, 1, source, 4, 0)
			setTimer (setVehicleDoorState, 9900, 1, source, 5, 0)
			setTimer (respawnVehicle, 10000, 1, source)
		else
			setTimer (destroyElement, 10000, 1, source) --Se for um veículo comum, destroi ele.
		end
	end
end
addEventHandler( "onVehicleExplode", getRootElement(), onVehicleExplodeHandler)

function isVehicleStatic (theVehicle) --Isso verifica se o veículo é de mapa ou não.
	if isElement(theVehicle) and getElementType(theVehicle) == "vehicle" then
		if getElementID(theVehicle):find("server_") then --Se o veículo tem escrito "server_" no seu ID, então é de mapa.
			return true
		end
	end
	return false --Senão é um veículo comum.
end

 

Porém, se eu uso ele em servidor na host, com vários jogadores online. Ele causa um lag absurdo que chega até a causar Network Trouble toda vez que ele faz a limpeza do mapa.
O problema é que ele gera erros nada a ver e não consigo descobrir o motivo:

Linha: 29 Bad argument @ 'destroyElement' [Expected element at argument 1]
Linha: 10 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 11 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 12 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 13 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 14 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 15 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 16 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 17 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]
Linha: 18 Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1]

Obs: Se eu coloco um verificador antes disso, ele não faz nada e não deleta nenhum veículo.

Spoiler

for k,v in ipairs(vehicles) do
    if isElement (v) then --Verificador.
        local ocupante0 = getVehicleOccupant (v, 0)
        local ocupante1 = getVehicleOccupant (v, 1)
        local ocupante2 = getVehicleOccupant (v, 2)
        local ocupante3 = getVehicleOccupant (v, 3)
        local ocupante4 = getVehicleOccupant (v, 4)
        local ocupante5 = getVehicleOccupant (v, 5)
        local ocupante6 = getVehicleOccupant (v, 6)
        local ocupante7 = getVehicleOccupant (v, 7)
        local ocupante8 = getVehicleOccupant (v, 8)
        if ocupante0 == false and ocupante1 == false and ocupante2 == false and ocupante3 == false and ocupante4 == false and ocupante5 == false and ocupante6 == false and ocupante7 == false and ocupante8 == false then
            if isVehicleStatic(v) then
                setVehicleDoorState(v,0,0)
                setVehicleDoorState(v,1,0)
                setVehicleDoorState(v,2,0)
                setVehicleDoorState(v,3,0)
                setVehicleDoorState(v,4,0)
                setVehicleDoorState(v,5,0)
                respawnVehicle (v)
            else
                destroyElement (v)
            end
        end
    end
end

 

O que pode estar ocorrendo?

Edited by Lord Henry
Link to comment

Você precisa definir a variável Vehicles dentro da função você a colocou fora da função e ela só recebe um/o valor quando o resource e iniciado ou seja ele só terá o valor dos veículos criados antes da resource ser iniciada. apenas move-lá para dentro da sua função !

  • Thanks 1
Link to comment
  • Other Languages Moderators
2 hours ago, raynner said:

Você precisa definir a variável Vehicles dentro da função você a colocou fora da função e ela só recebe um/o valor quando o resource e iniciado ou seja ele só terá o valor dos veículos criados antes da resource ser iniciada. apenas move-lá para dentro da sua função !

Mas então pq não ocorre erro nenhum em server local? Deveria ocorrer esse erro tbm.

Edited by Lord Henry
Link to comment
  • Other Languages Moderators

Bom, de qualquer forma obrigado. Resolveu o problema do script. Tome este REP+

Estou com outro resource com um problema parecido, ele não gera erro nenhum só que ele deveria funcionar somente no jogador que colidiu no marker, só que está funcionando com todos os jogadores. O resource movimenta a câmera do jogador por lugares pré definidos enquanto mostra textos dos créditos do servidor na tela.
Só que ele faz isso com todos os jogadores, só que deveria fazer somente com o player que colidiu no marker.
O script inteiro é client-side.
 

Spoiler

local me	= getLocalPlayer()
local scX, scY	= guiGetScreenSize()

--(Várias funções de texto e imagem aqui que serão acessadas durante os créditos.)

addEventHandler ("onClientResourceStart", resourceRoot, function(me)
	theMarker = createMarker (-2091.6, 2313.7, 25, "cylinder", 2, 0, 0, 0, 150) --Cria o marker preto.
	createBlipAttachedTo (theMarker, 0, 1, 0, 0, 0, 255, 0, 500) --Cria um blip preto e anexa no marker.
	
	closeButton = guiCreateButton (scX-40, scY-45, 30, 30, "X", false) --Cria o botão
	guiSetVisible (closeButton, false) --Oculta o botão.
	
	addEventHandler ("onClientMarkerHit", theMarker, function (hitElement) --Faz o esquema todo ao colidir no marker.
		if hitElement and getElementType (hitElement) == "player" and not getPedOccupiedVehicle (hitElement) then --Se o elemento que colidiu for um player e estiver sem veículo, faz o esquema todo acontecer.
			setElementData (hitElement, "cinematic", true) --Use isso para desativar temporariamente o hud modificado do jogador. Você precisa programar isso no resource do hud.
			local theTarget = {} --Objeto invisivel que a câmera irá olhar.
			local theCam = {} --Objeto invisivel que a câmera irá seguir.
			local theTimer1 = {} --Temporizador 1.
			local theTimer2 = {} --Temporizador 2.
			local theTimer3 = {} --Temporizador 3.
          
			local components = {} --Componentes do HUD.
			local componentes = {} --Componentes ativos do HUD.
			local isText = {} --Verificador se algum texto dos créditos está visível.
			
			components[hitElement] = {
			[1] = "ammo",
			[2] = "area_name",
			[3] = "armour",
			[4] = "breath",
			[5] = "clock",
			[6] = "health",
			[7] = "money",
			[8] = "radar",
			[9] = "vehicle_name",
			[10] = "weapon",
			[11] = "radio",
			[12] = "wanted",
			[13] = "crosshair"
			}
			componentes[hitElement] = { --Se um elemento da hud estiver visivel, ficará true abaixo.
			[1] = false,
			[2] = false,
			[3] = false,
			[4] = false,
			[5] = false,
			[6] = false,
			[7] = false,
			[8] = false,
			[9] = false,
			[10] = false,
			[11] = false,
			[12] = false,
			[13] = false
			}
			for k,component in ipairs(components[hitElement]) do --Verifica o hud do jogador, ocultando somente os componentes visíveis.
				if isPlayerHudComponentVisible(component)then
					-- outputChatBox("Hud Component: "..component.." is visible!")
					setPlayerHudComponentVisible (component, false)
					componentes[hitElement][k] = true
				else
					-- outputChatBox("Hud Component: "..component.." is not visible!")
					componentes[hitElement][k] = false
				end
			end
			
			function stopCredits () --Função do botão, que faz parar os créditos quando o jogador clicar nele. Executa isso automaticamente quando termina os créditos.
				removeEventHandler ("onClientRender", getRootElement(), moveCamera) --Para de movimentar a câmera.
				setCameraTarget (hitElement) --Coloca a câmera devolta no jogador.
				setTimer (setElementFrozen, 1000, 1, hitElement, false) --Depois de 1 segundo, descongela o jogador.
				fadeCamera (true) --Reativa a visão do jogador.
				if isElement (theSound) then --Se o som dos créditos ainda estiver tocando, então:
					stopSound (theSound) --Para o som.
				end
				if isElement (theCam[hitElement]) then --Se existir o objeto que a câmera estava seguindo, então:
					destroyElement (theCam[hitElement]) --Destroi esse objeto.
				end
				if isElement (theTarget[hitElement]) then --O mesmo com o objeto que a camera estava olhando.
					destroyElement (theTarget[hitElement])
				end
				showChat (true) --Mostra o chat devolta.
				showCursor (false) --Oculta o cursor novamente, que foi usado pra acessar o botão.
				guiSetInputEnabled (false) --Reabilita as binds.
				guiSetVisible (closeButton, false) --Oculta o botão.
				for key,component in ipairs(components[hitElement])do --Torna visiveis somente os componentes que antes estavam visiveis do hud.
					if componentes[hitElement][key] == true then
						setPlayerHudComponentVisible (component, true)
					end
				end
				if isTimer (theTimer1[hitElement]) then --Se existe o temporizador 1 funcionando, então:
					killTimer (theTimer1[hitElement]) --Cancela esse timer.
					-- outputChatBox ("Kilou o timer1")
				end
				if isTimer (theTimer2[hitElement]) then --O mesmo com o 2.
					killTimer (theTimer2[hitElement])
					-- outputChatBox ("Kilou o timer2")
				end
				if isTimer (theTimer3[hitElement]) then --O mesmo com o 3.
					killTimer (theTimer3[hitElement])
					-- outputChatBox ("Kilou o timer3")
				end
				setElementData (hitElement, "cinematic", false) --Coloca essa data como 'false' para tornar o hud editado visível novamente.
				if isText[hitElement] == true then --Se existir algum texto dos créditos na tela.
					removeEventHandler ("onClientRender", getRootElement(), renderTxIn) --Para de mostrar o texto parte 1.
					removeEventHandler ("onClientRender", getRootElement(), renderTxIn2) --Para de mostrar o texto parte 2.
				end
				return --Para de fazer qualquer outra coisa. (aqui não sei se para nos outros clientes, a principio não deveria influenciar outros clientes)
			end
			addEventHandler ("onClientGUIClick", closeButton, stopCredits)

          --Aqui começa os créditos.
			theSound = playSound ("GTA-SA-Cover.mp3") --Toca essa música.
			fadeCamera (false, 1.0, 0, 0, 0) --Fade a camera para preto.
			showChat (false) --Oculta o chat.
			guiSetInputEnabled (false) --Desativa as binds tipo o T e o Y.
			theTimer1[hitElement] = setTimer (function () --Depois de 3 segundos, faz isso:
				fadeInDX (0, 0, scX, scY, "Lord Logo.png", 0, 0, 0, false, 2000, "Linear") --Mostra essa logo em fade.
				theTimer2[hitElement] = setTimer (function () --Depois de mais 1 segundo, faz isso:
					isText[hitElement] = true --Indica que há um texto na tela.
					fadeInTX ("credits by", scX/2-200, 20, scX/2+200, 100, 255, 150, 0, 2, "beckett", "center", "top", true, true, true, 1000, 2, "Linear") --Faz fadeIn no texto.
					theTimer1[hitElement] = setTimer (function () --Depois de mais 3 segundos, faz isso:
						fadeOutTX ("credits by", scX/2-200, 20, scX/2+200, 100, 255, 150, 0, 2, "beckett", "center", "top", true, true, true, 1000, 2, "Linear") --Faz fadeOut no texto.
						isText[hitElement] = false --Indica que não há texto na tela.
					end, 3000, 1)
				end, 1000, 1)
				setElementFrozen (hitElement, true) --Congela o jogador logo depois que fez fadeIn naquela logo.
			end, 3000, 1)
			theTimer2[hitElement] = setTimer (function () --Depois de 9 segundos, faz isso:
				fadeOutDX (0, 0, scX, scY, "Lord Logo.png", 0, 0, 0, false, 1000, "Linear") --Faz fadeOut naquela logo.
				guiSetVisible (closeButton, true) --Mostra o botão pra cancelar os créditos no canto da tela.
				showCursor (true) --Mostra o cursor pra ser possível acessar o botão.
			end, 9000, 1)
			--(Mais um monte de scripts, textos e movimentações de câmera até a música acabar.)
			fadeCamera (false, 2.0, 0, 0, 0) --Escurece a câmera pra preto em 2 segundos.
			theTimer2[hitElement] = setTimer (function() --Depois de 2 segundos e meio:
				stopCredits () --Para tudo e finaliza.
			end, 2500, 1)
		end
	end)
end)

 

O script não tem erros e funciona perfeitamente tanto no server local como na host, só que na host ele ocorre com todos os jogadores, mas deveria acontecer somente com o jogador que colidiu no marker preto.

Edited by Lord Henry
Link to comment
  • Moderators
5 hours ago, Lord Henry said:

Bom, de qualquer forma obrigado. Resolveu o problema do script. Tome este REP+

Devia ser isso mesmo que o Raynner disse, porque você usou getElementsByType um tempo antes (1 hora) de quando a função do setTimer deveria executar, e o recomendado é usar no mesmo momento da execução.

 

Sobre o outro problema, lá no onClientMarkerHit, coloque uma condição hitElement == localPlayer, parece que por algum motivo, esse evento é acionado pra todos jogadores se me lembro bem.

  • Thanks 1
Link to comment
  • Other Languages Moderators

Assim?

local me	= getLocalPlayer()
local scX, scY  = guiGetScreenSize()
--[...]

addEventHandler ("onClientResourceStart", resourceRoot, function(me)
    theMarker = createMarker (-2091.6, 2313.7, 25, "cylinder", 2, 0, 0, 0, 150)
    createBlipAttachedTo (theMarker, 0, 1, 0, 0, 0, 255, 0, 200)

    closeButton = guiCreateButton (scX-40, scY-45, 30, 30, "X", false)
    guiSetVisible (closeButton, false)
	
    addEventHandler ("onClientMarkerHit", theMarker, function (hitElement)
        if hitElement == me and getElementType (hitElement) == "player" and not getPedOccupiedVehicle (hitElement) then
        --[...]
        end
    end)
end)

 

Edited by Lord Henry
Link to comment

realmente você só esqueceu de colocar hitElement == localPlayer na linha 15 eu já ia falar isso porém quando cliquei no reapli vi que já haviam respondido :D mais assim vou dizer oque eu ia falar eu n li seu codigo todo pq é mt coisa porém como vc falou que o erro é que ta fazendo para todos o erro só pode ser quando hitElement recebe seu valor então penas add um.

hitElement == localPlayer deve resolver '-' no seu caso trocar localPlayer por me ;)

Link to comment
  • Other Languages Moderators

Esqueci de avisar que deu certo. Obrigado a todos.
Ficou assim:

Spoiler

local me	= getLocalPlayer()
local scX, scY  = guiGetScreenSize()

--[...]

addEventHandler ("onClientResourceStart", resourceRoot, function(me)
    theMarker = createMarker (-2091.6, 2313.7, 25, "cylinder", 2, 0, 0, 0, 150)
    createBlipAttachedTo (theMarker, 0, 1, 0, 0, 0, 255, 0, 200)
	
    closeButton = guiCreateButton (scX-40, scY-45, 30, 30, "X", false)
    guiSetVisible (closeButton, false)
	
    addEventHandler ("onClientMarkerHit", theMarker, function (hitElement)
        if hitElement == localPlayer and getElementType (hitElement) == "player" and not getPedOccupiedVehicle (hitElement) then
            --[...]
        end
    end)
end)

 

[CLOSED]

  • Like 1
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...