Jump to content

Mount a minigun to a vehicle


Xwad

Recommended Posts

Hi i have a script that makes possible to mount a minigun to a vehicle but id does not work.

server.lua

local guns = 38

{

[470] =

{

["xPosOffset"] = 1.0,

["yPosOffset"] = 0,

["zPosOffset"] = 0

}

}

function attachMod(playerSource, commandName)

local vehicle = getPlayerOccupiedVehicle(playerSource)

local gun = false

if (vehicle) then

gun = guns[getVehicleID(vehicle)] or false

if (gun) then

attachElementToElement(createObject(2985, 0, 0, -100, 0, 0, 0), vehicle, gun.xPosOffset, gun.yPosOffset, gun.zPosOffset, 0, 0, 90)

setElementData(vehicle, "toggleVehicleWeapon", true)

end

end

end

function detachMod(playerSource, commandName)

local vehicle = getPlayerOccupiedVehicle(playerSource)

if (vehicle) then

for _, element in ipairs(getAttachedElements(vehicle)) do

destroyElement(element)

end

setElementData(vehicle, "toggleVehicleWeapon", false)

end

end

addCommandHandler("/attachmod", attachMod, false)

addCommandHandler("/detachmod", detachMod, false)

client.lua

local root = getRootElement()

local player = getLocalPlayer()

local toggleVehicleWeapon = false

local previousTick = getTickCount()

local projectileType = 21

function updateVehicleWeapon(source, dataName)

if (getElementType(source) == "vehicle" and dataName == "toggleVehicleWeapon") then

toggleVehicleWeapon = (getElementData(source, dataName) and isPlayerInVehicle(player))

end

end

function callbackDataChange(dataName)

updateVehicleWeapon(source, dataName)

end

Link to comment
addCommandHandler("/attachmod", attachMod, false) 
addCommandHandler("/detachmod", detachMod, false) 

should be

addCommandHandler("attachmod", attachMod, false) 
addCommandHandler("detachmod", detachMod, false) 

And there isn't any existing function with the name ToggleVehicleWeapon.

Link to comment
  • 2 weeks later...

try this

server

DON'T FORGET IT WORKS ONLY WHEN YOU're ON PATRIOT CAR

guns = { 
["xPosOffset"] = 1.0, 
["yPosOffset"] = 0, 
["zPosOffset"] = 0, 
} 
  
function attachMod(playerSource, commandName) 
if isElement(playerSource) and getElementType(playerSource)=="player" then  
local vehicle = getPedOccupiedVehicle(playerSource) 
if (vehicle) then 
vehMod = getVehicleModel(vehicle) 
if (vehMod == 470) then 
attachElementToElement(createObject(2985, 0, 0, -100, 0, 0, 0), vehicle, guns.xPosOffset, guns.yPosOffset, guns.zPosOffset, 0, 0, 90) 
setElementData(vehicle, "toggleVehicleWeapon", true) 
end 
end 
end 
end 
  
function detachMod(playerSource, commandName) 
local vehicle = getPedOccupiedVehicle(playerSource) 
if (vehicle) then 
for _, element in ipairs(getAttachedElements(vehicle)) do 
destroyElement(element) 
end 
setElementData(vehicle, "toggleVehicleWeapon", false) 
end 
end 
  
addCommandHandler("attachmod", attachMod, false) 
addCommandHandler("detachmod", detachMod, false) 

Link to comment

Look I'll show you an example code of how this works..

function attaching () 
   if not isElement(mg) then 
   mg = createWeapon(write the minigun things and all) 
   veh = getPedOccupiedVehicle(localPlayer) 
   attachElement(veh, position on the veh where u want to attach) 
   else 
   destroyElement(mg) 
   end 
end 
addCommandHandler("attach", attaching) 
  
function wepFire () 
   if isElement(mg) then 
   fireWeapon(mg, or something like that) 
   end 
end 
addCommandHandler("fire", wepFire) 

Link to comment

xD I can't make it you will laught that is what i made xdd

  
function MountMinigun() 
    local vehicle = getElementPosition(getLocalPlayer()) 
    local weapon = createWeapon("minigun", x, y, z + 1) 
    setWeaponClipAmmo(weapon, 5000) 
    setWeaponState(weapon, "firing") 
    setWeaponProperty(weapon, "fire_rotation", 0, -30, 0) 
end 
addEventHandler("onClientResourceStart", resourceRoot, MountMinigun) 
  
  
  
  
  
local randPlayer = getRandomPlayer() -- Get a random player 
giveWeapon(randPlayer,35,100) -- Give them a rocket launcher with 100 rockets. 
setWeaponAmmo(randPlayer,35,50) -- Decide we're only going to give them 50 rockets. 
  
  
  
   if not isElement(minigun) then 
   minigun = createWeapon(38) 
   veh = getPedOccupiedVehicle(localPlayer) 
   attachElement(veh, 0, 0, 3) 
   else 
   destroyElement(minigun) 
   end 
end 
addCommandHandler("attach", attaching) 
  
  
   if isElement(minigun) then 
   fireWeapon(minigun, or something like that) 
   end 
end 
addCommandHandler("fire", wepFire) 
  

Link to comment

Xwad, you're trying to use server functions in a client-side script. This will not work.

Also, you're getting the element position of the player, not the vehicle. Also, you're only storing the X co-ordinate, not the Y and Z co-ordinate. You need to make it something like this;

local vX, vY, vZ = getElementPosition(getPedOccupiedVehicle(localPlayer)) 

This gets the position of the vehicle the local player is using.

setWeaponProperty, getRandomPlayer, giveWeapon and setWeaponAmmo are server-side functions.

After this I have no idea what you're trying to do, I can't tell if you messed up your paste, but you haven't made any functions - only if statements.

There are several solutions to fire the weapon, you could have a check in a timer or a render event to check if the left mouse-button is being held down using getKeyState. Or you can use the function bindKey or the event onClientClick.

Take some time and read the MTA Wiki thoroughly, it will be your best friend once you learn how to navigate it to find what you need.

Link to comment

guys guys, he is not wanting a weapon, he is trying to make a mounted vehicle, example like a rhino or a hydra.. I hope you understood and for that he has to make a weapon, attach it to a vehicle and then use it.. and yeah he has to make it all both in server and in client side he has to trigger a lot of things if he needs to work with the position of the player or a ped... ike this x, y, z = getElementPosition(localPlayer) or something like that. and he has to render it if he is using it globally..

This should work for him.

Link to comment

I know what he wants to do, I simply corrected his mistakes.

I've been working on a similar resource from time to time, which lets one player enter as a gunner in a vehicle, where he can control the gun, look around and fire etc.

I've been meaning to look into releasing it, but it needs more work still. :(

Link to comment
function posRender () 
   x, y, z = getElementPosition(localPlayer) 
   vx, vy, vz = getElementPosition(veh) 
end 
addEventHandler("onClientRender", root, posRender) 
  
local veh = createVehicle(441, x+3, y, z+1) 
local mini = createWeapon("minigun", vx, vy, vz+2) 
  
  
 function attaching () 
   if not isElement(mini) then 
   mini = createWeapon("minigun", vx, vy, vz + 2) 
   veh = getPedOccupiedVehicle(localPlayer) 
   else 
   destroyElement(mini) 
   end 
end 
addCommandHandler("attach", attaching) -- do /attach to make minigun or destroy it... 
  
 function wepFire() 
   if isElement(mini) then 
   fireWeapon(mini) 
   end 
end 
addCommandHandler("fire", wepFire) 

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