Jump to content

I need to draw DXText


Deep thinker

Recommended Posts

-- Server Side
local newVehicle = {    }
local myBlip = {    }
local marker = {    }

addEvent('MyEvent',true)
addEventHandler('MyEvent',root,
    function  ( )
    if not getPlayerTeam(source) or getPlayerTeam(source) ~= getTeamFromName("Criminal") then return outputChatBox( 'You are not a criminal to deliver drugs',source,255,0,0 ) end
        if newVehicle [ source ] and isElement ( newVehicle [ source ] ) then 
        outPutChatBox("You must be a criminal to deliver drugs",255,0,0) return false end
             newVehicle [ source ] = createVehicle ( 482,1603.34717,-1459.95898,13.68424 )
             warpPedIntoVehicle ( source,newVehicle [ source ] )  
             myBlip [ source ] = createBlip( 1019.93677,2373.41040,10.93885,51,10,255,0,0,255,0 )
             setElementVisibleTo( myBlip [ source ],root,false )
             setElementVisibleTo( myBlip [ source ],source,true )
             marker [ source ] = createMarker ( 1019.93677,2373.41040,9.93885,'cylinder',5,255,53,53,53,source ) 
    end
)

addEventHandler ( 'onMarkerHit',resourceRoot,
    function  ( hit )
        if  ( hit and getElementType ( hit ) == 'vehicle' ) then
            local player = getVehicleController ( hit )
        if ( source == marker [ player  ] )then
        if isElement ( newVehicle [ player ] ) then
         triggerServerEvent ( 'onDelivered',player )
            if ( hit == newVehicle [ player ] ) then
                        triggerServerEvent ( 'onDelivered',player )
                        destroyElement ( newVehicle [ player ] )
                        destroyElement ( myBlip [ player ] )
                        destroyElement ( marker [ player ] )
                        newVehicle [ player ] = nil 
                        myBlip [ player ] = nil 
                        marker [ player ] = nil 
                        givePlayerMoney ( player,10000 )
                       end
                end
            end
        end
    end
)

addEventHandler ( 'onPlayerVehicleExit',root,
    function ( vehicle )
        if ( vehicle == newVehicle [ source ] ) then
            if isElement ( newVehicle [ source ] ) then
                 destroyElement( newVehicle [ source ] )
                 newVehicle [ source ] = nil
            end
            if isElement ( myBlip [ source ] ) then
                 destroyElement( myBlip [ source ] )
                 myBlip [ source ] = nil
            end
            if isElement ( marker [ source ] ) then
                 destroyElement( marker [ source ] )
                 marker [ source ] = nil
            end
        end
    end
)

addEventHandler ( 'onPlayerQuit',root,
    function (  )
           if isElement ( newVehicle [ source ] ) then
                 destroyElement( newVehicle [ source ] )
                 newVehicle [ source ] = nil
            end
            if isElement ( myBlip [ source ] ) then
                 destroyElement( myBlip [ source ] )
                 myBlip [ source ] = nil
            end
            if isElement ( marker [ source ] ) then
                 destroyElement( marker [ source ] )
                 marker [ source ] = nil
             end
    end
)
-- Client
local marker = createMarker ( 1616.44446,-1506.94568,13.20866,"cylinder",1,255,0,0,0 ) 
local ped = createPed ( 28,1616.44446, -1506.94568, 14.2066,90,0,0 )
setElementFrozen( ped,true )

GUIEditor_Window = {}
GUIEditor_Button = {}
GUIEditor_Memo = {}

GUIEditor_Window[1] = guiCreateWindow(0.3698,0.5208,0.2517,0.1863,"\"Drug Delivery Request\"",true)
    guiSetVisible ( GUIEditor_Window[1],false )
            showCursor ( false )
guiSetAlpha(GUIEditor_Window[1],1)
guiWindowSetMovable(GUIEditor_Window[1],false)
guiWindowSetSizable(GUIEditor_Window[1],false)
GUIEditor_Button[1] = guiCreateButton(29,114,92,31,"Cancel",false,GUIEditor_Window[1])
GUIEditor_Button[2] = guiCreateButton(158,114,92,31,"Accept",false,GUIEditor_Window[1])
GUIEditor_Memo[1] = guiCreateMemo(11,26,264,79,"If you Pressed \"Accept\"Follow The Blip in the radar and you will gain 10000$ .If you Pressed Cancel or Leaved the Vehicle in the Delivery then your mission failed.",false,GUIEditor_Window[1])
guiMemoSetReadOnly(GUIEditor_Memo[1],true)





function ToggleDDMenu ( hit )
  if ( hit and hit == localPlayer ) then 
        if eventName == 'onClientMarkerHit' then
                    guiSetVisible ( GUIEditor_Window[1],true )
                    showCursor ( true )
    elseif eventName == 'onClientMarkerLeave' then 
                    guiSetVisible ( GUIEditor_Window[1],false )
                    showCursor ( false )
                    
            end
        end
    end
addEventHandler("onClientMarkerHit",marker,ToggleDDMenu)
addEventHandler("onClientMarkerLeave",marker,ToggleDDMenu)

addEventHandler("onClientRender", getRootElement(), 
function()
local x, y, z = getElementPosition(ped)
local x2, y2, z2 = getElementPosition(localPlayer)
if(isLineOfSightClear(x, y, z, x2, y2, z2, true, true, false, true)) then
local sx, sy = getScreenFromWorldPosition(x, y, z+1.1)
if(sx) and (sy) then
local distance = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
if(distance < 30) then
                dxDrawText("Drugs Dealer", sx+2, sy+2, sx, sy, tocolor(0,0,0), 2-(distance/20), "arial", "center", "center")
                dxDrawText("Drugs Dealer", sx, sy, sx, sy, tocolor(255,0,0), 2-(distance/20), "arial", "center", "center")
            end
        end
    end
end
)
addEventHandler ('onClientGUIClick',GUIEditor_Button[1],
    function (  )
            guiSetVisible ( GUIEditor_Window[1],false )
            showCursor ( false )
    end,false
)

addEventHandler ('onClientGUIClick',GUIEditor_Button[2],
    function (  )
            guiSetVisible ( GUIEditor_Window[1],false )
            showCursor ( false )
            triggerServerEvent ("MyEvent",localPlayer)
    end,false
)
addEventHandler ( 'onClientPedDamage',ped,cancelEvent  )
--On delivered
addEvent("onDelivered",true)
addEventHandler("onDelivered",root
    function drawStaticBar()
						setTimer( function ()
							dxDrawText("Good Job ,Here is 10000$",247.0,332.0,917.0,373.0,tocolor(150,0,0,255),2.0,"pricedown","left","top",false,false,false)                
							end,false,3000,1 )
            end,false )

 

hello ,i wanted to add a dxText in the end of the mission that Says " Good Job ,Here is 10000$" but i have added it .the bug is it hides the marker.also i wanted to make a table of markers instead of 1 marker .

thank you

Link to comment

i knew where is

        triggerServerEvent ( 'onDelivered',player )
            if ( hit == newVehicle [ player ] ) then
                        triggerServerEvent ( 'onDelivered',player )
  -- it should be 
          triggerClientEvent ( 'onDelivered',player )
            if ( hit == newVehicle [ player ] ) then

the bug that hides the marker  which is  the tiggerServerEvent

but i still can't see the DX that i have drawn :3

Link to comment

try this

addEventHandler ( 'onMarkerHit',resourceRoot,

    function  ( hit )
	
	if  ( hit and getElementType ( hit ) == 'vehicle' ) then
		
	local player = getVehicleController ( hit )
	
	if ( player ) then
		
	if ( marker [ player  ] ) and ( source == marker [ player  ] )then

	if ( isElement ( newVehicle [ player ] ) ) and ( hit == newVehicle [ player ] ) then	
	
	triggerClientEvent ( player , 'onDelivered', player )
	
	destroyElement ( newVehicle [ player ] )
	
	destroyElement ( myBlip [ player ] )
	
	destroyElement ( marker [ player ] )
	
	newVehicle [ player ] = nil 
	
	myBlip [ player ] = nil 
	
	marker [ player ] = nil 
	
	givePlayerMoney ( player,10000 )

					end
                end
            end
        end
    end
	)

 

  • Like 1
Link to comment

The errors in your code you have to fix is :

 

  • In the server side line 27 and 29 You are using triggerServerEvent but it's a client side function and i don't know why are you triggering it twice :S so it should be just after taking the money like this :
triggerClientEvent ( player,'onDelivered',player )

 

  • In the client side from line 72 To 78 all wrong replace it like this you have to use 'onClientRender' To draw a text try the following : 
local aOnDraw = false 
function aOnDraw (		)
	dxDrawText ( 'Good Job ,Here is 10000$',247.0,332.0,917.0,373.0,tocolor(150,0,0,255),2.0,'pricedown','left','top',false,false,false )  
	aOnDraw = true  
	setTimer ( function (	) if aOnDraw == true then removeEventHandler ( 'onClientRender',root,aOnDraw ) aOnDraw = false end end,5000,1 )
end

addEvent ( 'onDelivered',true )
addEventHandler ( 'onDelivered',root,
	function (		)
	if aOnDraw == false then
		addEventHandler ( 'onClientRender',root,aOnDraw )
	end
end
)

 

 

  • And about your question how to create a table for the same marker do as the following : 
local aMyPositions = {	
	{ x,y,z };
	{ x,y,z };
	{ x,y,z };
	{ x,y,z };
};

for _,aV in ipairs ( aMyPositions ) do
	marker = createMarker ( aV[1],aV[2],aV[3],'cylinder',1,255,0,0,0 );
end

 

 

None of my code's are tasted so try it and if there's any errors warnings in the debug post it here. 

  • Like 1
Link to comment
12 minutes ago, FaHaD said:

The errors in your code you have to fix is :

 

  •  


triggerClientEvent ( player,'onDelivered',player )

 

  • In the client side from line 72 To 78 all wrong replace it like this you have to use 'onClientRender' To draw a text try the following : 

local aOnDraw = false 
function aOnDraw (		)
	dxDrawText ( 'Good Job ,Here is 10000$',247.0,332.0,917.0,373.0,tocolor(150,0,0,255),2.0,'pricedown','left','top',false,false,false )  
	aOnDraw = true  
	setTimer ( function (	) if aOnDraw == true then removeEventHandler ( 'onClientRender',root,aOnDraw ) aOnDraw = false end end,5000,1 )
end

addEvent ( 'onDelivered',true )
addEventHandler ( 'onDelivered',root,
	function (		)
	if aOnDraw == false then
		addEventHandler ( 'onClientRender',root,aOnDraw )
	end
end
)

 

 

  • And about your question how to create a table for the same marker do as the following : 

local aMyPositions = {	
	{ x,y,z };
	{ x,y,z };
	{ x,y,z };
	{ x,y,z };
};

for _,aV in ipairs ( aMyPositions ) do
	marker = createMarker ( aV[1],aV[2],aV[3],'cylinder',1,255,0,0,0 );
end

 

 

None of my code's are tasted so try it and if there's any errors warnings in the debug post it here. 

 

12 minutes ago, Abdul KariM said:

try this


addEventHandler ( 'onMarkerHit',resourceRoot,

    function  ( hit )
	
	if  ( hit and getElementType ( hit ) == 'vehicle' ) then
		
	local player = getVehicleController ( hit )
	
	if ( player ) then
		
	if ( marker [ player  ] ) and ( source == marker [ player  ] )then

	if ( isElement ( newVehicle [ player ] ) ) and ( hit == newVehicle [ player ] ) then	
	
	triggerClientEvent ( player , 'onDelivered', player )
	
	destroyElement ( newVehicle [ player ] )
	
	destroyElement ( myBlip [ player ] )
	
	destroyElement ( marker [ player ] )
	
	newVehicle [ player ] = nil 
	
	myBlip [ player ] = nil 
	
	marker [ player ] = nil 
	
	givePlayerMoney ( player,10000 )

					end
                end
            end
        end
    end
	)

 

