Recommended Posts

Hello there,

I am a total noob in scripting, but I thought I'd give it a try for the sake of learning new things.

What I am trying to accomplish with this script is disabling the "standard" rustler guns (this works thanks to an example script of the wiki.)

and make it fire "custom" projectiles when the player presses the left mouse button. 

 

For now I just want to check if the script recognizes a player inside a rustler pressing left mouse button (hence the outputChatBox)

However, when I am inside a rustler and use the left mouse button to fire nothing pops up in the chatbox (luckily, as intended, the rustler doesn't fire its guns)

What am I doing wrong? I'm sure I made some kind of noob mistake but hey, I am still learning ;)

function disableFireForRustler ( theVehicle, seat )
    if ( getElementModel ( theVehicle ) == 476 ) then 
        toggleControl ( "vehicle_secondary_fire", false ) 
    else 
        toggleControl ( "vehicle_secondary_fire", true ) 
    end

addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), disableFireForRustler )

function addTracer (mouse1, press)
     if (press) then
         outputChatBox ("guns fired")
    end

addEventHandler("onClientKey", root, addTracer)    

 

Edited by Noah_Antilles

Share this post


Link to post

u forget end at line 7 and  line14

function disableFireForRustler ( theVehicle, seat )
    if ( getElementModel ( theVehicle ) == 476 ) then 
        toggleControl ( "vehicle_secondary_fire", false ) 
    else 
        toggleControl ( "vehicle_secondary_fire", true ) 
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), disableFireForRustler )

function addTracer (mouse1, press)
     if (press) then
         outputChatBox ("guns fired")
    end
end
addEventHandler("onClientKey", root, addTracer)    

try now

Edited by Reval
  • Like 1

Share this post


Link to post

Replace addTracer with this :

function addTracer( BUTTON, STATE )
  if( BUTTON == "left" and STATE == "up" ) then
    outputChatBox( "guns fired" )
  end 
end 
addEventHandler( "onClientClick", getRootElement(), addTracer ) 

 

  • Like 1

Share this post


Link to post

Thanks for your replies.

I tried both of them

@Reval

This doesn't seem to work, the Rustler doesn't fire, but I don't get an output in the chat.

 

@*BeaT*

I only get an output in the chat when I am left-clicking in menu's (when using the F1 menu for example)

When seated in a rustler nothing happens

 

The two scripts need to work together, but it seems they don't.

Edited by Noah_Antilles

Share this post


Link to post

try this

function disableFireForRustler ( theVehicle, seat )
    if ( getElementModel ( theVehicle ) == 476 ) then 
        toggleControl ( "vehicle_secondary_fire", false ) 
    else 
        toggleControl ( "vehicle_secondary_fire", true ) 
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), disableFireForRustler )

function MouseKey(button, press)
    if (button == "mouse1" and press) then
        outputChatBox("Clicked the left mouse !")
    end
end
addEventHandler("onClientKey", root, MouseKey)

 

Share this post


Link to post

@coNolel

The script outputs the message whenever I click left mouse button. It exclusively needs to output the text when I am in a Rustler.

As far as I can see the second part of the script (line 10-15) ignores the first part of the script (line 1-8)

Edited by Noah_Antilles

Share this post


Link to post
1 minute ago, Noah_Antilles said:

@coNolel

The script outputs the message whenever I click left mouse button. It exclusively needs to output the text when I am in a Rustler.

As far as I can see the second part of the script (line 10-15) ignores the first part of the script (line 1-8)

so , whenever you enter the rustler, you add the Event of the mouse button , same thing when you exit, you remove it .

Share this post


Link to post

I am afraid I don't understand, I have little experience in scripting :/ 

This is the result I am striving for:

  • When I'm in a rustler and left click the text should pop up
  • When I'm in another vehicle or on foot and left click it shouldn't pop up.

Your script helped me a lot in the right direction, but whenever I left click the text pops up regardless of whether I'm in a rustler or not.

