Jump to content

Having problems, please a good scripter


|H|TiTanium

Recommended Posts

The above code is supposed to get Ped's health and show it in a progressBar( which should be above its head ). It doesn't work, I don't get anything from debugscript, but it doesn't work, progressbar is not created...

If you know what is wrong, please tell me and help me fixing, tigre1 is created server-side ( using slothbot ). Probably if you find any error, it's not the only one, so try to help me, please.

local rootElement = getRootElement() 
function PETvida() 
    if ( isElement ( tigre1 ) ) then 
    local pedX, pedY = getElementPosition ( tigre1 ) 
    if ( isElement ( tigre1 ) ) then 
    local vida = getElementHealth ( tigre1 ) 
    local sx, sy = getScreenFromWorldPosition (getElementPosition(tigre1)) 
        if sx then 
        life = guiCreateProgressBar( pedX, pedY+5, sx, sy, true, nil ) -- Made pedY+5 to make it above Ped's health and not on its body 
        guiProgressBarSetProgress(life, tonumber (vida)) 
    end 
    end 
    end 
end 
  
function HandleTheRendering() 
    addEventHandler("onClientRender",rootElement, PETvida) 
end 
addEventHandler("onBotSpawned",rootElement, HandleTheRendering) 

Link to comment

Even if they are global ? I thought that, but I wasn't sure of how do I do, I'm starting to give up.