it still doesn't work for both codes .

also

13 minutes ago, FaHaD said:
  • In the server side line 27 and 29 You are using triggerServerEvent but it's a client side function and i don't know why are you triggering it twice :S so it should be just after taking the money like this :

triggerClientEvent ( player,'onDelivered',player )

 

i noticed that i knew that i tiggered the event twice as you can see but thanks for the marker table 

33 minutes ago, ProMax said:

i knew where is


        triggerServerEvent ( 'onDelivered',player )
            if ( hit == newVehicle [ player ] ) then
                        triggerServerEvent ( 'onDelivered',player )
  -- it should be 
          triggerClientEvent ( 'onDelivered',player )
            if ( hit == newVehicle [ player ] ) then

the bug that hides the marker  which is  the tiggerServerEvent

but i still can't see the DX that i have drawn :3

 

Link to comment

Try this ;

 

function aOnDraw (		)
	dxDrawText ( 'Good Job ,Here is 10000$',247.0,332.0,917.0,373.0,tocolor(150,0,0,255),2.0,'pricedown','left','top',false,false,false )  
	setTimer ( 
		function (	) 
		if getElementData ( localPlayer,'aOnDraw' ) == true then
		removeEventHandler ( 'onClientRender',root,aOnDraw ) 
		setElementData ( localPlayer,'aOnDraw',false ) end
		end,3000,1 )
end

addEvent ( 'onDelivered',true )
addEventHandler ( 'onDelivered',root,
	function (		)
	local aDraw = getElementData ( localPlayer,'aOnDraw' )
	if not aDraw or aDraw ~= true then
		addEventHandler ( 'onClientRender',root,aOnDraw )
		setElementData ( localPlayer,'aOnDraw',true )
	end
end
)

 

  • Like 1
Link to comment
-- Client
local aMyPositions = {      { x,y,z };
    { 1616.44446,-1506.94568,14.20866 };
    { 1616.62317, -1503.23218, 14.22469 };
 };

for _,aV in ipairs ( aMyPositions ) do
    marker = createMarker ( aV[1],aV[2],aV[3],'cylinder',1,255,0,0,0 );
    ped = createPed ( 28,aV[1],aV[2],aV[3],90,0,0 );
    setElementFrozen( ped,true )
end

GUIEditor_Window = {}
GUIEditor_Button = {}
GUIEditor_Memo = {}

GUIEditor_Window[1] = guiCreateWindow(0.3698,0.5208,0.2517,0.1863,"\"Drug Delivery Request\"",true)
    guiSetVisible ( GUIEditor_Window[1],false )
            showCursor ( false )
guiSetAlpha(GUIEditor_Window[1],1)
guiWindowSetMovable(GUIEditor_Window[1],false)
guiWindowSetSizable(GUIEditor_Window[1],false)
GUIEditor_Button[1] = guiCreateButton(29,114,92,31,"Cancel",false,GUIEditor_Window[1])
GUIEditor_Button[2] = guiCreateButton(158,114,92,31,"Accept",false,GUIEditor_Window[1])
GUIEditor_Memo[1] = guiCreateMemo(11,26,264,79,"If you Pressed \"Accept\"Follow The Blip in the radar and you will gain 10000$ .If you Pressed Cancel or Leaved the Vehicle in the Delivery then your mission failed.",false,GUIEditor_Window[1])
guiMemoSetReadOnly(GUIEditor_Memo[1],true)





function ToggleDDMenu ( hit )
  if ( hit and hit == localPlayer ) then 
        if eventName == 'onClientMarkerHit' then
                    guiSetVisible ( GUIEditor_Window[1],true )
                    showCursor ( true )
    elseif eventName == 'onClientMarkerLeave' then 
                    guiSetVisible ( GUIEditor_Window[1],false )
                    showCursor ( false )
                    
            end
        end
    end
addEventHandler("onClientMarkerHit",marker,ToggleDDMenu)
addEventHandler("onClientMarkerLeave",marker,ToggleDDMenu)

addEventHandler("onClientRender", getRootElement(), 
function()
local x, y, z = getElementPosition(ped)
local x2, y2, z2 = getElementPosition(localPlayer)
if(isLineOfSightClear(x, y, z, x2, y2, z2, true, true, false, true)) then
local sx, sy = getScreenFromWorldPosition(x, y, z+1.1)
if(sx) and (sy) then
local distance = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
if(distance < 30) then
                dxDrawText("Drugs Dealer", sx+2, sy+2, sx, sy, tocolor(0,0,0), 2-(distance/20), "arial", "center", "center")
                dxDrawText("Drugs Dealer", sx, sy, sx, sy, tocolor(255,0,0), 2-(distance/20), "arial", "center", "center")
            end
        end
    end
end
)
addEventHandler ('onClientGUIClick',GUIEditor_Button[1],
    function (  )
            guiSetVisible ( GUIEditor_Window[1],false )
            showCursor ( false )
    end,false
)

addEventHandler ('onClientGUIClick',GUIEditor_Button[2],
    function (  )
            guiSetVisible ( GUIEditor_Window[1],false )
            showCursor ( false )
            triggerServerEvent ("MyEvent",localPlayer)
    end,false
)
addEventHandler ( 'onClientPedDamage',ped,cancelEvent  )
--On delivered
function aOnDraw (      )
    dxDrawText ( 'Good Job ,Here is 10000$',247.0,332.0,917.0,373.0,tocolor(150,0,0,255),2.0,'pricedown','left','top',false,false,false )  
    setTimer ( 
        function (  ) 
        if getElementData ( localPlayer,'aOnDraw' ) == true then
        removeEventHandler ( 'onClientRender',root,aOnDraw ) 
        setElementData ( localPlayer,'aOnDraw',false ) end
        end,3000,1 )
end

addEvent ( 'onDelivered',true )
addEventHandler ( 'onDelivered',root,
    function (      )
    local aDraw = getElementData ( localPlayer,'aOnDraw' )
    if not aDraw or aDraw ~= true then
        addEventHandler ( 'onClientRender',root,aOnDraw )
        setElementData ( localPlayer,'aOnDraw',true )
    end
end
)

if you can add it into the server side it would be better

-- Server Side
local newVehicle = {    }
local myBlip = {    }
local marker = {    }

addEvent('MyEvent',true)
addEventHandler('MyEvent',root,
    function  ( )
    if not getPlayerTeam(source) or getPlayerTeam(source) ~= getTeamFromName("Criminal") then return outputChatBox( 'You are not a criminal to deliver drugs',source,255,0,0 ) end
        if newVehicle [ source ] and isElement ( newVehicle [ source ] ) then 
        outPutChatBox("You must be a criminal to deliver drugs",255,0,0) return false end
             newVehicle [ source ] = createVehicle ( 482,1603.34717,-1459.95898,13.68424 )
             warpPedIntoVehicle ( source,newVehicle [ source ] )  
             myBlip [ source ] = createBlip( 1019.93677,2373.41040,10.93885,51,10,255,0,0,255,0 )
             setElementVisibleTo( myBlip [ source ],root,false )
             setElementVisibleTo( myBlip [ source ],source,true )
             marker [ source ] = createMarker ( 1019.93677,2373.41040,9.93885,'cylinder',5,255,53,53,53,source ) 
    end
)

addEventHandler ( 'onMarkerHit',resourceRoot,
    function  ( hit )
        if  ( hit and getElementType ( hit ) == 'vehicle' ) then
            local player = getVehicleController ( hit )
        if ( source == marker [ player  ] )then
        if isElement ( newVehicle [ player ] ) then
            if ( hit == newVehicle [ player ] ) then
                        triggerClientEvent ( 'onDelivered',player )
                        destroyElement ( newVehicle [ player ] )
                        destroyElement ( myBlip [ player ] )
                        destroyElement ( marker [ player ] )
                        newVehicle [ player ] = nil 
                        myBlip [ player ] = nil 
                        marker [ player ] = nil 
                        givePlayerMoney ( player,10000 )
                       end
                end
            end
        end
    end
)

addEventHandler ( 'onPlayerVehicleExit',root,
    function ( vehicle )
        if ( vehicle == newVehicle [ source ] ) then
            if isElement ( newVehicle [ source ] ) then
                 destroyElement( newVehicle [ source ] )
                 newVehicle [ source ] = nil
            end
            if isElement ( myBlip [ source ] ) then
                 destroyElement( myBlip [ source ] )
                 myBlip [ source ] = nil
            end
            if isElement ( marker [ source ] ) then
                 destroyElement( marker [ source ] )
                 marker [ source ] = nil
            end
        end
    end
)

addEventHandler ( 'onPlayerQuit',root,
    function (  )
           if isElement ( newVehicle [ source ] ) then
                 destroyElement( newVehicle [ source ] )
                 newVehicle [ source ] = nil
            end
            if isElement ( myBlip [ source ] ) then
                 destroyElement( myBlip [ source ] )
                 myBlip [ source ] = nil
            end
            if isElement ( marker [ source ] ) then
                 destroyElement( marker [ source ] )
                 marker [ source ] = nil
             end
    end
)

 

Link to comment

So what this should make two marker's and ped's but you are using the same position in the both of the row's so? i'm not getting that well what you mean by steal other ped features? i don't understand that so what's the problem? and why you need to make a table in the server side while there's one for the player element's.

  • Like 1
Link to comment

now they aren't in the same place they are next to each other just for a test ,i mean with stealing features is that the marker of the first one doesn't work anymore but in the new ped it works ,with the ped it deletes the "drug dealer" dxText on the first ped and put it on the new one.

what i mean is the first marker and ped became useless,

Link to comment
local aMyPositions = {    
    { 1616.44446,-1506.94568,14.20866 } ,
    { 1616.62317, -1503.23218, 14.22469 }
 };
 
function ToggleDDMenu ( hit )

	if ( source and hit and hit == localPlayer ) then 
	
	if eventName == 'onClientMarkerHit' then
	
	guiSetVisible ( GUIEditor_Window[1],true )
	
	showCursor ( true )
	
    elseif eventName == 'onClientMarkerLeave' then 
	
	guiSetVisible ( GUIEditor_Window[1],false )
	
	showCursor ( false )                
            end
        end
    end 

for _,aV in ipairs ( aMyPositions ) do

	marker = createMarker ( aV[1],aV[2],aV[3],'cylinder',1,255,0,0,0 );

	ped = createPed ( 28,aV[1],aV[2],aV[3],90,0,0 );

	setElementFrozen( ped,true )

	if ( marker ) then

	addEventHandler("onClientMarkerHit",marker,ToggleDDMenu)

	addEventHandler("onClientMarkerLeave",marker,ToggleDDMenu)

	end
end

 

addEventHandler("onClientRender", getRootElement(), 
function()

for _ ,v in ipairs ( getElementsByType ( "ped" , resourceRoot ) ) do 

local x, y, z = getElementPosition(v)

local x2, y2, z2 = getElementPosition(localPlayer)
if(isLineOfSightClear(x, y, z, x2, y2, z2, true, true, false, true)) then
local sx, sy = getScreenFromWorldPosition(x, y, z+1.1)
if(sx) and (sy) then
local distance = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
if(distance < 30) then
                dxDrawText("Drugs Dealer", sx+2, sy+2, sx, sy, tocolor(0,0,0), 2-(distance/20), "arial", "center", "center")
                dxDrawText("Drugs Dealer", sx, sy, sx, sy, tocolor(255,0,0), 2-(distance/20), "arial", "center", "center")
				end
			end
        end	
    end
end
)

 

 

 

 

 

 

 

Link to comment
3 minutes ago, Abdul KariM said:

