Gaimo Posted February 22, 2019 Posted February 22, 2019 (edited) Estou bem feliz de conseguir criar o script quase que por completo, só olhando na wiki, mas não foi dessa vez... O que o script faz, ou deveria fazer, a cada um tempo aleatório, ele vai gerar uma bolsa de dinheiro, um marker e um blip. Se o jogador entrar no marker, ele recebe uma quantia aleatória de dinheiro e aqui encontra-se o problema, deveria deletar tudo, o objeto, marker e blip. as variáveis que criam os elementos (objeto, marker, blip) estão em uma função diferente de onde eu mando deletar. Segue o script: SERVER: local Gaimo = { -- Minutos time = { 5, 10, 15, 20}, pos = { -- x,y,z {1468.21350, -1738.90234, 13.54688}, {1473.89673, -1725.94067, 13.54688} }, money = { 1000, 2000, 3000 } } --(Gaimo.time[math.random(#Gaimo.time)] * 60000) function initialFunction() setTimer(function() mainFunction() end, 2000, 1) end addEventHandler("onResourceStart", resourceRoot, initialFunction) function mainFunction() local rndPos = Gaimo.pos[math.random(#Gaimo.pos)] local rndMoney = Gaimo.money[math.random(#Gaimo.money)] local marker = createMarker(rndPos[1], rndPos[2], (rndPos[3] - 0.6), "cylinder", 1.5, 0, 0, 0, 0) local blip = createBlipAttachedTo(marker, 0, 2, 255, 255, 255, 255, 0, 250) local object = createObject(1550, rndPos[1], rndPos[2], (rndPos[3] - 0.5), 0, 0, 0, true) outputChatBox("#00ff00[SERVER]: #ffffffUma bolsa com dinheiro foi perdida no mapa.", _,255, 255, 255, true) addEventHandler("onPlayerMarkerHit", getRootElement(), function(m) if m == marker and not isPedInVehicle(source) then --destroyElement(marker) --destroyElement(rndPos) --destroyElement(blip) --destroyElement(object) givePlayerMoney(source, rndMoney) outputChatBox("#00ff00[SERVER]: #ffffffParabéns #00ffe1" .. getPlayerName(source) .." #ffffffvocê encontrou: #00ff00R$" .. rndMoney, source, 255, 255, 255, true) initialFunction() end end) end Olhando o script percebi que não vai adiantar destroyElement, na verdade eu preciso arrumar tudo isso em um loop para que toda vez que o loop inicie faça tudo de novo certo? DEBUGSCRIPT > bad argument @ 'destroyElement' [Expected element at argument 1, got table] Edited February 22, 2019 by Gaimo
Other Languages Moderators androksi Posted February 22, 2019 Other Languages Moderators Posted February 22, 2019 Você colocou pra destruir "rndPos", sendo que rndPos é uma variável que tá armazenando posições. Tente substituir por isso: if ( marker and isElement( marker ) ) then destroyElement( marker ); end if ( blip and isElement( blip ) ) then destroyElement( blip ); end if ( object and isElement( object ) ) then destroyElement( object ); end Também, retire a palavra local antes de marker, blip e object. Se eu fosse você, passaria a usar o evento onMarkerHit ao invés de onPlayerMarkerHit. 1
Gaimo Posted February 22, 2019 Author Posted February 22, 2019 Uma dúvida, toda vez que a mainFunction for chamada, as variáveis vão receber um novo valor?
Other Languages Moderators Lord Henry Posted February 22, 2019 Other Languages Moderators Posted February 22, 2019 2 minutes ago, asrzk said: Se eu fosse você, passaria a usar o evento onMarkerHit ao invés de onPlayerMarkerHit. Explique o porquê disto. A diferença entre as funções. 1 minute ago, Gaimo said: Uma dúvida, toda vez que a mainFunction for chamada, as variáveis vão receber um novo valor? Sim. Todas as variáveis locais receberão novos valores. 1
Other Languages Moderators androksi Posted February 22, 2019 Other Languages Moderators Posted February 22, 2019 1 minute ago, Lord Henry said: Explique o porquê disto. A diferença entre as funções. É uma opção pessoal. Prefiro usar onMarkerHit pois você pode já anexar o marker neste evento, acho que facilitaria pra ele e deixaria mais otimizado. Já no onPlayerMarkerHit não é possível anexar um marker, você tem que adicionar o evento como root e, dentro do evento, verificar os marcadores. 1 1
Gaimo Posted February 22, 2019 Author Posted February 22, 2019 Testando com o timer por 2 segundos funcionou perfeitamente!!! Mas vem minha dúvida se eu usar isso no timer: (Gaimo.time[math.random(#Gaimo.time)] * 60000) O timer vai usar isso uma vez ou sempre que a função dele for chamada ele ganha um novo tempo? Script completo: local Gaimo = { -- Minutos time = { 5, 10, 15, 20}, pos = { -- x,y,z {1468.21350, -1738.90234, 13.54688}, {1473.89673, -1725.94067, 13.54688} }, money = { 1000, 2000, 3000 } } function initialFunction() setTimer(function() mainFunction() end, (Gaimo.time[math.random(#Gaimo.time)] * 60000), 1) end addEventHandler("onResourceStart", resourceRoot, initialFunction) function mainFunction() rndPos = Gaimo.pos[math.random(#Gaimo.pos)] rndMoney = Gaimo.money[math.random(#Gaimo.money)] marker = createMarker(rndPos[1], rndPos[2], (rndPos[3] - 0.6), "cylinder", 1.5, 0, 0, 0, 0) blip = createBlipAttachedTo(marker, 0, 2, 255, 255, 255, 255, 0, 250) object = createObject(1550, rndPos[1], rndPos[2], (rndPos[3] - 0.5), 0, 0, 0, true) outputChatBox("#00ff00[SERVER]: #ffffffUma bolsa com dinheiro foi perdida no mapa.", _,255, 255, 255, true) addEventHandler("onMarkerHit", marker, function(thePlayer, dim) if (getElementType(thePlayer) == "player") and not isPedInVehicle(thePlayer) and dim then if ( marker and isElement( marker ) ) then destroyElement( marker ); end if ( blip and isElement( blip ) ) then destroyElement( blip ); end if ( object and isElement( object ) ) then destroyElement( object ); end givePlayerMoney(thePlayer, rndMoney) outputChatBox("#00ff00[SERVER]: #ffffffParabéns #00ffe1" .. getPlayerName(thePlayer) .." #ffffffvocê encontrou: #00ff00R$" .. rndMoney, thePlayer, 255, 255, 255, true) initialFunction() end end) end E na ideia de OTIMIZAÇÃO, ficaria mais otimizado se eu colocasse as variáveis: rndPos = Gaimo.pos[math.random(#Gaimo.pos)] rndMoney = Gaimo.money[math.random(#Gaimo.money)] marker = createMarker(rndPos[1], rndPos[2], (rndPos[3] - 0.6), "cylinder", 1.5, 0, 0, 0, 0) blip = createBlipAttachedTo(marker, 0, 2, 255, 255, 255, 255, 0, 250) object = createObject(1550, rndPos[1], rndPos[2], (rndPos[3] - 0.5), 0, 0, 0, true) Dentro de uma tabela??
Other Languages Moderators androksi Posted February 22, 2019 Other Languages Moderators Posted February 22, 2019 (edited) 4 minutes ago, Gaimo said: O timer vai usar isso uma vez ou sempre que a função dele for chamada ele ganha um novo tempo? Quando a função for chamada, um novo tempo será definido. Pelo o que eu vejo na sua tabela: 5, 10, 15 ou 20 minutos. 4 minutes ago, Gaimo said: E na ideia de OTIMIZAÇÃO, ficaria mais otimizado se eu colocasse as variáveis: Opcional. Não irá fazer muita diferença neste caso. Edited February 22, 2019 by asrzk 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now