Jump to content

[HELP]Police Marker


StateSway

Recommended Posts

Well, I made a script wich was working without gui when i made the gui i was needed to make some events and trigger them and i am begginer with this things

Server.lua

local theMarker1 = createMarker ( 1542.35608, -1682.45471, 12.55434, "cylinder", 2, 0, 0, 255, 150 ) 
  
function createPoliceTeam () 
    Policeteam = createTeam ("Police", 100, 149, 237) 
end 
addEventHandler ("onResourceStart", resourceRoot, createPoliceTeam) 
  
function joinTeam(thePlayer) 
if (getTeamName(getPlayerTeam(thePlayer)) == "Police" or getTeamName(getPlayerTeam(thePlayer)) == "Police") then 
            exports ["guimessages"] : outputServer (root, "Error: You are already working as a police" , 255, 0, 0) 
      else 
            exports ["guimessages"] : outputServer (root, "You are now working as a police" , 0, 0, 255) 
            setPlayerTeam(thePlayer,Policeteam) 
            setPlayerNametagColor ( thePlayer, 100, 149, 237 ) 
            setElementModel(thePlayer, 280) 
            giveWeapon (thePlayer, 3 ) 
      end 
end 
addEvent("Get.job", true) 
addEventHandler("Get.job", getRootElement(), joinTeam) 
  
funtion showgui() 
     triggerClientEvent ( "onShowgui", getRootElement() ) 
end 
addEventHandler( "onMarkerHit", theMarker1, showgui ) 

 

Client.lua

local player = getLocalPlayer () 
  
~~~~~~~~GUI~~~~~~~~~ 
Window = guiCreateWindow(544,186,456,422,"Police test",false) 
Getjob = guiCreateButton(34,283,145,92,"Get job",false,Window) 
Cancel = guiCreateButton(247,287,144,85,"Cancel",false,Window) 
Label = guiCreateLabel(69,46,321,206,"Test",false,Window) 
  
~~~~~~~~JobButton~~~~~~~ 
  
function getjob() 
     triggerServerEvent("Get.job", player) 
     guiSetVisible(Window, false) 
end 
addEventHandler ( "onClientGUIClick", Getjob, getjob, false ) 
  
~~~~~~~Show~~~~~~~~~~~ 
  
function showgui1() 
    guiSetVisible(Window, true) 
    showCursor(true) 
end 
addEvent ( "onShowgui", true ) 
addEventHandler ( "onShowgui", getRootElement(), showgui1 ) 

Well, I will be glad if someone Fixed me it, Thanks

Edited by Guest
Link to comment

1) Use the lua -tags when posting code here on forums.

2) '~~~~~~~' is not representing commenting, to make comments in your code, use '--' or '--[[ ]]'

3) You didn't write the 'function' correctly, you wrote it 'funtion'

4) You triggered the GUI for all players, not the one hitting the marker

5) You didn't check what kind of elements hit the marker, and whether it was in the corresponding dimension

6) getLocalPlayer() has a predefined name 'localPlayer', so no need to make any special ones for it

7) You checked whether the player was in the same team, twice

I made the code a little simpler, try this.

Server-side

local theMarker1 = createMarker(1542.35608, -1682.45471, 12.55434, "cylinder", 2, 0, 0, 255, 150) 
  
addEventHandler("onResourceStart", resourceRoot, 
    function() 
        Policeteam = createTeam("Police", 100, 149, 237) 
    end 
) 
  
addEvent("Get.job", true) 
addEventHandler("Get.job", root, 
    function() 
        if getTeamName(getPlayerTeam(source)) == "Police" then 
            exports["guimessages"]:outputServer(root, "Error: You are already working as a police", 255, 0, 0) 
        else 
            exports["guimessages"]:outputServer(root, "You are now working as a police", 0, 0, 255) 
            setPlayerTeam(source, Policeteam) 
            setPlayerNametagColor(source, 100, 149, 237) 
            setElementModel(source, 280) 
            giveWeapon(source, 3) 
        end 
    end 
) 
  