local aMyPositions = {        { 1616.44446,-1506.94568,14.20866 } ,    { 1616.62317, -1503.23218, 14.22469 } }; function ToggleDDMenu ( hit )	if ( source and hit and hit == localPlayer ) then 		if eventName == 'onClientMarkerHit' then		guiSetVisible ( GUIEditor_Window[1],true )		showCursor ( true )	    elseif eventName == 'onClientMarkerLeave' then 		guiSetVisible ( GUIEditor_Window[1],false )		showCursor ( false )                            end        end    end for _,aV in ipairs ( aMyPositions ) do	marker = createMarker ( aV[1],aV[2],aV[3],'cylinder',1,255,0,0,0 );	ped = createPed ( 28,aV[1],aV[2],aV[3],90,0,0 );	setElementFrozen( ped,true )	if ( marker ) then	addEventHandler("onClientMarkerHit",marker,ToggleDDMenu)	addEventHandler("onClientMarkerLeave",marker,ToggleDDMenu)	endend

 


addEventHandler("onClientRender", getRootElement(), function()for _ ,v in ipairs ( getElementsByType ( "ped" , resourceRoot ) ) do local x, y, z = getElementPosition(v)local x2, y2, z2 = getElementPosition(localPlayer)if(isLineOfSightClear(x, y, z, x2, y2, z2, true, true, false, true)) thenlocal sx, sy = getScreenFromWorldPosition(x, y, z+1.1)if(sx) and (sy) thenlocal distance = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)if(distance < 30) then                dxDrawText("Drugs Dealer", sx+2, sy+2, sx, sy, tocolor(0,0,0), 2-(distance/20), "arial", "center", "center")                dxDrawText("Drugs Dealer", sx, sy, sx, sy, tocolor(255,0,0), 2-(distance/20), "arial", "center", "center")				end			end        end	    endend)

 

 

 

 

 

 

 

You are a PRO scripting :3

thank you both for helping me but  i got 1 last question .i wanted to make superman script for "Staff" team

Link to comment
local Superman = {}

-- Static global values
local rootElement = getRootElement()
local thisResource = getThisResource()

-- Resource events
addEvent("superman:start", true)
addEvent("superman:stop", true)

--
-- Start/stop functions
--
-- server side
function Superman.Start()
  local self = Superman

  addEventHandler("superman:start", rootElement, self.clientStart)
  addEventHandler("superman:stop", rootElement, self.clientStop)
end
addEventHandler("onResourceStart", getResourceRootElement(thisResource), Superman.Start, false)

function Superman.clientStart()
  setElementData(client, "superman:flying", true)
end

function Superman.clientStop()
  setElementData(client, "superman:flying", false)
end

i think we should add getPlayerTeam or getTeamFromName ,but where to add it in the whole script 

Link to comment
local Superman = {}

-- Settings
local ZERO_TOLERANCE = 0.00001
local MAX_ANGLE_SPEED = 6 -- In degrees per frame
local MAX_SPEED = 1.0
local EXTRA_SPEED_FACTOR = 1.85
local LOW_SPEED_FACTOR = 0.40
local ACCELERATION = 0.025
local EXTRA_ACCELERATION_FACTOR = 1.8
local LOW_ACCELERATION_FACTOR = 0.85
local TAKEOFF_VELOCITY = 3.75
local TAKEOFF_FLIGHT_DELAY = 750
local SMOKING_SPEED = 1.25
local GROUND_ZERO_TOLERANCE = 0.18
local LANDING_DISTANCE = 3.2
local FLIGHT_ANIMLIB = "swim"
local FLIGHT_ANIMATION = "Swim_Dive_Under"
local FLIGHT_ANIM_LOOP = false
local IDLE_ANIMLIB = "cop_ambient"
local IDLE_ANIMATION = "Coplook_loop"
local IDLE_ANIM_LOOP = true
local MAX_Y_ROTATION = 55
local ROTATION_Y_SPEED = 3.8

-- Static global variables
local thisResource = getThisResource()
local rootElement = getRootElement()
local localPlayer = getLocalPlayer()
local serverGravity = getGravity()


--
-- Utility functions
--
local function isPlayerFlying(player)
  local data = getElementData(player, "superman:flying")
  if not data or data == false then return false
  else return true end
end

local function setPlayerFlying(player, state)
  if state == true then state = true
  else state = false end
  setElementData(player, "superman:flying", state)
end

local function iterateFlyingPlayers()
  local current = 1
  local allPlayers = getElementsByType("player")

  return function()
    local player
    repeat
      player = allPlayers[current]
      current = current + 3
    until not player or (isPlayerFlying(player) and isElementStreamedIn(player))

    return player
  end
end

function Superman:restorePlayer(player)
  setPlayerFlying(player, false)
  setPedAnimation(player, false)
  setElementVelocity(player, 0, 0, 0)
  setElementRotation(player, 0, 0, 0)
  --setPedRotation(player, getPedRotation(player))
  setElementCollisionsEnabled(player, true)
  self:destroySmokeGenerators(player)
  self.rotations[player] = nil
  self.previousVelocity[player] = nil
end

function Superman:createSmokeGenerator(player)
  local generator = createObject(2780, getElementPosition(player))
  setElementCollisionsEnabled(generator, false)
  setObjectScale(generator, 0)
  return generator
end

function Superman:createSmokeGenerators(player)
  if not self.smokeGenerators[player] then
    local smokeGenerators = {}

    smokeGenerators[1] = self:createSmokeGenerator(player)
    attachElementToElement(smokeGenerators[1], player, 0.75, -0.2, -0.4, -40, 0, 60)
    smokeGenerators[2] = self:createSmokeGenerator(player)
    attachElementToElement(smokeGenerators[2], player, -0.75, -0.2, -0.4, -40, 0, -60)

    self.smokeGenerators[player] = smokeGenerators
  end
end

function Superman:destroySmokeGenerators(player)
  if self.smokeGenerators[player] then
    for k, v in ipairs(self.smokeGenerators[player]) do
      destroyElement(v)
    end
    self.smokeGenerators[player] = nil
  end
end

function angleDiff(angle1, angle2)
  angle1, angle2 = angle1 % 360, angle2 % 360
  local diff = (angle1 - angle2) % 360
  if diff <= 180 then
    return diff
  else
    return -(360 - diff)
  end
end

local function isPedInWater(ped)
  local pedPosition = Vector3D:new(getElementPosition(ped))
  if pedPosition.z <= 0 then return true end

  local waterLevel = getWaterLevel(pedPosition.x, pedPosition.y, pedPosition.z)
  if not isElementStreamedIn(ped) or not waterLevel or waterLevel < pedPosition.z then
    return false
  else
    return true
  end
end

local function isnan(x)
	math.inf = 1/0
	if x == math.inf or x == -math.inf or x ~= x then
		return true
	end
	return false
end

local function getVector2DAngle(vec)
  if vec.x == 0 and vec.y == 0 then return 0 end
  local angle = math.deg(math.atan(vec.x / vec.y)) + 90
  if vec.y < 0 then
    angle = angle + 180
  end
  return angle
end

--
-- Initialization and shutdown functions
--
function Superman.Start()
  local self = Superman

  -- Register events
  addEventHandler("onClientResourceStop", getResourceRootElement(thisResource), Superman.Stop, false)
  addEventHandler("onPlayerJoin", rootElement, Superman.onJoin)
  addEventHandler("onPlayerQuit", rootElement, Superman.onQuit)
  addEventHandler("onClientRender", rootElement, Superman.processControls)
  addEventHandler("onClientRender", rootElement, Superman.processFlight)
  addEventHandler("onClientPlayerDamage", localPlayer, Superman.onDamage, false)
  addEventHandler("onClientElementDataChange", rootElement, Superman.onDataChange)
  addEventHandler("onClientElementStreamIn", rootElement, Superman.onStreamIn)
  addEventHandler("onClientElementStreamOut", rootElement, Superman.onStreamOut)

  -- Bind keys
  bindKey("jump", "down", Superman.onJump)

  -- Register commands
  addCommandHandler("superman", Superman.cmdSuperman)

  -- Initializate attributes
  self.smokeGenerators = {}
  self.rotations = {}
  self.previousVelocity = {}
end
addEventHandler("onClientResourceStart", getResourceRootElement(thisResource), Superman.Start, false)

function Superman.Stop()
  local self = Superman

  setGravity(serverGravity)

  -- Restore all players animations, collisions, etc
  for player in iterateFlyingPlayers() do
    self:restorePlayer(player)
  end
end



--
-- Join/Quit
--
function Superman.onJoin(player)
  local self = Superman
  local player = player or source

  setPlayerFlying(player, false)
end

function Superman.onQuit(reason, player)
  local self = Superman
  local player = player or source

  if isPlayerFlying(player) then
    self:restorePlayer(player)
  end
end


--
-- onDamage: superman is invulnerable
--
function Superman.onDamage()
  local self = Superman

  if isPlayerFlying(localPlayer) then
    cancelEvent()
  end
end


--
-- onStreamIn: Reset rotation attribute for player
--
function Superman.onStreamIn()
  local self = Superman
end

function Superman.onStreamOut()
  local self = Superman

  if source and isElement(source) and getElementType(source) == "player" and isPlayerFlying(source) then
    self.rotations[source] = nil
    self.previousVelocity[source] = nil
  end
end

--
-- onDataChange: Check if somebody who is out of stream stops being superman
--
function Superman.onDataChange(dataName, oldValue)
  local self = Superman

  if dataName == "superman:flying" and isElement(source) and getElementType(source) == "player" and
     oldValue ~= getElementData(source, dataName) and oldValue == true and getElementData(source, dataName) == false then
    self:restorePlayer(source)
  end
end

--
-- onJump: Combo to start flight without any command
--
function Superman.onJump(key, keyState)
  local self = Superman

  local task = getPlayerSimplestTask(localPlayer)
  if not isPlayerFlying(localPlayer) then
	if task == "TASK_SIMPLE_IN_AIR" then
	  setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)
      setTimer(Superman.startFlight, 100, 1)
	end
  end
end

--
-- Commands
--
function Superman.cmdSuperman()
  local self = Superman

  if isPedInVehicle(localPlayer) or isPlayerFlying(localPlayer) then return end
  setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)
  setTimer(Superman.startFlight, TAKEOFF_FLIGHT_DELAY, 1)
end

function Superman.startFlight()
  local self = Superman

  if isPlayerFlying(localPlayer) then return end

  triggerServerEvent("superman:start", rootElement)
  setPlayerFlying(localPlayer, true)
  setElementVelocity(localPlayer, 0, 0, 0)
  self.currentSpeed = 0
  self.extraVelocity = { x = 0, y = 0, z = 0 }
end