If you have any tips about how do I do dxDrawRectangle using a server-side variable, please tell me. How do I "share" variables between both sides. Also check this please, tell me if the only thing is wrong is the variable ( I didn't get element health yet ) :

local rootElement = getRootElement() 
local x,y = guiGetScreenSize() 
function PETvida() 
    local sx, sy = getScreenFromWorldPosition (getElementPosition(tigre1)) 
        if sx then 
        dxDrawRectangle( sx, sy, x/2.02, y/2, tocolor ( 0, 0, 0, 150 ) ) 
    end 
end 
  
function HandleTheRendering() 
    addEventHandler("onClientRender",rootElement, PETvida) 
end 
addEventHandler("onBotSpawned",rootElement, HandleTheRendering) 

Link to comment

Client side :

local x,y = guiGetScreenSize() 
function PETvida() 
    local sx, sy = getScreenFromWorldPosition (getElementPosition(tigre1)) 
        if sx then 
        dxDrawRectangle( sx, sy, x/2.02, y/2, tocolor ( 0, 0, 0, 150 ) ) 
    end 
end 
addEvent( "onGetHealth", true ) 
addEventHandler( "onGetHealth", getRootElement(), PETvida ) 

Server side(I think showing the whole function is not needed):

tigre1 = call (getResourceFromName("slothbot"), "spawnBot", x+3, y+1, z, rot, skin, interior, dimension, team, weapon, mode, modesubject) 
setElementData ( tigre1, "pet", cachorro ) 
setElementExtraHealth  ( tigre1, 999 ) 
triggerClientEvent ( "onGetHealth", getRootElement(), tigre1 ) 
else 
outputChatBox("Você não tem dinheiro para criar um PET", thePlayer, 225, 0, 0) 
    end 
end 

Is it going to work, so I would "share" variables ... and the first code, is it right ?

Link to comment
Even if they are global ? I thought that, but I wasn't sure of how do I do, I'm starting to give up.

If you have any tips about how do I do dxDrawRectangle using a server-side variable, please tell me. How do I "share" variables between both sides. Also check this please, tell me if the only thing is wrong is the variable ( I didn't get element health yet ) :

local rootElement = getRootElement() 
local x,y = guiGetScreenSize() 
function PETvida() 
    local sx, sy = getScreenFromWorldPosition (getElementPosition(tigre1)) 
        if sx then 
        dxDrawRectangle( sx, sy, x/2.02, y/2, tocolor ( 0, 0, 0, 150 ) ) 
    end 
end 
  
function HandleTheRendering() 
    addEventHandler("onClientRender",rootElement, PETvida) 
end 
addEventHandler("onBotSpawned",rootElement, HandleTheRendering) 

It is not directly possible to share variables between the client and server sides because they run on different Lua states, running on different machines (except when you are both hosting the server and running the client, yet they run on different states). The concept of client and server-side scripts is that the client ones are transfered over to the client (the client is what you use to join a server), while server-side scripts are loaded and run on the server, which is why you have to download client-side scripts and create GUI elements on the client side.

Link to comment
You can't make a 3D progress bar using GUI functions, you must draw it using dxDrawRectangle.

You actually can:

Client:

local screenX, screenY = guiGetScreenSize() 
local sizeX = screenX*(100/1920) 
  
  
  
local rootElement = getRootElement() 
function PETvida() 
    if ( isElement ( tigre1 ) ) then 
local pedX, pedY, pedZ = getElementPosition ( tigre1 ) 
local plX, plY, plZ = getElementPosition ( localPlayer ) 
local visibleto = getDistanceBetweenPoints3D(pedX, pedY, pedZ,plX, plY, plZ) 
if visibleto < 45 then 
local headX, headY, headZ = getPedBonePosition( tigre1, 5 ) 
local sx, sy = getScreenFromWorldPosition (headX, headY, headZ+0.6) 
        if sx then 
guiSetPosition ( life,  sx-((sizeX-visibleto)/2), sy, false ) 
guiSetSize (life, sizeX-visibleto, screenY*((30-visibleto)/1080), false) 
    local vida = getElementHealth ( tigre1 ) 
        guiProgressBarSetProgress(life, tonumber (vida)) 
end 
end 
end 
end 
  
  
function HandleTheRendering() 
        life = guiCreateProgressBar( 0, 0,0, 0, false, nil ) 
addEventHandler("onClientRender",rootElement, PETvida) 
end 
addEventHandler("onBotSpawned",rootElement, HandleTheRendering) 
  

Now you only need to send the element to the client like this:

client:

local clientElement = nil 
  
addEvent ( "get:element", true ) 
addEventHandler ( "get:element", root, function(element) 
clientElement = element 
end) 
  

Server:

Lped = createPed ( 0,0,0,1) 
  
function sendServer(playerToSendTo) 
triggerClientEvent (playerToSendTo, "get:element", root, Lped ) 
end 

This will send the server element "Lped" to the client, in that way you can check his health.

If you need explanation with these functions tell it here.

But dx rectangles will look nicer, and it it's faster for a client pc.

Regards,

Danny

Link to comment

As I said, yes, but you still need to edit the code foryourself

Client

local x, y = guiGetScreenSize() 
local tigre1 = nil 
  
addEventHandler("onClientRender",getRootElement(), 
function () 
if ( isElement ( tigre1 ) ) then 
local mx, my, mz = getElementPosition(tigre1) 
local mcx, mcy, mcz = getCameraMatrix() 
if isLineOfSightClear ( mcx , mcy, mcz, mx, my, mz, true, false, false, true, false, true, false, getLocalPlayer ( )) then 
local x1,y1,z1 = getElementPosition (getLocalPlayer()) 
local x2,y2,z2 = getElementPosition (tigre1) 
local visibleto = getDistanceBetweenPoints3D(x1,y1,z1,x2,y2,z2) 
if visibleto < 75 then 
local sx,sy = getScreenFromWorldPosition ( x2,y2,z2+1.45 ) 
if sx and sy then 
if y/102-visibleto/5 > 0 then 
dxDrawRectangle ( sx-x/14/2+visibleto/2, sy+y/25-visibleto/3, x/14-visibleto,y/146.3-visibleto/10, tocolor(0,0,0,245) ) 
if getElementHealth(tigre1) > 1 then 
dxDrawRectangle ( sx-x/14/2+visibleto/2, sy+y/25-visibleto/3, x/14*getElementHealth(tigre1)/100-visibleto,y/146.3-visibleto/10, tocolor(190,0,0,245) ) 
end 
end 
end 
end 
end 
end 
end) 
  
  
  
addEvent ("get:element",true) 
addEventHandler ( "get:element", root,  
function (element) 
tigre1 = element 
outputChatBox ( "element" ) 
end) 
  
  
addEventHandler ( "onClientResourceStart", resourceRoot, function() 
triggerServerEvent ( "ped:clientready", getLocalPlayer(), getLocalPlayer() ) 
end) 

Server:

Lped = createPed ( 0,0,0,10) 
  
  
function sendServer(playerToSendTo) 
triggerClientEvent (playerToSendTo, "get:element", root, Lped ) 
end 
addEvent ( "ped:clientready", true ) 
addEventHandler ( "ped:clientready", root, sendServer ) 

Link to comment
You can't make a 3D progress bar using GUI functions, you must draw it using dxDrawRectangle.

You actually can:

Client:

local screenX, screenY = guiGetScreenSize() 
local sizeX = screenX*(100/1920) 
  
  
  
local rootElement = getRootElement() 
function PETvida() 
    if ( isElement ( tigre1 ) ) then 
local pedX, pedY, pedZ = getElementPosition ( tigre1 ) 
local plX, plY, plZ = getElementPosition ( localPlayer ) 
local visibleto = getDistanceBetweenPoints3D(pedX, pedY, pedZ,plX, plY, plZ) 
if visibleto < 45 then 
local headX, headY, headZ = getPedBonePosition( tigre1, 5 ) 
local sx, sy = getScreenFromWorldPosition (headX, headY, headZ+0.6) 
        if sx then 
guiSetPosition ( life,  sx-((sizeX-visibleto)/2), sy, false ) 
guiSetSize (life, sizeX-visibleto, screenY*((30-visibleto)/1080), false) 
    local vida = getElementHealth ( tigre1 ) 
        guiProgressBarSetProgress(life, tonumber (vida)) 
end 
end 
end 
end 
  
  
function HandleTheRendering() 
        life = guiCreateProgressBar( 0, 0,0, 0, false, nil ) 
addEventHandler("onClientRender",rootElement, PETvida) 
end 
addEventHandler("onBotSpawned",rootElement, HandleTheRendering) 
  

Now you only need to send the element to the client like this:

client:

local clientElement = nil 
  
addEvent ( "get:element", true ) 
addEventHandler ( "get:element", root, function(element) 
clientElement = element 
end) 
  

Server:

Lped = createPed ( 0,0,0,1) 
  
function sendServer(playerToSendTo) 
triggerClientEvent (playerToSendTo, "get:element", root, Lped ) 
end 

This will send the server element "Lped" to the client, in that way you can check his health.

If you need explanation with these functions tell it here.

But dx rectangles will look nicer, and it it's faster for a client pc.

Regards,

Danny

I doubt it'll be 3D, plus, using dxDrawRectangle looks a lot better.

Link to comment
@SolidSnake,

It depends what do you mean by 3D. Since dxDrawRectangle draws just a rectangle but moves around the screen and resizes depending on distance, it can be done the same with progress bars.

@|H|TiTanium,

Have a look in the source code: https://community.multitheftauto.com/in ... ils&id=234

I used the dxDrawRectangle to draw health bars.

I'm using slothbot, so I didn't understand half thing you have made, I'm not that expert as you see .-.

Also, I didn't get what denny said, he told me to overwrite/replace/edit something while everything seems to be done for me, probably I'm forgetting something, so this is what I got, and stil the same, Nothing works, no rectangle, and not even ped's health.

Client:

local x, y = guiGetScreenSize() 
local tigre1 = nil 
  
addEventHandler("onClientRender",getRootElement(), 
function () 
if ( isElement ( tigre1 ) ) then 
local mx, my, mz = getElementPosition(tigre1) 
local mcx, mcy, mcz = getCameraMatrix() 
if isLineOfSightClear ( mcx , mcy, mcz, mx, my, mz, true, false, false, true, false, true, false, getLocalPlayer ( )) then 
local x1,y1,z1 = getElementPosition (getLocalPlayer()) 
local x2,y2,z2 = getElementPosition (tigre1) 
local visibleto = getDistanceBetweenPoints3D(x1,y1,z1,x2,y2,z2) 
if visibleto < 75 then 
local sx,sy = getScreenFromWorldPosition ( x2,y2,z2+1.45 ) 
if sx and sy then 
if y/102-visibleto/5 > 0 then 
dxDrawRectangle ( sx-x/14/2+visibleto/2, sy+y/25-visibleto/3, x/14-visibleto,y/146.3-visibleto/10, tocolor(0,0,0,245) ) 
if getElementHealth(tigre1) > 1 then 
dxDrawRectangle ( sx-x/14/2+visibleto/2, sy+y/25-visibleto/3, x/14*getElementHealth(tigre1)/100-visibleto,y/146.3-visibleto/10, tocolor(190,0,0,245) ) 
end 
end 
end 
end 
end 
end 
end) 
  
  
  
addEvent ("get:element",true) 
addEventHandler ( "get:element", root, 
function (element) 
tigre1 = element 
outputChatBox ( "element" ) 
end) 
  
  
addEventHandler ( "onClientResourceStart", resourceRoot, function() 
triggerServerEvent ( "ped:clientready", getLocalPlayer(), getLocalPlayer() ) 
end) 

Server side:

function sendServer(playerToSendTo) 
triggerClientEvent (playerToSendTo, "get:element", root, tigre1 ) 
end 
addEvent ( "ped:clientready", true ) 
addEventHandler ( "ped:clientready", root, sendServer ) 

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...