addEventHandler("onMarkerHit", theMarker1, 
    function(hitElement, matchingDimension) 
        if getElementType(hitElement) == "player" and matchingDimension then 
            triggerClientEvent(hitElement, "onShowgui", hitElement) 
        end 
    end 
) 

Client-side

function toggleWindow() 
    if not isElement(Window) then 
        Window = guiCreateWindow(544, 186, 456, 422, "Police test", false) 
        Getjob = guiCreateButton(34, 283, 145, 92, "Get job", false, Window) 
        Cancel = guiCreateButton(247, 287, 144, 85, "Cancel", false, Window) 
        Label = guiCreateLabel(69, 46, 321, 206, "Test", false, Window) 
        showCursor(true) 
        addEventHandler("onClientGUIClick", Getjob, function() 
            triggerServerEvent("Get.job", localPlayer) 
            destroyElement(Window) 
            showCursor(false) 
        end, false) 
    else 
        guiBringToFront(window) 
        showCursor(true) 
    end 
end 
addEvent("onShowgui", true) 
addEventHandler("onShowgui", root, toggleWindow) 

Edited by Guest
Link to comment

Well, the marker is showed and when i hit the marker the gui shows up but when i click on "get job" Button it only destroy the gui without making the following fuctions

addEvent("Get.job", true) 
addEventHandler("Get.job", root, 
    function(thePlayer) 
        if getTeamName(getPlayerTeam(thePlayer)) == "Police" then 
            exports["guimessages"]:outputServer(root, "Error: You are already working as a police", 255, 0, 0) 
        else 
            exports["guimessages"]:outputServer(root, "You are now working as a police", 0, 0, 255) 
            setPlayerTeam(thePlayer, Policeteam) 
            setPlayerNametagColor(thePlayer, 100, 149, 237) 
            setElementModel(thePlayer, 280) 
            giveWeapon(thePlayer, 3) 
        end 
    end 
) 

Thanks, for the help :)

Edit: Just a question is it good for my first script i have ever made ?

Link to comment

All didn't understand event system.

Client side:

Change this:
triggerServerEvent("Get.job", player) 

To:

triggerServerEvent("Get.job", localPlayer, localPlayer) 

You trigger localPlayer 2 times but you can do it once.

triggerServerEvent( 'Get.Job', localPlayer ) -- source is localPlayer 

Server side:

Well, the marker is showed and when i hit the marker the gui shows up but when i click on "get job" Button it only destroy the gui without making the following fuctions
addEvent("Get.job", true) 
addEventHandler("Get.job", root, 
    function(thePlayer) 
        if getTeamName(getPlayerTeam(thePlayer)) == "Police" then 
            exports["guimessages"]:outputServer(root, "Error: You are already working as a police", 255, 0, 0) 
        else 
            exports["guimessages"]:outputServer(root, "You are now working as a police", 0, 0, 255) 
            setPlayerTeam(thePlayer, Policeteam) 
            setPlayerNametagColor(thePlayer, 100, 149, 237) 
            setElementModel(thePlayer, 280) 
            giveWeapon(thePlayer, 3) 
        end 
    end 
) 

Thanks, for the help

Edit: Just a question is it good for my first script i have ever made ?

Argument 'thePlayer' doesn't needed because we can use source( source = player here ).

So it should be

addEvent( 'Get.job', true ) 
  
addEventHandler( 'Get.job', root, 
    function() 
        if getTeamName( getPlayerTeam( source ) ) == 'Police' then 
            exports["guimessages"]:outputServer( root, 'Error: You are already working as a police', 255, 0, 0 ) 
        else 
            exports["guimessages"]:outputServer( root, 'You are now working as a police', 0, 0, 255 ) 
            setPlayerTeam( source, Policeteam ) 
            setPlayerNametagColor( source, 100, 149, 237 ) 
            setElementModel( source, 280 ) 
            giveWeapon( source, 3 ) 
        end 
    end 
) 

https://wiki.multitheftauto.com/wiki/Event_system

viewtopic.php?f=91&t=39678

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