--
-- Controls processing
--
function Superman.processControls()
  local self = Superman

  if not isPlayerFlying(localPlayer) then return end

  -- Calculate the requested movement direction
  local Direction = Vector3D:new(0, 0, 0)
  if getControlState("forwards") then
    Direction.y = 1
  elseif getControlState("backwards") then
    Direction.y = -1
  end

  if getControlState("left") then
    Direction.x = 1
  elseif getControlState("right") then
    Direction.x = -1
  end
  Direction:Normalize()

  -- Calculate the sight direction
  local cameraX, cameraY, cameraZ, lookX, lookY, lookZ = getCameraMatrix()
  local SightDirection = Vector3D:new((lookX - cameraX), (lookY - cameraY), (lookZ - cameraZ))
  SightDirection:Normalize()
  if getControlState("look_behind") then
    SightDirection = SightDirection:Mul(-1)
  end

  -- Calculate the current max speed and acceleration values
  local maxSpeed = MAX_SPEED
  local acceleration = ACCELERATION
  if getControlState("sprint") then
    maxSpeed = MAX_SPEED * EXTRA_SPEED_FACTOR
    acceleration = acceleration * EXTRA_ACCELERATION_FACTOR
  elseif getControlState("walk") then
    maxSpeed = MAX_SPEED * LOW_SPEED_FACTOR
    acceleration = acceleration * LOW_ACCELERATION_FACTOR
  end

  local DirectionModule = Direction:Module()

  -- Check if we must change the gravity
  if DirectionModule == 0 and self.currentSpeed ~= 0 then
    setGravity(0)
  else
    setGravity(serverGravity)
  end

  -- Calculate the new current speed
  if self.currentSpeed ~= 0 and (DirectionModule == 0 or self.currentSpeed > maxSpeed) then
    -- deccelerate
    self.currentSpeed = self.currentSpeed - acceleration
    if self.currentSpeed < 0 then self.currentSpeed = 0 end

  elseif DirectionModule ~= 0 and self.currentSpeed < maxSpeed then
    -- accelerate
    self.currentSpeed = self.currentSpeed + acceleration
    if self.currentSpeed > maxSpeed then self.currentSpeed = maxSpeed end

  end

  -- Calculate the movement requested direction
  if DirectionModule ~= 0 then
    Direction = Vector3D:new(SightDirection.x * Direction.y - SightDirection.y * Direction.x,
                             SightDirection.x * Direction.x + SightDirection.y * Direction.y,
                             SightDirection.z * Direction.y)
    -- Save the last movement direction for when player releases all direction keys
    self.lastDirection = Direction
  else
    -- Player is not specifying any direction, use last known direction or the current velocity
    if self.lastDirection then
      Direction = self.lastDirection
      if self.currentSpeed == 0 then self.lastDirection = nil end
    else
      Direction = Vector3D:new(getElementVelocity(localPlayer))
    end
  end
  Direction:Normalize()
  Direction = Direction:Mul(self.currentSpeed)

  -- Applicate a smooth direction change, if moving
  if self.currentSpeed > 0 then
    local VelocityDirection = Vector3D:new(getElementVelocity(localPlayer))
    VelocityDirection:Normalize()

    if math.sqrt(VelocityDirection.x^2 + VelocityDirection.y^2) > 0 then
      local DirectionAngle = getVector2DAngle(Direction)
      local VelocityAngle = getVector2DAngle(VelocityDirection)

      local diff = angleDiff(DirectionAngle, VelocityAngle)
      local calculatedAngle

      if diff >= 0 then 
        if diff > MAX_ANGLE_SPEED then
          calculatedAngle = VelocityAngle + MAX_ANGLE_SPEED
	else
	  calculatedAngle = DirectionAngle
	end
      else
        if diff < MAX_ANGLE_SPEED then
          calculatedAngle = VelocityAngle - MAX_ANGLE_SPEED
	else
          calculatedAngle = DirectionAngle
        end
      end
      calculatedAngle = calculatedAngle % 360

      local DirectionModule2D = math.sqrt(Direction.x^2 + Direction.y^2)
      Direction.x = -DirectionModule2D*math.cos(math.rad(calculatedAngle))
      Direction.y = DirectionModule2D*math.sin(math.rad(calculatedAngle))
    end
  end

  if Direction:Module() == 0 then
	self.extraVelocity = { x = 0, y = 0, z = 0 }
  end
  
  -- Set the new velocity
  setElementVelocity(localPlayer, Direction.x + self.extraVelocity.x,
                                  Direction.y + self.extraVelocity.y,
								  Direction.z + self.extraVelocity.z)

  if self.extraVelocity.z > 0 then
    self.extraVelocity.z = self.extraVelocity.z - 1
	if self.extraVelocity.z < 0 then self.extraVelocity.z = 0 end
  elseif self.extraVelocity.z < 0 then
	self.extraVelocity.z = self.extraVelocity.z + 1
	if self.extraVelocity.z > 0 then self.extraVelocity.z = 0 end
  end
end



--
-- Players flight processing
--
function Superman.processFlight()
  local self = Superman

  for player in iterateFlyingPlayers() do
    local Velocity = Vector3D:new(getElementVelocity(player))
    local distanceToBase = getElementDistanceFromCentreOfMassToBaseOfModel(player)
    local playerPos = Vector3D:new(getElementPosition(player))
    playerPos.z = playerPos.z - distanceToBase

    local distanceToGround
    if playerPos.z > 0 then
      local hit, hitX, hitY, hitZ, hitElement = processLineOfSight(playerPos.x, playerPos.y, playerPos.z,
                                                                   playerPos.x, playerPos.y, playerPos.z - LANDING_DISTANCE - 1,
                                                                   true, true, true, true, true, false, false, false)
      if hit then distanceToGround = playerPos.z - hitZ end
    end

    if distanceToGround and distanceToGround < GROUND_ZERO_TOLERANCE then
      self:restorePlayer(player)
      if player == localPlayer then
      	setGravity(serverGravity)
        triggerServerEvent("superman:stop", getRootElement())
      end
    elseif distanceToGround and distanceToGround < LANDING_DISTANCE then
      self:processLanding(player, Velocity, distanceToGround)
    elseif Velocity:Module() < ZERO_TOLERANCE then
      self:processIdleFlight(player)
    else
      self:processMovingFlight(player, Velocity)
    end
  end
end

function Superman:processIdleFlight(player)
  -- Set the proper animation on the player
  local animLib, animName = getPedAnimation(player)
  if animLib ~= IDLE_ANIMLIB or animName ~= IDLE_ANIMATION then
    setPedAnimation(player, IDLE_ANIMLIB, IDLE_ANIMATION, -1, IDLE_ANIM_LOOP, false, false)
  end

  setElementCollisionsEnabled(player, false)

  -- If this is myself, calculate the ped rotation depending on the camera rotation
  if player == localPlayer then
    local cameraX, cameraY, cameraZ, lookX, lookY, lookZ = getCameraMatrix()
    local Sight = Vector3D:new(lookX - cameraX, lookY - cameraY, lookZ - cameraZ)
    Sight:Normalize()
    if getControlState("look_behind") then
      Sight = Sight:Mul(-1)
    end

    Sight.z = math.atan(Sight.x / Sight.y)
    if Sight.y > 0 then
      Sight.z = Sight.z + math.pi
    end
    Sight.z = math.deg(Sight.z) + 180

    setPedRotation(localPlayer, Sight.z)
    setElementRotation(localPlayer, 0, 0, Sight.z)
  else
    local Zangle = getPedCameraRotation(player)
    setPedRotation(player, Zangle)
    setElementRotation(player, 0, 0, Zangle)
  end
end

function Superman:processMovingFlight(player, Velocity)
  -- Set the proper animation on the player
  local animLib, animName = getPedAnimation(player)
  if animLib ~= FLIGHT_ANIMLIB or animName ~= FLIGHT_ANIMATION then
    setPedAnimation(player, FLIGHT_ANIMLIB, FLIGHT_ANIMATION, -1, FLIGHT_ANIM_LOOP, true, false)
  end

  if player == localPlayer then
    setElementCollisionsEnabled(player, true)
  else
    setElementCollisionsEnabled(player, false)
  end

  -- Calculate the player rotation depending on their velocity
  local Rotation = Vector3D:new(0, 0, 0)
  if Velocity.x == 0 and Velocity.y == 0 then
    Rotation.z = getPedRotation(player)
  else
    Rotation.z = math.deg(math.atan(Velocity.x / Velocity.y))
    if Velocity.y > 0 then
      Rotation.z = Rotation.z - 180
    end
    Rotation.z = (Rotation.z + 180) % 360
  end
  Rotation.x = -math.deg(Velocity.z / Velocity:Module() * 1.2)

  -- Rotation compensation for the self animation rotation
  Rotation.x = Rotation.x - 40

  -- Calculate the Y rotation for barrel rotations
  if not self.rotations[player] then self.rotations[player] = 0 end
  if not self.previousVelocity[player] then self.previousVelocity[player] = Vector3D:new(0, 0, 0) end
  
  local previousAngle = getVector2DAngle(self.previousVelocity[player])
  local currentAngle = getVector2DAngle(Velocity)
  local diff = angleDiff(currentAngle, previousAngle)
  if isnan(diff) then
	diff = 0
  end
  local calculatedYRotation = -diff * MAX_Y_ROTATION / MAX_ANGLE_SPEED

  if calculatedYRotation > self.rotations[player] then
    if calculatedYRotation - self.rotations[player] > ROTATION_Y_SPEED then
      self.rotations[player] = self.rotations[player] + ROTATION_Y_SPEED
    else
      self.rotations[player] = calculatedYRotation
    end
  else
    if self.rotations[player] - calculatedYRotation > ROTATION_Y_SPEED then
      self.rotations[player] = self.rotations[player] - ROTATION_Y_SPEED
    else
      self.rotations[player] = calculatedYRotation
    end
  end

  if self.rotations[player] > MAX_Y_ROTATION then
    self.rotations[player] = MAX_Y_ROTATION
  elseif self.rotations[player] < -MAX_Y_ROTATION then
    self.rotations[player] = -MAX_Y_ROTATION
  elseif math.abs(self.rotations[player]) < ZERO_TOLERANCE then
    self.rotations[player] = 0
  end
  Rotation.y = self.rotations[player]

  -- Apply the calculated rotation
  setPedRotation(player, Rotation.z)
  setElementRotation(player, Rotation.x, Rotation.y, Rotation.z)

  -- Save the current velocity
  self.previousVelocity[player] = Velocity

  -- If the speed is over the given value, create the smoke generators
  if Velocity:Module() > (SMOKING_SPEED - ZERO_TOLERANCE) and not isPedInWater(player) then
    self:createSmokeGenerators(player)
  else
    self:destroySmokeGenerators(player)
  end
end

function Superman:processLanding(player, Velocity, distanceToGround)
  -- Set the proper animation on the player
  local animLib, animName = getPedAnimation(player)
  if animLib ~= FLIGHT_ANIMLIB or animName ~= FLIGHT_ANIMATION then
    setPedAnimation(player, FLIGHT_ANIMLIB, FLIGHT_ANIMATION, -1, FLIGHT_ANIM_LOOP, true, false)
  end

  if player == localPlayer then
    setElementCollisionsEnabled(player, true)
  else
    setElementCollisionsEnabled(player, false)
  end

  -- If the speed is over the given value, create the smoke generators
  if Velocity:Module() > (SMOKING_SPEED - ZERO_TOLERANCE) and not isPedInWater(player) then
    self:createSmokeGenerators(player)
  else
    self:destroySmokeGenerators(player)
  end

  -- Calculate the player rotation depending on their velocity and distance to ground
  local Rotation = Vector3D:new(0, 0, 0)
  if Velocity.x == 0 and Velocity.y == 0 then
    Rotation.z = getPedRotation(player)
  else
    Rotation.z = math.deg(math.atan(Velocity.x / Velocity.y))
    if Velocity.y > 0 then
      Rotation.z = Rotation.z - 180
    end
    Rotation.z = (Rotation.z + 180) % 360
  end
  Rotation.x = -(85 - (distanceToGround * 85 / LANDING_DISTANCE))

  -- Rotation compensation for the self animation rotation
  Rotation.x = Rotation.x - 40

  -- Apply the calculated rotation
  setPedRotation(player, Rotation.z)
  setElementRotation(player, Rotation.x, Rotation.y, Rotation.z)
end



