Jump to content

Need help


Recommended Posts

so i have made a script when a player clicks on a marker a gui shows up and if he clicks on get job he gets moved to a team changing his skin and giving him a weapon, everything worked fine, then i wanted to change entering the marker to clicking on a ped, but then it didn't work ( i will not post the whole script i will just post the functions and eventhandlers which i used )

Client

GUI: 
guiCreateWindow 
guiCreateMemo 
guiCreateButton 
guiCreateLabel 
  
Take job: 
isPedInVehicle 
getElementType 
guiSetVisible 
showCursor 
addEvent( "pedClick", true ) 
addEventHandler( "pedClick", root, guiwindow ) 
  
Cancel/takejob button: 
if ( source == GUIEditor.button[1] ) then 
triggerServerEvent 
guiSetVisible 
showCursor 
onClientGUIClick 

 

Server:

createTeam 
createPed 
  
On clicking the takejob button: 
getPlayerTeam 
getTeamName 
outputChatBox 
setPlayerTeam 
setPedArmor 
giveWeapon 
setElementModel 
addEvent 
addEventHandler 
  
on clicking the ped: 
triggerClientEvent 
onElementClicked 
  

Debugscript 3 doesn't show any errors

Link to comment
  • Moderators

I need the full code of this part:

Client

Take job: 
isPedInVehicle 
getElementType 
guiSetVisible 
showCursor 
addEvent( "pedClick", true ) 
addEventHandler( "pedClick", root, guiwindow ) 

And this one:

Server:

on clicking the ped: 
triggerClientEvent 
onElementClicked 

And please do it.

Link to comment
function guiwindow ( thePlayer ) 
        if isPedInVehicle(thePlayer)==true then return end 
        if (getElementType (thePlayer) == "player") then 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
end 
end 
addEvent( "pedClick", true ) 
addEventHandler( "pedClick", root, guiwindow ) 

function pedclick (thePlayer) 
 triggerClientEvent ( thePlayer, "pedClick") 
 end 
 addEventHandler( "onElementClicked", Policeped, pedclick ) 

Link to comment
  • Moderators

Read the wiki page of onElementClicked event (also the examples are here to help you to understand how it does work)

https://wiki.multitheftauto.com/wiki/OnElementClicked

function pedclick ( theButton, theState, thePlayer ) 
    local clickedElement = source --the wiki says that source is the element clicked 
    --if the element clicked is your ped and he clicked with the left mouse button and only when he released it 
    if hitElement == Policeped and  theButton == "left" and theState == "up" then  
        triggerClientEvent ( thePlayer, "pedClick") 
    end 
end 
addEventHandler( "onElementClicked", root, pedclick ) 

Link to comment
  • Moderators

Sorry, made a typo here:

if hitElement == Policeped and theButton == "left" and theState == "up" then 

it's clickedElement and not hitElement :oops:

Try again and be sure to tell us if /debugscript 3 is showing anything.

Note: use /debugscript 3 before starting the resource your code is in.

if not, then put some outputs where the code should go step by step.

if an output isn't working, then you know there is a problem from the if statement above or something similar.

This way, you can check your if statements, if triggers are working properly etc.

Link to comment
  • Moderators
if not, then put some outputs where the code should go step by step.

if an output isn't working, then you know there is a problem from the if statement above or something similar.

This way, you can check your if statements, if triggers are working properly etc.

Link to comment

Okay, i think i got it

function guiwindow ( thePlayer ) 
        if isPedInVehicle(thePlayer)==true then return end 
        if (getElementType (thePlayer) == "player") then 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
end 
end 
addEvent( "test", true ) 
addEventHandler( "test", root, guiwindow ) 

function pedclick ( theButton, theState, thePlayer ) 
    local clickedElement = source 
    if clickedElement == policeped and  theButton == "left" and theState == "up" then 
     triggerClientEvent ( thePlayer, "test") 
    outputChatBox("Test") 
    end 
end 
addEventHandler( "onElementClicked", root, pedclick ) 

still not working

debug script is not showing errors,

Link to comment
Okay, i think i got it
function guiwindow ( thePlayer ) 
        if isPedInVehicle(thePlayer)==true then return end 
        if (getElementType (thePlayer) == "player") then 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
end 
end 
addEvent( "test", true ) 
addEventHandler( "test", root, guiwindow ) 

function pedclick ( theButton, theState, thePlayer ) 
    local clickedElement = source 
    if clickedElement == policeped and  theButton == "left" and theState == "up" then 
     triggerClientEvent ( thePlayer, "test") 
    outputChatBox("Test") 
    end 
end 
addEventHandler( "onElementClicked", root, pedclick ) 

still not working

debug script is not showing errors,

function pedclick ( theButton, theState, thePlayer ) 
    local clickedElement = source 
    if clickedElement == policeped and  theButton == "left" and theState == "up" then 
     triggerClientEvent ( thePlayer, "test", thePlayer ) 
    outputChatBox("Test") 
    end 
end 
addEventHandler( "onElementClicked", root, pedclick ) 

function guiwindow (  ) 
        if isPedInVehicle (getLocalPlayer ()) then 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
end 
end 
addEvent( "test", true ) 
addEventHandler( "test", root, guiwindow ) 

Link to comment
Still not working it just puts "Test" in the chatbox when the resource starts,

Debuscript 3: Server.lua:24: Bad argument @ 'triggerClientEvent' [Expected element at argument 3, got none]