Share this post


Link to post
3 hours ago, Noah_Antilles said:

I am afraid I don't understand, I have little experience in scripting :/ 

This is the result I am striving for:

  • When I'm in a rustler and left click the text should pop up
  • When I'm in another vehicle or on foot and left click it shouldn't pop up.

Your script helped me a lot in the right direction, but whenever I left click the text pops up regardless of whether I'm in a rustler or not.

hey again , sorry about the " late " answer , but not everything you wish comes real and quick , you should make codes and get experience trough out writing codes and the important thing in to know what are you writing, not taking from scratches , as i used to do tbh , so there is nothing to be afraid from , you are now making your experience better and better , so let's don't talk too much , as i said above , you can write a function checking from it the vehicle that you entered , and if it's the Hustler as you want , you add the Event Handler , else you remove the event handler , that's pretty much it !

  • Like 1

Share this post


Link to post

I indeed have to learn it myself. I will try to fix the remainder of the problem myself :) thanks for helping me @coNolel @*BeaT* and @Reval

You've given me lots of motivation to move on and learn more, thanks!

Edited by Noah_Antilles
  • Like 1

Share this post


Link to post
3 minutes ago, Noah_Antilles said:

I indeed have to learn it myself. I will try to fix the remainder of the problem myself :) thanks for helping me @coNolel @*BeaT* and @Reval

You've given me lots of motivation to move on and learn more, thanks!

No problem dude, catch you in the next one ! :D 

Share this post


Link to post

Alright, I've tried a load of things, and I am beginning to get a grasp of scripting (atleast I think so ;) ) but I just cannot seem to get the scripts to work with eachother.

This is what I have right now:

  • The disable rustler firing script is in another script - client
  • This scipt - client 
function checkIfRustler ( theVehicle, seat )
     local id = getElementModel ( theVehicle )
     if id == 476 then
	 end
end
addEventHandler ("onClientPlayerVehicleEnter", getLocalPlayer(), checkIfRustler)

function MouseKey(button, press)
    if (button == "mouse1" and press) then
        outputChatBox("Clicked the left mouse !")
    end
end
addEventHandler("onClientKey", checkIfRustler, MouseKey)

Could you give me some more tips as to what I am doing wrong? because I feel like I am running against a wall right now.

Share this post


Link to post

Okey , so

function MouseKey(button, press)
    if (button == "mouse1" and press) then
        outputChatBox("Clicked the left mouse !")
    end
end

function checkIfRustler ( theVehicle, seat )
     local id = getElementModel ( theVehicle )
     if id == 476 then
    	-- then what ? you shout add the Event Handler
   		 addEventHandler("onClientKey", checkIfRustler, MouseKey)
    else 
    outputChatBox("you can't use it ! ( only in Rustler vehicle ).")
	 end
end
addEventHandler ("onClientPlayerVehicleEnter", getLocalPlayer(), checkIfRustler)

Try this 

  • Like 1

Share this post


Link to post

Thanks for your reply again @coNolel

The script doesn't seem to work. I get no chat message.

I have no idea what's wrong, your script seems perfect to me :/

edit: when I enter another vehicle that is not a rustler I get the chat message "you can't use it ! (only in Rustler vehicle)" even though I did not press left mouse button

 

Edited by Noah_Antilles

Share this post


Link to post

Try this:


addEventHandler( "onClientKey", root, 
	function ( button )
		if button == "mouse1" and getPedOccupiedVehicle(localPlayer) then
			if getElementModel( getPedOccupiedVehicle(localPlayer) ) == 476 and isControlEnabled( "vehicle_secondary_fire" ) then
				toggleControl ( "vehicle_secondary_fire", false )
			else
				if not isControlEnabled( "vehicle_secondary_fire" ) then
					toggleControl ( "vehicle_secondary_fire", true )
				end
			end
		end
	end
)

 

If it doesn't work, try using cancelEvent.

Edited by DNL291
  • Like 2