--
-- Vectors
--
Vector3D = {
  new = function(self, _x, _y, _z)
    local newVector = { x = _x or 0.0, y = _y or 0.0, z = _z or 0.0 }
    return setmetatable(newVector, { __index = Vector3D })
  end,

  Copy = function(self)
    return Vector3D:new(self.x, self.y, self.z)
  end,

  Normalize = function(self)
    local mod = self:Module()
    if mod ~= 0 then
      self.x = self.x / mod
      self.y = self.y / mod
      self.z = self.z / mod
    end
  end,

  Dot = function(self, V)
    return self.x * V.x + self.y * V.y + self.z * V.z
  end,

  Module = function(self)
    return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  end,

  AddV = function(self, V)
    return Vector3D:new(self.x + V.x, self.y + V.y, self.z + V.z)
  end,

  SubV = function(self, V)
    return Vector3D:new(self.x - V.x, self.y - V.y, self.z - V.z)
  end,

  CrossV = function(self, V)
    return Vector3D:new(self.y * V.z - self.z * V.y,
                        self.z * V.x - self.x * V.z,
                        self.x * V.y - self.y * V.z)
  end,

  Mul = function(self, n)
    return Vector3D:new(self.x * n, self.y * n, self.z * n)
  end,

  Div = function(self, n)
    return Vector3D:new(self.x / n, self.y / n, self.z / n)
  end,

  MulV = function(self, V)
    return Vector3D:new(self.x * V.x, self.y * V.y, self.z * V.z)
  end,

  DivV = function(self, V)
    return Vector3D:new(self.x / V.x, self.y / V.y, self.z / V.z)
  end,
}

here is the rest :3

Link to comment

try this 

local function setPlayerFlying(player, state)

	local playerTeam = getPlayerTeam ( player )
	
	local Team = getTeamFromName ( "Staff" )
	
	if ( playerTeam ) and ( Team ) then 
	
	if ( playerTeam == Team ) then 
	
	if state == true then

	state = true

	else
  
	state = false
  
	end
  
	setElementData(player, "superman:flying", state)
			end
		end
	end
	
function Superman.onJump(key, keyState)

	local self = Superman

 	local playerTeam = getPlayerTeam ( localPlayer )
	
	local Team = getTeamFromName ( "Staff" )
	
	if ( playerTeam ) and ( Team ) then 
	
	if ( playerTeam == Team ) then  
  
	local task = getPlayerSimplestTask(localPlayer)
	
	if not isPlayerFlying(localPlayer) then
	
	if task == "TASK_SIMPLE_IN_AIR" then
	
	setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)
	
	setTimer(Superman.startFlight, 100, 1)
	
					end
				end
			end
		end
	end

 

Edited by Abdul KariM
Link to comment

@Abdul KariM Some bug's will be in the script you can try this because there's a cmd and some velocity functions.

 @ProMax Try this :

 

-- Copyright (c) 2008, Alberto Alonso
--
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without modification,
-- are permitted provided that the following conditions are met:
--
--     * Redistributions of source code must retain the above copyright notice, this
--       list of conditions and the following disclaimer.
--     * Redistributions in binary form must reproduce the above copyright notice, this
--       list of conditions and the following disclaimer in the documentation and/or other
--       materials provided with the distribution.
--     * Neither the name of the superman script nor the names of its contributors may be used
--       to endorse or promote products derived from this software without specific prior
--       written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
local Superman = {}

-- Settings
local ZERO_TOLERANCE = 0.00001
local MAX_ANGLE_SPEED = 6 -- In degrees per frame
local MAX_SPEED = 1.0
local EXTRA_SPEED_FACTOR = 1.85
local LOW_SPEED_FACTOR = 0.40
local ACCELERATION = 0.025
local EXTRA_ACCELERATION_FACTOR = 1.8
local LOW_ACCELERATION_FACTOR = 0.85
local TAKEOFF_VELOCITY = 1.75
local TAKEOFF_FLIGHT_DELAY = 750
local SMOKING_SPEED = 1.25
local GROUND_ZERO_TOLERANCE = 0.18
local LANDING_DISTANCE = 3.2
local FLIGHT_ANIMLIB = "swim"
local FLIGHT_ANIMATION = "Swim_Dive_Under"
local FLIGHT_ANIM_LOOP = false
local IDLE_ANIMLIB = "cop_ambient"
local IDLE_ANIMATION = "Coplook_loop"
local IDLE_ANIM_LOOP = true
local MAX_Y_ROTATION = 55
local ROTATION_Y_SPEED = 3.8

-- Static global variables
local thisResource = getThisResource()
local rootElement = getRootElement()
local localPlayer = getLocalPlayer()
local serverGravity = getGravity()


--
-- Utility functions
--
local function isPlayerFlying(player)
  local data = getElementData(player, "superman:flying")
  if not data or data == false then return false
  else return true end
end

local function setPlayerFlying(player, state)
  if state == true then state = true
  else state = false end

  setElementData(player, "superman:flying", state)
end

local function iterateFlyingPlayers()
  local current = 1
  local allPlayers = getElementsByType("player")

  return function()
    local player
    
    repeat
      player = allPlayers[current]
      current = current + 1
    until not player or (isPlayerFlying(player) and isElementStreamedIn(player))

    return player
  end
end

function Superman:restorePlayer(player)
  setPlayerFlying(player, false)
  setPedAnimation(player, false)
  setElementVelocity(player, 0, 0, 0)
  setElementRotation(player, 0, 0, 0)
  --setPedRotation(player, getPedRotation(player))
  setElementCollisionsEnabled(player, true)
  self:destroySmokeGenerators(player)
  self.rotations[player] = nil
  self.previousVelocity[player] = nil
end

function Superman:createSmokeGenerator(player)
  local generator = createObject(2780, getElementPosition(player))
  setElementCollisionsEnabled(generator, false)
  setObjectScale(generator, 0)
  return generator
end

function Superman:createSmokeGenerators(player)
  if not self.smokeGenerators[player] then
    local smokeGenerators = {}

    smokeGenerators[1] = self:createSmokeGenerator(player)
    attachElementToElement(smokeGenerators[1], player, 0.75, -0.2, -0.4, -40, 0, 60)
    smokeGenerators[2] = self:createSmokeGenerator(player)
    attachElementToElement(smokeGenerators[2], player, -0.75, -0.2, -0.4, -40, 0, -60)

    self.smokeGenerators[player] = smokeGenerators
  end
end

function Superman:destroySmokeGenerators(player)
  if self.smokeGenerators[player] then
    for k, v in ipairs(self.smokeGenerators[player]) do
      destroyElement(v)
    end
    self.smokeGenerators[player] = nil
  end
end

function angleDiff(angle1, angle2)
  angle1, angle2 = angle1 % 360, angle2 % 360
  local diff = (angle1 - angle2) % 360
  if diff <= 180 then
    return diff
  else
    return -(360 - diff)
  end
end

local function isPedInWater(ped)
  local pedPosition = Vector3D:new(getElementPosition(ped))
  if pedPosition.z <= 0 then return true end

  local waterLevel = getWaterLevel(pedPosition.x, pedPosition.y, pedPosition.z)
  if not isElementStreamedIn(ped) or not waterLevel or waterLevel < pedPosition.z then
    return false
  else
    return true
  end
end

local function isnan(x)
  math.inf = 1/0
  if x == math.inf or x == -math.inf or x ~= x then
    return true
  end
  return false
end

local function getVector2DAngle(vec)
  if vec.x == 0 and vec.y == 0 then return 0 end
  local angle = math.deg(math.atan(vec.x / vec.y)) + 90
  if vec.y < 0 then
    angle = angle + 180
  end
  return angle
end

--
-- Initialization and shutdown functions
--
function Superman.Start()
  local self = Superman

  -- Register events
  addEventHandler("onClientResourceStop", getResourceRootElement(thisResource), Superman.Stop, false)
  addEventHandler("onPlayerJoin", rootElement, Superman.onJoin)
  addEventHandler("onPlayerQuit", rootElement, Superman.onQuit)
  addEventHandler("onClientRender", rootElement, Superman.processControls)
  addEventHandler("onClientRender", rootElement, Superman.processFlight)
  addEventHandler("onClientPlayerDamage", localPlayer, Superman.onDamage, false)
  addEventHandler("onClientElementDataChange", rootElement, Superman.onDataChange)
  addEventHandler("onClientElementStreamIn", rootElement, Superman.onStreamIn)
  addEventHandler("onClientElementStreamOut", rootElement, Superman.onStreamOut)

  -- Bind keys
  bindKey("jump", "down", Superman.onJump)

  -- Register commands
  addCommandHandler("superman", Superman.cmdSuperman)

  -- Initializate attributes
  self.smokeGenerators = {}
  self.rotations = {}
  self.previousVelocity = {}
end
addEventHandler("onClientResourceStart", getResourceRootElement(thisResource), Superman.Start, false)

function Superman.Stop()
  local self = Superman

  setGravity(serverGravity)

  -- Restore all players animations, collisions, etc
  for player in iterateFlyingPlayers() do
    self:restorePlayer(player)
  end
end



--
-- Join/Quit
--
function Superman.onJoin(player)
  local self = Superman
  local player = player or source

  setPlayerFlying(player, false)
end

function Superman.onQuit(reason, player)
  local self = Superman
  local player = player or source

  if isPlayerFlying(player) then
    self:restorePlayer(player)
  end
end


--
-- onDamage: superman is invulnerable
--
function Superman.onDamage()
  local self = Superman

  if isPlayerFlying(localPlayer) then
    cancelEvent()
  end
end


--
-- onStreamIn: Reset rotation attribute for player
--
function Superman.onStreamIn()
  local self = Superman
end

function Superman.onStreamOut()
  local self = Superman

  if source and isElement(source) and getElementType(source) == "player" and isPlayerFlying(source) then
    self.rotations[source] = nil
    self.previousVelocity[source] = nil
  end
end

--
-- onDataChange: Check if somebody who is out of stream stops being superman
--
function Superman.onDataChange(dataName, oldValue)
  local self = Superman

  if dataName == "superman:flying" and isElement(source) and getElementType(source) == "player" and
     oldValue ~= getElementData(source, dataName) and oldValue == true and getElementData(source, dataName) == false then
    self:restorePlayer(source)
  end
end

--
-- onJump: Combo to start flight without any command
--
function Superman.onJump(key, keyState)
   local aTeam = getPlayerTeam ( localPlayer )
   if not aTeam or aTeam ~= getTeamFromName ( 'Staff' ) then 
    return outputChatBox ( 'Superman is for staff only',255,0,0 ) end
      local self = Superman
  local self = Superman
  local task = getPlayerSimplestTask(localPlayer)
  if not isPlayerFlying(localPlayer) then
  if task == "TASK_SIMPLE_IN_AIR" then
    setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)
      setTimer(Superman.startFlight, 100, 1)
  end
  end
end

--
-- Commands
--
function Superman.cmdSuperman()
       local aTeam = getPlayerTeam ( localPlayer )
      if aTeam and aTeam == getTeamFromName ( 'Staff' ) then 
      local self = Superman
      if isPedInVehicle(localPlayer) or isPlayerFlying(localPlayer) then return end
      setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)
      setTimer(Superman.startFlight, TAKEOFF_FLIGHT_DELAY, 1)
else
      outputChatBox ( 'Superman is for staff only',255,0,0 )
    end
end

function Superman.startFlight()
        local aTeam = getPlayerTeam ( localPlayer )
        if aTeam and aTeam == getTeamFromName ( 'Staff' ) then 
        local self = Superman
        if isPlayerFlying(localPlayer) then return end
        triggerServerEvent("superman:start", rootElement)
        setPlayerFlying(localPlayer, true)
        setElementVelocity(localPlayer, 0, 0, 0)
        self.currentSpeed = 0
        self.extraVelocity = { x = 0, y = 0, z = 0 }
  else
        outputChatBox ( 'Superman is for staff only',255,0,0 )
    end