replace line 4 with

  
     triggerClientEvent ( thePlayer, "test" )  
  

if this doesn't work then try this.

  
    triggerClientEvent ( thePlayer, "test", getRootElement() ) 

Link to comment

I got the problem.

  
function pedclick ( theButton, theState, thePlayer ) 
  
    if theButton == "left" and theState == "up" then 
 if getElementType ( source ) == "marker" then 
  
     triggerClientEvent ( thePlayer, "test", getRootElement()) 
  
    outputChatBox("Test") 
  
    end 
     end 
end 
  
addEventHandler( "onElementClicked", root, pedclick ) 

Link to comment
  • Moderators
debug script is not showing errors,

You already said it yeah, but what about the output you did ? is is shown in the chatbox ?

And when I said to put some outputs I didn't mean only one, this is what you should write:

function pedclick ( theButton, theState, thePlayer ) 
    outputChatBox("1 - pedclick") 
    local clickedElement = source 
    if clickedElement == policeped and theButton == "left" and theState == "up" then 
        outputChatBox("2 - trigger") 
        triggerClientEvent ( thePlayer, "test") 
    end 
    outputChatBox("3 - end pedclick") 
end 
addEventHandler( "onElementClicked", root, pedclick ) 
  
function guiwindow ( thePlayer ) 
    outputChatBox("4 - guiwindow") 
    if isPedInVehicle(thePlayer) == true then return end 
    outputChatBox("5") 
    if (getElementType (thePlayer) == "player") then 
        outputChatBox("6") 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
    end 
    outputChatBox("7 - end guiWindow") 
end 
addEvent( "test", true ) 
addEventHandler( "test", root, guiwindow ) 

Are you sure that it's policeped and not Policeped ?? if yes, then it's okay.

You said that there were no error in debugscript ?!

The triggerClientEvent is wrong, here is what you should do:

triggerClientEvent ( thePlayer, "test", thePlayer) 

If you really didn't get any error then it means there is a problem here:

if clickedElement == policeped and  theButton == "left" and theState == "up" then 

Here is the client-side script to make guiwindow works with out triggerClientEvent:

function guiwindow () 
    outputChatBox("4 - guiwindow") 
    if isPedInVehicle( localPlayer ) == true then return end 
    outputChatBox("5") 
    -- if (getElementType ( source ) == "player") then --not needed anymore 
        outputChatBox("6") 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
    -- end 
    outputChatBox("7 - end guiWindow") 
end 
addEvent( "test", true ) 
addEventHandler( "test", root, guiwindow ) 

Hope that this time, it will work :S

Link to comment
  • Moderators
I got the problem.
... 
     triggerClientEvent ( thePlayer, "test", getRootElement()) 
... 

Nope and this is worst because you are using the root element which is a bad practice:

Note: To save client CPU, you should avoid setting sourceElement to the root element where possible. Using resourceRoot is usually sufficient if the event is handled by the same resource on the client.

And the problem was in the client side (using thePlayer instead of localPlayer or source because thePlayer isn't sent as 1st parameter (4th argument of triggerClientEvent

Link to comment
Saml1er - your code didn't work

Citizen - idk what really happened with your code, when i started the resource i clicked on the ped found the chatbox msgs appeared, but still the gui didn't work :(

Does it output anything like numbers? That code looks pretty fine to me. >.> ( talking about citizen code )

oh well I thought you meant clicking the marker( now talking about mine). :D

EDIT: you're using the old one, use this one as citize just told you now.

  
function guiwindow () 
  
    outputChatBox("4 - guiwindow") 
  
    if isPedInVehicle( localPlayer ) == true then return end 
  
    outputChatBox("5") 
  
    -- if (getElementType ( source ) == "player") then --not needed anymore 
  
        outputChatBox("6") 
  
        guiSetVisible(GUIEditor.window[1], true) 
  
        showCursor ( true ) 
  
    -- end 
  
    outputChatBox("7 - end guiWindow") 
  
end 
  
addEvent( "test", true ) 
  
addEventHandler( "test", root, guiwindow ) 

Link to comment
  • Moderators
Saml1er - your code didn't work

Citizen - idk what really happened with your code, when i started the resource i clicked on the ped found the chatbox msgs appeared, but still the gui didn't work :(

EDIT: Bad argument at 'getElementType'

I commented two lines, they weren't supposed to be restored by you ... Please read instead of randomly copying, so again:

triggerClientEvent ( thePlayer, "test", thePlayer) 

and

function guiwindow () 
    outputChatBox("4 - guiwindow") 
    if isPedInVehicle( localPlayer ) == true then return end 
    outputChatBox("5") 
    -- if (getElementType ( source ) == "player") then --not needed anymore 
        outputChatBox("6") 
        guiSetVisible(GUIEditor.window[1], true) 
        showCursor ( true ) 
    -- end 
    outputChatBox("7 - end guiWindow") 
end 
addEvent( "test", true ) 
addEventHandler( "test", root, guiwindow ) 

And once again, do not modify the comments !

Link to comment
  • Moderators
Citizen i didn't ignore what you said i tried both ways, but idk it didn't work when i tried before

now your code works, Really thanks

I have to apologies then.

By the way you had to end with this code if you had read carefully (but I'm agree there were a lot of suggestions that were only fixing 1 thing at a time).

So now you can remove the useless outputs (but I'm sure you already did it, which is good). But don't forget that outputs are really usefull to check where the execution process is going in the script and where it doesn't.

Keep it up :)

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