Share this post


Link to post

@DNL291 

Thanks for the reply, I tested the script and it almost works.

I added an outputChatBox function to see whether or not it worked here's what I found:

  • When I press left mouse button in the rustler it posts both outputchatbox texts in the chat. 

Here's the small addition I added

addEventHandler( "onClientKey", root, 
	function ( button )
		if button == "mouse1" and getPedOccupiedVehicle(localPlayer) then
			if getElementModel( getPedOccupiedVehicle(localPlayer) ) == 476 and isControlEnabled( "vehicle_secondary_fire" ) then
				toggleControl ( "vehicle_secondary_fire", false )
				outputChatBox ("Guns activated")
			else
				if not isControlEnabled( "vehicle_secondary_fire" ) then
					toggleControl ( "vehicle_secondary_fire", true )
					outputChatBox ("No rustler guns")
				end
			end
		end
	end
)

I added these outputChatBox functions because later on I want to replace them with createProjectile so I have "custom" guns firing from the rustler.

  • Like 2

Share this post


Link to post

 

Try using the second parameter: pressOrRelease. So the updated code should look like this:

addEventHandler( "onClientKey", root, 
	function ( button, press )
		if button == "mouse1" and press and getPedOccupiedVehicle(localPlayer) then
			if getElementModel( getPedOccupiedVehicle(localPlayer) ) == 476 and isControlEnabled( "vehicle_secondary_fire" ) then
				toggleControl ( "vehicle_secondary_fire", false )
				outputChatBox ("Guns activated")
			else
				if not isControlEnabled( "vehicle_secondary_fire" ) then
					toggleControl ( "vehicle_secondary_fire", true )
					outputChatBox ("No rustler guns")
				end
			end
		end
	end
)

 

  • Like 1

Share this post


Link to post
function shootProjectile()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    -- Check if the player is in a vehicle
    if(vehicle)then
        --Check if its a rustler
        if getElementModel(vehicle) == 476 then
            --Get the current position of the vehicle and fire a rocket (No rotation is passed to createProjectile function because it automaticaly gets the vehicle rotation)
            local x, y, z = getElementPosition(vehicle)
            createProjectile(vehicle, 19, x, y, z)
        end
    end
end

bindKey("mouse1", "down", shootProjectile)

I think I understand you. Give a try to this.

  • Like 1

Share this post


Link to post

Tested both scripts, thanks again for your replies

@DNL291

  • When I am in a rustler and press the left mouse it works perfectly. however, when I press it for the second time the guns fire and the chatbox launches the "No rustler guns message"

@Elmatus

  • Your script works perfectly, it spawns a rocket when I press left mouse and am inside of a rustler. However, the normal rustler guns still fired, so I added the disableFireForRustler script. It now works perfectly fine!

For anyone interested in the script, here it is:

function disableFireForRustler ( theVehicle, seat )
    if ( getElementModel ( theVehicle ) == 476 ) then 
        toggleControl ( "vehicle_secondary_fire", false ) 
    else 
        toggleControl ( "vehicle_secondary_fire", true ) 
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), disableFireForRustler )

function shootProjectile()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    -- Check if the player is in a vehicle
    if(vehicle)then
        --Check if its a rustler
        if getElementModel(vehicle) == 476 then
            --Get the current position of the vehicle and fire a rocket (No rotation is passed to createProjectile function because it automaticaly gets the vehicle rotation)
            local x, y, z = getElementPosition(vehicle)
            createProjectile(vehicle, 19, x, y, z)
        end
    end
end

bindKey("mouse1", "down", shootProjectile)

 

I want to thank all of you for helping out a beginner in scripting. I feel more confident knowing there is a big community that is willing to help me when I am struggling.

Big thanks to everyone who helped me :) 

 

-Noah

  • Like 1

Share this post


Link to post

Yeah, you said you already had it so I didn't post it! I'm glad its working now.

  • Like 1

Share this post


Link to post

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.