end
--
-- Controls processing
--
function Superman.processControls()
  local self = Superman

  if not isPlayerFlying(localPlayer) then return end

  -- Calculate the requested movement direction
  local Direction = Vector3D:new(0, 0, 0)
  if getControlState("forwards") then
    Direction.y = 1
  elseif getControlState("backwards") then
    Direction.y = -1
  end

  if getControlState("left") then
    Direction.x = 1
  elseif getControlState("right") then
    Direction.x = -1
  end
  Direction:Normalize()

  -- Calculate the sight direction
  local cameraX, cameraY, cameraZ, lookX, lookY, lookZ = getCameraMatrix()
  local SightDirection = Vector3D:new((lookX - cameraX), (lookY - cameraY), (lookZ - cameraZ))
  SightDirection:Normalize()
  if getControlState("look_behind") then
    SightDirection = SightDirection:Mul(-1)
  end

  -- Calculate the current max speed and acceleration values
  local maxSpeed = MAX_SPEED
  local acceleration = ACCELERATION
  if getControlState("sprint") then
    maxSpeed = MAX_SPEED * EXTRA_SPEED_FACTOR
    acceleration = acceleration * EXTRA_ACCELERATION_FACTOR
  elseif getControlState("walk") then
    maxSpeed = MAX_SPEED * LOW_SPEED_FACTOR
    acceleration = acceleration * LOW_ACCELERATION_FACTOR
  end

  local DirectionModule = Direction:Module()

  -- Check if we must change the gravity
  if DirectionModule == 0 and self.currentSpeed ~= 0 then
    setGravity(0)
  else
    setGravity(serverGravity)
  end

  -- Calculate the new current speed
  if self.currentSpeed ~= 0 and (DirectionModule == 0 or self.currentSpeed > maxSpeed) then
    -- deccelerate
    self.currentSpeed = self.currentSpeed - acceleration
    if self.currentSpeed < 0 then self.currentSpeed = 0 end

  elseif DirectionModule ~= 0 and self.currentSpeed < maxSpeed then
    -- accelerate
    self.currentSpeed = self.currentSpeed + acceleration
    if self.currentSpeed > maxSpeed then self.currentSpeed = maxSpeed end

  end

  -- Calculate the movement requested direction
  if DirectionModule ~= 0 then
    Direction = Vector3D:new(SightDirection.x * Direction.y - SightDirection.y * Direction.x,
                             SightDirection.x * Direction.x + SightDirection.y * Direction.y,
                             SightDirection.z * Direction.y)
    -- Save the last movement direction for when player releases all direction keys
    self.lastDirection = Direction
  else
    -- Player is not specifying any direction, use last known direction or the current velocity
    if self.lastDirection then
      Direction = self.lastDirection
      if self.currentSpeed == 0 then self.lastDirection = nil end
    else
      Direction = Vector3D:new(getElementVelocity(localPlayer))
    end
  end
  Direction:Normalize()
  Direction = Direction:Mul(self.currentSpeed)

  -- Applicate a smooth direction change, if moving
  if self.currentSpeed > 0 then
    local VelocityDirection = Vector3D:new(getElementVelocity(localPlayer))
    VelocityDirection:Normalize()

    if math.sqrt(VelocityDirection.x^2 + VelocityDirection.y^2) > 0 then
      local DirectionAngle = getVector2DAngle(Direction)
      local VelocityAngle = getVector2DAngle(VelocityDirection)

      local diff = angleDiff(DirectionAngle, VelocityAngle)
      local calculatedAngle

      if diff >= 0 then 
        if diff > MAX_ANGLE_SPEED then
          calculatedAngle = VelocityAngle + MAX_ANGLE_SPEED
  else
    calculatedAngle = DirectionAngle
  end
      else
        if diff < MAX_ANGLE_SPEED then
          calculatedAngle = VelocityAngle - MAX_ANGLE_SPEED
  else
          calculatedAngle = DirectionAngle
        end
      end
      calculatedAngle = calculatedAngle % 360

      local DirectionModule2D = math.sqrt(Direction.x^2 + Direction.y^2)
      Direction.x = -DirectionModule2D*math.cos(math.rad(calculatedAngle))
      Direction.y = DirectionModule2D*math.sin(math.rad(calculatedAngle))
    end
  end

  if Direction:Module() == 0 then
  self.extraVelocity = { x = 0, y = 0, z = 0 }
  end
  
  -- Set the new velocity
  setElementVelocity(localPlayer, Direction.x + self.extraVelocity.x,
                                  Direction.y + self.extraVelocity.y,
                  Direction.z + self.extraVelocity.z)

  if self.extraVelocity.z > 0 then
    self.extraVelocity.z = self.extraVelocity.z - 1
  if self.extraVelocity.z < 0 then self.extraVelocity.z = 0 end
  elseif self.extraVelocity.z < 0 then
  self.extraVelocity.z = self.extraVelocity.z + 1
  if self.extraVelocity.z > 0 then self.extraVelocity.z = 0 end
  end
end



--
-- Players flight processing
--
function Superman.processFlight()
  local self = Superman

  for player in iterateFlyingPlayers() do
    local Velocity = Vector3D:new(getElementVelocity(player))
    local distanceToBase = getElementDistanceFromCentreOfMassToBaseOfModel(player)
    local playerPos = Vector3D:new(getElementPosition(player))
    playerPos.z = playerPos.z - distanceToBase

    local distanceToGround
    if playerPos.z > 0 then
      local hit, hitX, hitY, hitZ, hitElement = processLineOfSight(playerPos.x, playerPos.y, playerPos.z,
                                                                   playerPos.x, playerPos.y, playerPos.z - LANDING_DISTANCE - 1,
                                                                   true, true, true, true, true, false, false, false)
      if hit then distanceToGround = playerPos.z - hitZ end
    end

    if distanceToGround and distanceToGround < GROUND_ZERO_TOLERANCE then
      self:restorePlayer(player)
      if player == localPlayer then
        setGravity(serverGravity)
        triggerServerEvent("superman:stop", getRootElement())
      end
    elseif distanceToGround and distanceToGround < LANDING_DISTANCE then
      self:processLanding(player, Velocity, distanceToGround)
    elseif Velocity:Module() < ZERO_TOLERANCE then
      self:processIdleFlight(player)
    else
      self:processMovingFlight(player, Velocity)
    end
  end
end

function Superman:processIdleFlight(player)
  -- Set the proper animation on the player
  local animLib, animName = getPedAnimation(player)
  if animLib ~= IDLE_ANIMLIB or animName ~= IDLE_ANIMATION then
    setPedAnimation(player, IDLE_ANIMLIB, IDLE_ANIMATION, -1, IDLE_ANIM_LOOP, false, false)
  end

  setElementCollisionsEnabled(player, false)

  -- If this is myself, calculate the ped rotation depending on the camera rotation
  if player == localPlayer then
    local cameraX, cameraY, cameraZ, lookX, lookY, lookZ = getCameraMatrix()
    local Sight = Vector3D:new(lookX - cameraX, lookY - cameraY, lookZ - cameraZ)
    Sight:Normalize()
    if getControlState("look_behind") then
      Sight = Sight:Mul(-1)
    end

    Sight.z = math.atan(Sight.x / Sight.y)
    if Sight.y > 0 then
      Sight.z = Sight.z + math.pi
    end
    Sight.z = math.deg(Sight.z) + 180

    setPedRotation(localPlayer, Sight.z)
    setElementRotation(localPlayer, 0, 0, Sight.z)
  else
    local Zangle = getPedCameraRotation(player)
    setPedRotation(player, Zangle)
    setElementRotation(player, 0, 0, Zangle)
  end
end

function Superman:processMovingFlight(player, Velocity)
  -- Set the proper animation on the player
  local animLib, animName = getPedAnimation(player)
  if animLib ~= FLIGHT_ANIMLIB or animName ~= FLIGHT_ANIMATION then
    setPedAnimation(player, FLIGHT_ANIMLIB, FLIGHT_ANIMATION, -1, FLIGHT_ANIM_LOOP, true, false)
  end

  if player == localPlayer then
    setElementCollisionsEnabled(player, true)
  else
    setElementCollisionsEnabled(player, false)
  end

  -- Calculate the player rotation depending on their velocity
  local Rotation = Vector3D:new(0, 0, 0)
  if Velocity.x == 0 and Velocity.y == 0 then
    Rotation.z = getPedRotation(player)
  else
    Rotation.z = math.deg(math.atan(Velocity.x / Velocity.y))
    if Velocity.y > 0 then
      Rotation.z = Rotation.z - 180
    end
    Rotation.z = (Rotation.z + 180) % 360
  end
  Rotation.x = -math.deg(Velocity.z / Velocity:Module() * 1.2)

  -- Rotation compensation for the self animation rotation
  Rotation.x = Rotation.x - 40

  -- Calculate the Y rotation for barrel rotations
  if not self.rotations[player] then self.rotations[player] = 0 end
  if not self.previousVelocity[player] then self.previousVelocity[player] = Vector3D:new(0, 0, 0) end
  
  local previousAngle = getVector2DAngle(self.previousVelocity[player])
  local currentAngle = getVector2DAngle(Velocity)
  local diff = angleDiff(currentAngle, previousAngle)
  if isnan(diff) then
  diff = 0
  end
  local calculatedYRotation = -diff * MAX_Y_ROTATION / MAX_ANGLE_SPEED

  if calculatedYRotation > self.rotations[player] then
    if calculatedYRotation - self.rotations[player] > ROTATION_Y_SPEED then
      self.rotations[player] = self.rotations[player] + ROTATION_Y_SPEED
    else
      self.rotations[player] = calculatedYRotation
    end
  else
    if self.rotations[player] - calculatedYRotation > ROTATION_Y_SPEED then
      self.rotations[player] = self.rotations[player] - ROTATION_Y_SPEED
    else
      self.rotations[player] = calculatedYRotation
    end
  end

  if self.rotations[player] > MAX_Y_ROTATION then
    self.rotations[player] = MAX_Y_ROTATION
  elseif self.rotations[player] < -MAX_Y_ROTATION then
    self.rotations[player] = -MAX_Y_ROTATION
  elseif math.abs(self.rotations[player]) < ZERO_TOLERANCE then
    self.rotations[player] = 0
  end
  Rotation.y = self.rotations[player]

  -- Apply the calculated rotation
  setPedRotation(player, Rotation.z)
  setElementRotation(player, Rotation.x, Rotation.y, Rotation.z)

  -- Save the current velocity
  self.previousVelocity[player] = Velocity

  -- If the speed is over the given value, create the smoke generators
  if Velocity:Module() > (SMOKING_SPEED - ZERO_TOLERANCE) and not isPedInWater(player) then
    self:createSmokeGenerators(player)
  else
    self:destroySmokeGenerators(player)
  end
end

function Superman:processLanding(player, Velocity, distanceToGround)
  -- Set the proper animation on the player
  local animLib, animName = getPedAnimation(player)
  if animLib ~= FLIGHT_ANIMLIB or animName ~= FLIGHT_ANIMATION then
    setPedAnimation(player, FLIGHT_ANIMLIB, FLIGHT_ANIMATION, -1, FLIGHT_ANIM_LOOP, true, false)
  end

  if player == localPlayer then
    setElementCollisionsEnabled(player, true)
  else
    setElementCollisionsEnabled(player, false)
  end

  -- If the speed is over the given value, create the smoke generators
  if Velocity:Module() > (SMOKING_SPEED - ZERO_TOLERANCE) and not isPedInWater(player) then
    self:createSmokeGenerators(player)
  else
    self:destroySmokeGenerators(player)
  end

  -- Calculate the player rotation depending on their velocity and distance to ground
  local Rotation = Vector3D:new(0, 0, 0)
  if Velocity.x == 0 and Velocity.y == 0 then
    Rotation.z = getPedRotation(player)
  else
    Rotation.z = math.deg(math.atan(Velocity.x / Velocity.y))
    if Velocity.y > 0 then
      Rotation.z = Rotation.z - 180
    end
    Rotation.z = (Rotation.z + 180) % 360
  end
  Rotation.x = -(85 - (distanceToGround * 85 / LANDING_DISTANCE))

  -- Rotation compensation for the self animation rotation
  Rotation.x = Rotation.x - 40

  -- Apply the calculated rotation
  setPedRotation(player, Rotation.z)
  setElementRotation(player, Rotation.x, Rotation.y, Rotation.z)
end



