Jump to content

Recommended Posts

Hello there,

I've been having some trouble with the DestroyElement function. (destroyHydraGun function, line 42-51)

The rest of the script works fine. An uzi is created when a player enters a Hydra, but it doesn't seem to destroy the uzi when a player exits the vehicle.

What am I doing wrong?

function hydraFunctions() -- ignore this function
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		    outputChatBox ("Hydra guns installed")
		    toggleControl ( "accelerate", true )
	        toggleControl ( "brake_reverse", true )
            toggleControl ( "vehicle_secondary_fire", false )
		    toggleControl ( "vehicle_fire", false )
		    setControlState ( "special_control_up", true)
		    toggleControl ( "special_control_down", false)
		    local x, y, z = getElementPosition(vehicle)
            local weapon = createWeapon("uzi", x, y, z)
		    attachElements ( weapon, vehicle, 0, 4, 1, 0, 0, 90)
        end
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), hydraFunctions )

function hydraFiring() -- ignore this function
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		setWeaponClipAmmo(weapon, 500)
		setWeaponFiringRate(weapon, 90)
		setWeaponState(weapon, "firing")
		outputChatBox ("guns activated")
		end
	end	
end

function hydraStopping() -- ignore this function
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
	    if getElementModel(vehicle) == 520 then		
		setWeaponState(weapon, "ready")
		outputChatBox ("guns deactivated")
		end
	end	
end

function destroyHydraGun -- This function doesn't work
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		local attached = getElementAttachedTo (vehicle)
		if ( attached ) then
		destroyElement ( weapon )
	end	
end
addEventHandler("onClientPlayerVehicleExit", getLocalPlayer(), destroyHydraGun )

bindKey("mouse1", "down", hydraFiring)
bindKey("mouse1", "up", hydraStopping)
bindKey("lctrl", "down", hydraFiring)
bindKey("lctrl", "up", hydraStopping)

 

Edited by Noah_Antilles
Link to comment

I know you are new to scripting and you can always come to these forums for help, but before asking for help, you should try to solve it your own. There are number of ways you can debug your code, if you look at IIYAMA's tutorial here: 

Or the debug page on wiki here:

https://wiki.multitheftauto.com/wiki/Debugging

The reason why I'm saying this, is because the problem with your code is so obvious, you could have spotted yourself if you knew how to debug. Don't take this as on offence. Just to show you how easily you could have fixed it yourself (even just by looking at it):

function destroyHydraGun -- missing ( ) after function declaration
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if(vehicle)then
		if getElementModel(vehicle) == 520 then
			local attached = getElementAttachedTo (vehicle)
			if ( attached ) then
				destroyElement ( weapon )
			end
      	-- missing end
    -- missing end
end

When you run this code, you would have gotten all sorts of errors in your debugscript. 

  • Like 1
Link to comment

Thanks for replying, and believe me I am not taking this as an offence at all.

You see if I knew there was such a debugging tool I would´ve used it before, but I simply didn´t know it excisted.

I try to spend atleast a day trying to get a code to work, and after that day if I still haven´t managed I try to look for help here.

In the future I will use the debugging tool before posting here.

Thanks @pa3ck :)

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