Jump to content

Push script


h4x7o0r

Recommended Posts

Hi guys, I have a "push" script that works just for the player who use it .I wanna change it somehow that only admins be able to push online players. Then i wanna integrate it into a GUI (maybe when i press "N" for example, the panel will be shown) that i can select a player from a list of active players and then to have a "PUSH" button .

Here is the my script :

  
function push () 
player=getLocalPlayer() -- I think i need to modify something  
    if isPedInVehicle(player) then 
        local vehicle=getPedOccupiedVehicle(player) 
x,y,z=getElementVelocity(vehicle) 
  
setElementVelocity ( vehicle, x, y, z+0.25) 
   
end 
end 
  
addCommandHandler ( "push", push, true) 
  

I know that i need to find the "target" player and also i need this :

  
function findPlayer(name) 
    if not name then return false end 
    local name = name:lower() 
    for i, p in ipairs(getElementsByType("player")) do 
        local fullname = getPlayerName(p):lower() 
        if string.find(fullname:gsub("#%x%x%x%x%x%x", ""), name, 1, true) then 
            return p 
        end 
    end 
    return false 
end 
  

How can i put all togheter ?

Link to comment
function push (thePlayer, commandName, namepart) 
     player = findPlayer(namepart) 
      if not player then 
        outputChatBox("* /push: player not found", thePlayer) 
      elseif isPedInVehicle(player) then 
        local vehicle = getPedOccupiedVehicle(player) 
        x,y,z = getElementVelocity(vehicle) 
        setElementVelocity ( vehicle, x, y, z + 0.25 ) 
  
   end 
    
  end 
  
   addCommandHandler("push", push) 
  

Try this

Link to comment

SetElementVelocity wont work alone , i suggest to use getElementSpeed+setElementSpeed from Useful functions :

https://wiki.multitheftauto.com/wiki/SetElementSpeed

https://wiki.multitheftauto.com/wiki/GetElementSpeed

  
 function push (thePlayer, commandName, namepart) 
if hasObjectPermissionTo ( thePlayer, "function.banPlayer",false ) then -- check if he has permissions 
     player = findPlayer(namepart) 
      if not player then 
        outputChatBox("* /push: player not found", thePlayer) 
      return end 
      if isPedInVehicle(player) then 
        local vehicle = getPedOccupiedVehicle(player) 
        local push = 60 
        setElementSpeed ( vehicle,"mph", getElementSpeed(vehicle, "mph") + push ) 
  
    
   end 
   end 
end 
addCommandHandler("push", push) 
  
function setElementSpeed(element, unit, speed) -- only work if element is moving! 
    if (unit == nil) then unit = 0 end 
    if (speed == nil) then speed = 0 end 
    speed = tonumber(speed) 
    local acSpeed = getElementSpeed(element, unit) 
    if (acSpeed~=false) then -- if true - element is valid, no need to check again 
        local diff = speed/acSpeed 
        local x,y,z = getElementVelocity(element) 
        setElementVelocity(element,x*diff,y*diff,z*diff) 
        return true 
    end 
  
    return false 
end 
  
function getElementSpeed(element,unit) 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
  
  
function findPlayer(name) 
    if not name then return false end 
    local name = name:lower() 
    for i, p in ipairs(getElementsByType("player")) do 
        local fullname = getPlayerName(p):lower() 
        if string.find(fullname:gsub("#%x%x%x%x%x%x", ""), name, 1, true) then 
            return p 
        end 
    end 
    return false 
end 
  
  

Link to comment

@Al3grab: Do you even know what are you saying? because it makes no sense ;).

function push (thePlayer, commandName, namepart) 
     local player = findPlayer(namepart) 
     if (not player) then 
           outputChatBox("* /push: player not found", thePlayer) 
      else 
        local vehicle = getPedOccupiedVehicle(player) 
        if (not vehicle) then outputChatBox("* /push: This player is not in a vehicle", thePlayer) return end 
        local x,y,z = getElementVelocity(vehicle) 
        setElementVelocity ( vehicle, x, y, z + 0.25 ) 
      end    
end 
addCommandHandler("push", push) 
  
function findPlayer(name) 
    if not name then return false end 
    local name = name:lower() 
    for i, p in ipairs(getElementsByType("player")) do 
        local fullname = getPlayerName(p):lower() 
        if string.find(fullname:gsub("#%x%x%x%x%x%x", ""), name, 1, true) then 
            return p 
        end 
    end 
    return false 
end 

Edited by Guest
Link to comment

Thank you very much guys, but it looks like's not working.

GTX - I don't have any error just a public message "/push: player not found" so the player wasn't found.

Al3grab - error line 3 - attempt to call global "hasObjectPermissionTo" (a nil value)

I appreciate your help.

LE: thank you solidsnake14 - (same like gtx) no errors but it says "/push: player not found" .

Link to comment
function push (thePlayer, commandName, namepart) 
     local player = findPlayer(namepart) 
     if (not player) then 
           outputChatBox("* /push: player not found", thePlayer) 
      else 
        local vehicle = getPedOccupiedVehicle(player) 
        if (not vehicle) then outputChatBox("* /push: This player is not in a vehicle", thePlayer) return end 
        local x,y,z = getElementVelocity(vehicle) 
        setElementVelocity ( vehicle, x, y, z + 0.25 ) 
      end   
end 
addCommandHandler("push", push) 
  
function findPlayer(playerPart) 
  local pl = getPlayerFromName(playerPart) 
  if isElement(pl) then 
    return pl 
  else 
    for i,v in ipairs (getElementsByType ("player")) do 
      if (string.find(getPlayerName(v),playerPart)) then 
        return v 
      end 
    end 
  end 
end 

Edited by Guest
Link to comment

Thank you very much SolidSnake14 for all your help.

I've modified the findPlayer function and not it works as i want . Here's is the final code :

  
  
    function push (thePlayer, commandName, namepart) 
         local player = findPlayer(namepart) 
         if (not player) then 
               outputChatBox("* /push: player not found", thePlayer) 
          else 
            local vehicle = getPedOccupiedVehicle(player) 
            if (not vehicle) then outputChatBox("* /push: This player is not in a vehicle", thePlayer) return end 
            local x,y,z = getElementVelocity(vehicle) 
            setElementVelocity ( vehicle, x, y, z + 0.25 ) 
          end   
    end 
    addCommandHandler("push", push) 
  
     
             
    function findPlayer(thePlayer) 
        if not thePlayer then return false end 
        local thePlayer = thePlayer:lower() 
        for i, p in ipairs(getElementsByType("player")) do 
            local fullname = getPlayerName(p):lower() 
            if string.find(fullname:gsub("#%x%x%x%x%x%x", ""), thePlayer, 1, true) then 
                return p 
            end 
        end 
        return false 
        end 

And now maybe another hard part :( i don't have any ideea how can i make that GUI ?:(

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