--
-- Vectors
--
Vector3D = {
  new = function(self, _x, _y, _z)
    local newVector = { x = _x or 0.0, y = _y or 0.0, z = _z or 0.0 }
    return setmetatable(newVector, { __index = Vector3D })
  end,

  Copy = function(self)
    return Vector3D:new(self.x, self.y, self.z)
  end,

  Normalize = function(self)
    local mod = self:Module()
    if mod ~= 0 then
      self.x = self.x / mod
      self.y = self.y / mod
      self.z = self.z / mod
    end
  end,

  Dot = function(self, V)
    return self.x * V.x + self.y * V.y + self.z * V.z
  end,

  Module = function(self)
    return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  end,

  AddV = function(self, V)
    return Vector3D:new(self.x + V.x, self.y + V.y, self.z + V.z)
  end,

  SubV = function(self, V)
    return Vector3D:new(self.x - V.x, self.y - V.y, self.z - V.z)
  end,

  CrossV = function(self, V)
    return Vector3D:new(self.y * V.z - self.z * V.y,
                        self.z * V.x - self.x * V.z,
                        self.x * V.y - self.y * V.z)
  end,

  Mul = function(self, n)
    return Vector3D:new(self.x * n, self.y * n, self.z * n)
  end,

  Div = function(self, n)
    return Vector3D:new(self.x / n, self.y / n, self.z / n)
  end,

  MulV = function(self, V)
    return Vector3D:new(self.x * V.x, self.y * V.y, self.z * V.z)
  end,

  DivV = function(self, V)
    return Vector3D:new(self.x / V.x, self.y / V.y, self.z / V.z)
  end,
}

 

Link to comment
2 minutes ago, FaHaD said:

@Abdul KariM Some bug's will be in the script you can try this because there's a cmd and some velocity functions.

 @ProMax Try this :

 


-- Copyright (c) 2008, Alberto Alonso---- All rights reserved.---- Redistribution and use in source and binary forms, with or without modification,-- are permitted provided that the following conditions are met:----     * Redistributions of source code must retain the above copyright notice, this--       list of conditions and the following disclaimer.--     * Redistributions in binary form must reproduce the above copyright notice, this--       list of conditions and the following disclaimer in the documentation and/or other--       materials provided with the distribution.--     * Neither the name of the superman script nor the names of its contributors may be used--       to endorse or promote products derived from this software without specific prior--       written permission.---- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,-- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,-- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.local Superman = {}-- Settingslocal ZERO_TOLERANCE = 0.00001local MAX_ANGLE_SPEED = 6 -- In degrees per framelocal MAX_SPEED = 1.0local EXTRA_SPEED_FACTOR = 1.85local LOW_SPEED_FACTOR = 0.40local ACCELERATION = 0.025local EXTRA_ACCELERATION_FACTOR = 1.8local LOW_ACCELERATION_FACTOR = 0.85local TAKEOFF_VELOCITY = 1.75local TAKEOFF_FLIGHT_DELAY = 750local SMOKING_SPEED = 1.25local GROUND_ZERO_TOLERANCE = 0.18local LANDING_DISTANCE = 3.2local FLIGHT_ANIMLIB = "swim"local FLIGHT_ANIMATION = "Swim_Dive_Under"local FLIGHT_ANIM_LOOP = falselocal IDLE_ANIMLIB = "cop_ambient"local IDLE_ANIMATION = "Coplook_loop"local IDLE_ANIM_LOOP = truelocal MAX_Y_ROTATION = 55local ROTATION_Y_SPEED = 3.8-- Static global variableslocal thisResource = getThisResource()local rootElement = getRootElement()local localPlayer = getLocalPlayer()local serverGravity = getGravity()---- Utility functions--local function isPlayerFlying(player)  local data = getElementData(player, "superman:flying")  if not data or data == false then return false  else return true endendlocal function setPlayerFlying(player, state)  if state == true then state = true  else state = false end  setElementData(player, "superman:flying", state)endlocal function iterateFlyingPlayers()  local current = 1  local allPlayers = getElementsByType("player")  return function()    local player        repeat      player = allPlayers[current]      current = current + 1    until not player or (isPlayerFlying(player) and isElementStreamedIn(player))    return player  endendfunction Superman:restorePlayer(player)  setPlayerFlying(player, false)  setPedAnimation(player, false)  setElementVelocity(player, 0, 0, 0)  setElementRotation(player, 0, 0, 0)  --setPedRotation(player, getPedRotation(player))  setElementCollisionsEnabled(player, true)  self:destroySmokeGenerators(player)  self.rotations[player] = nil  self.previousVelocity[player] = nilendfunction Superman:createSmokeGenerator(player)  local generator = createObject(2780, getElementPosition(player))  setElementCollisionsEnabled(generator, false)  setObjectScale(generator, 0)  return generatorendfunction Superman:createSmokeGenerators(player)  if not self.smokeGenerators[player] then    local smokeGenerators = {}    smokeGenerators[1] = self:createSmokeGenerator(player)    attachElementToElement(smokeGenerators[1], player, 0.75, -0.2, -0.4, -40, 0, 60)    smokeGenerators[2] = self:createSmokeGenerator(player)    attachElementToElement(smokeGenerators[2], player, -0.75, -0.2, -0.4, -40, 0, -60)    self.smokeGenerators[player] = smokeGenerators  endendfunction Superman:destroySmokeGenerators(player)  if self.smokeGenerators[player] then    for k, v in ipairs(self.smokeGenerators[player]) do      destroyElement(v)    end    self.smokeGenerators[player] = nil  endendfunction angleDiff(angle1, angle2)  angle1, angle2 = angle1 % 360, angle2 % 360  local diff = (angle1 - angle2) % 360  if diff <= 180 then    return diff  else    return -(360 - diff)  endendlocal function isPedInWater(ped)  local pedPosition = Vector3D:new(getElementPosition(ped))  if pedPosition.z <= 0 then return true end  local waterLevel = getWaterLevel(pedPosition.x, pedPosition.y, pedPosition.z)  if not isElementStreamedIn(ped) or not waterLevel or waterLevel < pedPosition.z then    return false  else    return true  endendlocal function isnan(x)  math.inf = 1/0  if x == math.inf or x == -math.inf or x ~= x then    return true  end  return falseendlocal function getVector2DAngle(vec)  if vec.x == 0 and vec.y == 0 then return 0 end  local angle = math.deg(math.atan(vec.x / vec.y)) + 90  if vec.y < 0 then    angle = angle + 180  end  return angleend---- Initialization and shutdown functions--function Superman.Start()  local self = Superman  -- Register events  addEventHandler("onClientResourceStop", getResourceRootElement(thisResource), Superman.Stop, false)  addEventHandler("onPlayerJoin", rootElement, Superman.onJoin)  addEventHandler("onPlayerQuit", rootElement, Superman.onQuit)  addEventHandler("onClientRender", rootElement, Superman.processControls)  addEventHandler("onClientRender", rootElement, Superman.processFlight)  addEventHandler("onClientPlayerDamage", localPlayer, Superman.onDamage, false)  addEventHandler("onClientElementDataChange", rootElement, Superman.onDataChange)  addEventHandler("onClientElementStreamIn", rootElement, Superman.onStreamIn)  addEventHandler("onClientElementStreamOut", rootElement, Superman.onStreamOut)  -- Bind keys  bindKey("jump", "down", Superman.onJump)  -- Register commands  addCommandHandler("superman", Superman.cmdSuperman)  -- Initializate attributes  self.smokeGenerators = {}  self.rotations = {}  self.previousVelocity = {}endaddEventHandler("onClientResourceStart", getResourceRootElement(thisResource), Superman.Start, false)function Superman.Stop()  local self = Superman  setGravity(serverGravity)  -- Restore all players animations, collisions, etc  for player in iterateFlyingPlayers() do    self:restorePlayer(player)  endend---- Join/Quit--function Superman.onJoin(player)  local self = Superman  local player = player or source  setPlayerFlying(player, false)endfunction Superman.onQuit(reason, player)  local self = Superman  local player = player or source  if isPlayerFlying(player) then    self:restorePlayer(player)  endend---- onDamage: superman is invulnerable--function Superman.onDamage()  local self = Superman  if isPlayerFlying(localPlayer) then    cancelEvent()  endend---- onStreamIn: Reset rotation attribute for player--function Superman.onStreamIn()  local self = Supermanendfunction Superman.onStreamOut()  local self = Superman  if source and isElement(source) and getElementType(source) == "player" and isPlayerFlying(source) then    self.rotations[source] = nil    self.previousVelocity[source] = nil  endend---- onDataChange: Check if somebody who is out of stream stops being superman--function Superman.onDataChange(dataName, oldValue)  local self = Superman  if dataName == "superman:flying" and isElement(source) and getElementType(source) == "player" and     oldValue ~= getElementData(source, dataName) and oldValue == true and getElementData(source, dataName) == false then    self:restorePlayer(source)  endend---- onJump: Combo to start flight without any command--function Superman.onJump(key, keyState)   local aTeam = getPlayerTeam ( localPlayer )   if not aTeam or aTeam ~= getTeamFromName ( 'Staff' ) then     return outputChatBox ( 'Superman is for staff only',255,0,0 ) end      local self = Superman  local self = Superman  local task = getPlayerSimplestTask(localPlayer)  if not isPlayerFlying(localPlayer) then  if task == "TASK_SIMPLE_IN_AIR" then    setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)      setTimer(Superman.startFlight, 100, 1)  end  endend---- Commands--function Superman.cmdSuperman()       local aTeam = getPlayerTeam ( localPlayer )      if aTeam and aTeam == getTeamFromName ( 'Staff' ) then       local self = Superman      if isPedInVehicle(localPlayer) or isPlayerFlying(localPlayer) then return end      setElementVelocity(localPlayer, 0, 0, TAKEOFF_VELOCITY)      setTimer(Superman.startFlight, TAKEOFF_FLIGHT_DELAY, 1)else      outputChatBox ( 'Superman is for staff only',255,0,0 )    endendfunction Superman.startFlight()        local aTeam = getPlayerTeam ( localPlayer )        if aTeam and aTeam == getTeamFromName ( 'Staff' ) then         local self = Superman        if isPlayerFlying(localPlayer) then return end        triggerServerEvent("superman:start", rootElement)        setPlayerFlying(localPlayer, true)        setElementVelocity(localPlayer, 0, 0, 0)        self.currentSpeed = 0        self.extraVelocity = { x = 0, y = 0, z = 0 }  else        outputChatBox ( 'Superman is for staff only',255,0,0 )    endend---- Controls processing--function Superman.processControls()  local self = Superman  if not isPlayerFlying(localPlayer) then return end  -- Calculate the requested movement direction  local Direction = Vector3D:new(0, 0, 0)  if getControlState("forwards") then    Direction.y = 1  elseif getControlState("backwards") then    Direction.y = -1  end  if getControlState("left") then    Direction.x = 1  elseif getControlState("right") then    Direction.x = -1  end  Direction:Normalize()  -- Calculate the sight direction  local cameraX, cameraY, cameraZ, lookX, lookY, lookZ = getCameraMatrix()  local SightDirection = Vector3D:new((lookX - cameraX), (lookY - cameraY), (lookZ - cameraZ))  SightDirection:Normalize()  if getControlState("look_behind") then    SightDirection = SightDirection:Mul(-1)  end  -- Calculate the current max speed and acceleration values  local maxSpeed = MAX_SPEED  local acceleration = ACCELERATION  if getControlState("sprint") then    maxSpeed = MAX_SPEED * EXTRA_SPEED_FACTOR    acceleration = acceleration * EXTRA_ACCELERATION_FACTOR  elseif getControlState("walk") then    maxSpeed = MAX_SPEED * LOW_SPEED_FACTOR    acceleration = acceleration * LOW_ACCELERATION_FACTOR  end  local DirectionModule = Direction:Module()  -- Check if we must change the gravity  if DirectionModule == 0 and self.currentSpeed ~= 0 then    setGravity(0)  else    setGravity(serverGravity)  end  -- Calculate the new current speed  if self.currentSpeed ~= 0 and (DirectionModule == 0 or self.currentSpeed > maxSpeed) then    -- deccelerate    self.currentSpeed = self.currentSpeed - acceleration    if self.currentSpeed < 0 then self.currentSpeed = 0 end  elseif DirectionModule ~= 0 and self.currentSpeed < maxSpeed then    -- accelerate    self.currentSpeed = self.currentSpeed + acceleration    if self.currentSpeed > maxSpeed then self.currentSpeed = maxSpeed end  end  -- Calculate the movement requested direction  if DirectionModule ~= 0 then    Direction = Vector3D:new(SightDirection.x * Direction.y - SightDirection.y * Direction.x,                             SightDirection.x * Direction.x + SightDirection.y * Direction.y,                             SightDirection.z * Direction.y)    -- Save the last movement direction for when player releases all direction keys    self.lastDirection = Direction  else    -- Player is not specifying any direction, use last known direction or the current velocity    if self.lastDirection then      Direction = self.lastDirection      if self.currentSpeed == 0 then self.lastDirection = nil end    else      Direction = Vector3D:new(getElementVelocity(localPlayer))    end  end  Direction:Normalize()  Direction = Direction:Mul(self.currentSpeed)  -- Applicate a smooth direction change, if moving  if self.currentSpeed > 0 then    local VelocityDirection = Vector3D:new(getElementVelocity(localPlayer))    VelocityDirection:Normalize()    if math.sqrt(VelocityDirection.x^2 + VelocityDirection.y^2) > 0 then      local DirectionAngle = getVector2DAngle(Direction)      local VelocityAngle = getVector2DAngle(VelocityDirection)      local diff = angleDiff(DirectionAngle, VelocityAngle)      local calculatedAngle      if diff >= 0 then         if diff > MAX_ANGLE_SPEED then          calculatedAngle = VelocityAngle + MAX_ANGLE_SPEED  else    calculatedAngle = DirectionAngle  end      else        if diff < MAX_ANGLE_SPEED then          calculatedAngle = VelocityAngle - MAX_ANGLE_SPEED  else          calculatedAngle = DirectionAngle        end      end      calculatedAngle = calculatedAngle % 360      local DirectionModule2D = math.sqrt(Direction.x^2 + Direction.y^2)      Direction.x = -DirectionModule2D*math.cos(math.rad(calculatedAngle))      Direction.y = DirectionModule2D*math.sin(math.rad(calculatedAngle))    end  end  if Direction:Module() == 0 then  self.extraVelocity = { x = 0, y = 0, z = 0 }  end    -- Set the new velocity  setElementVelocity(localPlayer, Direction.x + self.extraVelocity.x,                                  Direction.y + self.extraVelocity.y,                  Direction.z + self.extraVelocity.z)  if self.extraVelocity.z > 0 then    self.extraVelocity.z = self.extraVelocity.z - 1  if self.extraVelocity.z < 0 then self.extraVelocity.z = 0 end  elseif self.extraVelocity.z < 0 then  self.extraVelocity.z = self.extraVelocity.z + 1  if self.extraVelocity.z > 0 then self.extraVelocity.z = 0 end  endend---- Players flight processing--function Superman.processFlight()  local self = Superman  for player in iterateFlyingPlayers() do    local Velocity = Vector3D:new(getElementVelocity(player))    local distanceToBase = getElementDistanceFromCentreOfMassToBaseOfModel(player)    local playerPos = Vector3D:new(getElementPosition(player))    playerPos.z = playerPos.z - distanceToBase    local distanceToGround    if playerPos.z > 0 then      local hit, hitX, hitY, hitZ, hitElement = processLineOfSight(playerPos.x, playerPos.y, playerPos.z,                                                                   playerPos.x, playerPos.y, playerPos.z - LANDING_DISTANCE - 1,                                                                   true, true, true, true, true, false, false, false)      if hit then distanceToGround = playerPos.z - hitZ end    end    if distanceToGround and distanceToGround < GROUND_ZERO_TOLERANCE then      self:restorePlayer(player)      if player == localPlayer then        setGravity(serverGravity)        triggerServerEvent("superman:stop", getRootElement())      end    elseif distanceToGround and distanceToGround < LANDING_DISTANCE then      self:processLanding(player, Velocity, distanceToGround)    elseif Velocity:Module() < ZERO_TOLERANCE then      self:processIdleFlight(player)    else      self:processMovingFlight(player, Velocity)    end  endendfunction Superman:processIdleFlight(player)  -- Set the proper animation on the player  local animLib, animName = getPedAnimation(player)  if animLib ~= IDLE_ANIMLIB or animName ~= IDLE_ANIMATION then    setPedAnimation(player, IDLE_ANIMLIB, IDLE_ANIMATION, -1, IDLE_ANIM_LOOP, false, false)  end  setElementCollisionsEnabled(player, false)  -- If this is myself, calculate the ped rotation depending on the camera rotation  if player == localPlayer then    local cameraX, cameraY, cameraZ, lookX, lookY, lookZ = getCameraMatrix()    local Sight = Vector3D:new(lookX - cameraX, lookY - cameraY, lookZ - cameraZ)    Sight:Normalize()    if getControlState("look_behind") then      Sight = Sight:Mul(-1)    end    Sight.z = math.atan(Sight.x / Sight.y)    if Sight.y > 0 then      Sight.z = Sight.z + math.pi    end    Sight.z = math.deg(Sight.z) + 180    setPedRotation(localPlayer, Sight.z)    setElementRotation(localPlayer, 0, 0, Sight.z)  else    local Zangle = getPedCameraRotation(player)    setPedRotation(player, Zangle)    setElementRotation(player, 0, 0, Zangle)  endendfunction Superman:processMovingFlight(player, Velocity)  -- Set the proper animation on the player  local animLib, animName = getPedAnimation(player)  if animLib ~= FLIGHT_ANIMLIB or animName ~= FLIGHT_ANIMATION then    setPedAnimation(player, FLIGHT_ANIMLIB, FLIGHT_ANIMATION, -1, FLIGHT_ANIM_LOOP, true, false)  end  if player == localPlayer then    setElementCollisionsEnabled(player, true)  else    setElementCollisionsEnabled(player, false)  end  -- Calculate the player rotation depending on their velocity  local Rotation = Vector3D:new(0, 0, 0)  if Velocity.x == 0 and Velocity.y == 0 then    Rotation.z = getPedRotation(player)  else    Rotation.z = math.deg(math.atan(Velocity.x / Velocity.y))    if Velocity.y > 0 then      Rotation.z = Rotation.z - 180    end    Rotation.z = (Rotation.z + 180) % 360  end  Rotation.x = -math.deg(Velocity.z / Velocity:Module() * 1.2)  -- Rotation compensation for the self animation rotation  Rotation.x = Rotation.x - 40  -- Calculate the Y rotation for barrel rotations  if not self.rotations[player] then self.rotations[player] = 0 end  if not self.previousVelocity[player] then self.previousVelocity[player] = Vector3D:new(0, 0, 0) end    local previousAngle = getVector2DAngle(self.previousVelocity[player])  local currentAngle = getVector2DAngle(Velocity)  local diff = angleDiff(currentAngle, previousAngle)  if isnan(diff) then  diff = 0  end  local calculatedYRotation = -diff * MAX_Y_ROTATION / MAX_ANGLE_SPEED  if calculatedYRotation > self.rotations[player] then    if calculatedYRotation - self.rotations[player] > ROTATION_Y_SPEED then      self.rotations[player] = self.rotations[player] + ROTATION_Y_SPEED    else      self.rotations[player] = calculatedYRotation    end  else    if self.rotations[player] - calculatedYRotation > ROTATION_Y_SPEED then      self.rotations[player] = self.rotations[player] - ROTATION_Y_SPEED    else      self.rotations[player] = calculatedYRotation    end  end  if self.rotations[player] > MAX_Y_ROTATION then    self.rotations[player] = MAX_Y_ROTATION  elseif self.rotations[player] < -MAX_Y_ROTATION then    self.rotations[player] = -MAX_Y_ROTATION  elseif math.abs(self.rotations[player]) < ZERO_TOLERANCE then    self.rotations[player] = 0  end  Rotation.y = self.rotations[player]  -- Apply the calculated rotation  setPedRotation(player, Rotation.z)  setElementRotation(player, Rotation.x, Rotation.y, Rotation.z)  -- Save the current velocity  self.previousVelocity[player] = Velocity  -- If the speed is over the given value, create the smoke generators  if Velocity:Module() > (SMOKING_SPEED - ZERO_TOLERANCE) and not isPedInWater(player) then    self:createSmokeGenerators(player)  else    self:destroySmokeGenerators(player)  endendfunction Superman:processLanding(player, Velocity, distanceToGround)  -- Set the proper animation on the player  local animLib, animName = getPedAnimation(player)  if animLib ~= FLIGHT_ANIMLIB or animName ~= FLIGHT_ANIMATION then    setPedAnimation(player, FLIGHT_ANIMLIB, FLIGHT_ANIMATION, -1, FLIGHT_ANIM_LOOP, true, false)  end  if player == localPlayer then    setElementCollisionsEnabled(player, true)  else    setElementCollisionsEnabled(player, false)  end  -- If the speed is over the given value, create the smoke generators  if Velocity:Module() > (SMOKING_SPEED - ZERO_TOLERANCE) and not isPedInWater(player) then    self:createSmokeGenerators(player)  else    self:destroySmokeGenerators(player)  end  -- Calculate the player rotation depending on their velocity and distance to ground  local Rotation = Vector3D:new(0, 0, 0)  if Velocity.x == 0 and Velocity.y == 0 then    Rotation.z = getPedRotation(player)  else    Rotation.z = math.deg(math.atan(Velocity.x / Velocity.y))    if Velocity.y > 0 then      Rotation.z = Rotation.z - 180    end    Rotation.z = (Rotation.z + 180) % 360  end  Rotation.x = -(85 - (distanceToGround * 85 / LANDING_DISTANCE))  -- Rotation compensation for the self animation rotation  Rotation.x = Rotation.x - 40  -- Apply the calculated rotation  setPedRotation(player, Rotation.z)  setElementRotation(player, Rotation.x, Rotation.y, Rotation.z)end---- Vectors--Vector3D = {  new = function(self, _x, _y, _z)    local newVector = { x = _x or 0.0, y = _y or 0.0, z = _z or 0.0 }    return setmetatable(newVector, { __index = Vector3D })  end,  Copy = function(self)    return Vector3D:new(self.x, self.y, self.z)  end,  Normalize = function(self)    local mod = self:Module()    if mod ~= 0 then      self.x = self.x / mod      self.y = self.y / mod      self.z = self.z / mod    end  end,  Dot = function(self, V)    return self.x * V.x + self.y * V.y + self.z * V.z  end,  Module = function(self)    return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)  end,  AddV = function(self, V)    return Vector3D:new(self.x + V.x, self.y + V.y, self.z + V.z)  end,  SubV = function(self, V)    return Vector3D:new(self.x - V.x, self.y - V.y, self.z - V.z)  end,  CrossV = function(self, V)    return Vector3D:new(self.y * V.z - self.z * V.y,                        self.z * V.x - self.x * V.z,                        self.x * V.y - self.y * V.z)  end,  Mul = function(self, n)    return Vector3D:new(self.x * n, self.y * n, self.z * n)  end,  Div = function(self, n)    return Vector3D:new(self.x / n, self.y / n, self.z / n)  end,  MulV = function(self, V)    return Vector3D:new(self.x * V.x, self.y * V.y, self.z * V.z)  end,  DivV = function(self, V)    return Vector3D:new(self.x / V.x, self.y / V.y, self.z / V.z)  end,}

 

i still can fly with out being in the staff team

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