Jump to content

when I click a button, I join a team


swag_k_dog

Recommended Posts

Hi, On this script there are 2 teams created and 2 buttons..

I want when you click the bCrim button to join the crimz team and close the gui. help?

crimz = createTeam("Criminal", 255, 0, 0)
enfz = createTeam("Enforcer", 0, 0, 255)





addEventHandler("onClientResourceStart", resourceRoot,
    function()
local screenW, screenH = guiGetScreenSize()
        sScreen = guiCreateStaticImage((screenW - 1366) / 2, (screenH - 768) / 2, 1366, 768, "images/scrn.png", false)

        bCrim = guiCreateButton(376, 369, 162, 63, "", false, sScreen)
        guiSetAlpha(bCrim, 0.00)
        bEnf = guiCreateButton(743, 369, 162, 63, "", false, sScreen)
        guiSetAlpha(bEnf, 0.00)    
    end
)

thanks 

Edited by swag_k_dog
Link to comment

1. Create team server side

2. Handle the GUI button click client side and triggerServerEvent with a variable that indicates which team has been clicked

3. Handle the server event, set the team of the player according the value you passed in step 2.

 

If you don't know how to trigger a server side event, look at the wiki page and try to do it on your own, if you can't manage to get it working, come back with your code and the problem you are facing.

Link to comment
9 minutes ago, pa3ck said:

1. Create team server side

2. Handle the GUI button click client side and triggerServerEvent with a variable that indicates which team has been clicked

3. Handle the server event, set the team of the player according the value you passed in step 2.

 

If you don't know how to trigger a server side event, look at the wiki page and try to do it on your own, if you can't manage to get it working, come back with your code and the problem you are facing.

I started scripting few days ago and I dont really understand everything you're saying but

here is the "client" script

addEventHandler("onClientResourceStart", resourceRoot,
    function()
local screenW, screenH = guiGetScreenSize()
        sScreen = guiCreateStaticImage((screenW - 1366) / 2, (screenH - 768) / 2, 1366, 768, "images/scrn.png", false)

        bCrim = guiCreateButton(376, 369, 162, 63, "", false, sScreen)
        guiSetAlpha(bCrim, 0.00)
        bEnf = guiCreateButton(743, 369, 162, 63, "", false, sScreen)
        guiSetAlpha(bEnf, 0.00)    
    end
)


function showC()
showCursor( true )
end


addEventHandler("onClientResourceStart", getRootElement(), showC)

addEventHandler("onClientGUIClick", bCrim, teamset )

 

and  "server" script

function teemz ()
crimz = createTeam ( "Criminal", 255, 0, 0 )
enfz = createTeam ( "Enforcer", 0, 0, 255 )
end
addEventHandler("onResourceStart", resourceRoot, teemz)

can you like write a code on how to do it? would be cool, thanks

Link to comment

We usually don't just write the code for you without you making an effort, but because you said you just started, I wrote the code for you with heavy documentation. Hopefully you will get the gist of it

 

-- Server Side

local crimz = createTeam("Criminal", 255, 0, 0)
local enfz = createTeam("Enforcer", 0, 0, 255)

addEvent("onClientTeamPicked", true) -- its essential to enable the event so we can actually use it in this script file
-- notice the name is same as we had client side
-- the client is enabled, now lets assign a function to it
addEventHandler("onClientTeamPicked", root, function(name) -- the variable "name" is coming from the client	
       
    local team = getTeamFromName(name) -- this is where our variable comes in to play
	-- its gonna be either "Criminal" or "Enforcer" so we can find the team by its name
    -- we can be 100% that we found the team as we have hard coded names both end

	setPlayerTeam(client, team) 
	-- client is a "predefined" variable that comes with the the event and it is the player element that triggered from client side
	-- we have the team element, we have the client, so we can enroll them to the team
end)


-- Client Side

local sScreen, bCrim, bEnf
local screenW, screenH = guiGetScreenSize()

addEventHandler("onClientResourceStart", resourceRoot,
    function() -- basic stuff, I guess you already know this, as this is your code
        showCursor(true)
		sScreen = guiCreateStaticImage((screenW - 1366) / 2, (screenH - 768) / 2, 1366, 768, "images/scrn.png", false)
        bCrim = guiCreateButton(376, 369, 162, 63, "", false, sScreen)
		guiSetAlpha(bCrim, 0.00)
        bEnf = guiCreateButton(743, 369, 162, 63, "", false, sScreen)
		guiSetAlpha(bEnf, 0.00)
        
        function closePanel()
            destroyElement(sScreen) -- we can just destroy the image as the buttons are children
            removeEventHandler("onClientGUIClick", resourceRoot, guiClicked) -- remove the event listener for GUI clicks
            showCursor(false)
        end
        
        function guiClicked(btn, state)
            if btn == "left" and state == "up" then -- left mouse button, when user released it
                if source == bCrim then
                    triggerServerEvent("onClientTeamPicked", localPlayer, "Criminal") -- pass "Criminal" with the trigger as string
					-- triggerServerEvent "triggerName", playerWeWantToSendItTo, variable
					closePanel() -- destroy the panel and remove event listener
                elseif source == bEnf then
                    triggerServerEvent("onClientTeamPicked", localPlayer, "Enforcer") -- pass "Enforcer" with the trigger as string
					closePanel()					
                end
            end
        end
        addEventHandler("onClientGUIClick", resourceRoot, guiClicked) -- add the GUI button click listener
        
    end
)

 

 

This code tag is a bit messy, it's fine in Notepad++, but comes out with bad indent here... 

Edited by pa3ck
  • Like 2
Link to comment
1 minute ago, pa3ck said:

We usually don't just write the code for you without you making an effort, but because you said you just started, I wrote the code for you with heavy documentation. Hopefully you will get the gist of it

 


-- Server Side

local crimz = createTeam("Criminal", 255, 0, 0)
local enfz = createTeam("Enforcer", 0, 0, 255)

addEvent("onClientTeamPicked", true) -- its essential to enable the event so we can actually use it in this script file
-- notice the name is same as we had client side
-- the client is enabled, now lets assign a function to it
addEventHandler("onClientTeamPicked", root, function(name) -- the variable "name" is coming from the client	
       
    local team = getTeamFromName(name) -- this is where our variable comes in to play
	-- its gonna be either "Criminal" or "Enforcer" so we can find the team by its name
    -- we can be 100% that we found the team as we have hard coded names both end

	setPlayerTeam(client, team) 
	-- client is a "predefined" variable that comes with the the event and it is the player element that triggered from client side
	-- we have the team element, we have the client, so we can enroll them to the team
end)


-- Client Side

local sScreen, bCrim, bEnf
local screenW, screenH = guiGetScreenSize()

addEventHandler("onClientResourceStart", resourceRoot,
    function() -- basic stuff, I guess you already know this, as this is your code
        showCursor(true)
		sScreen = guiCreateStaticImage((screenW - 1366) / 2, (screenH - 768) / 2, 1366, 768, "images/scrn.png", false)
        bCrim = guiCreateButton(376, 369, 162, 63, "", false, sScreen)
		guiSetAlpha(bCrim, 0.00)
        bEnf = guiCreateButton(743, 369, 162, 63, "", false, sScreen)
		guiSetAlpha(bEnf, 0.00)
        
        function closePanel()
            destroyElement(sScreen) -- we can just destroy the image as the buttons are children
            removeEventHandler("onClientGUIClick", resourceRoot, guiClicked) -- remove the event listener for GUI clicks
            showCursor(false)
        end
        
        function guiClicked(btn, state)
            if btn == "left" and state == "up" then -- left mouse button, when user released it
                if source == bCrim then
                    triggerServerEvent("onClientTeamPicked", localPlayer, "Criminal") -- pass "Criminal" with the trigger as string
					-- triggerServerEvent "triggerName", playerWeWantToSendItTo, variable
					closePanel() -- destroy the panel and remove event listener
                elseif source == bEnf then
                    triggerServerEvent("onClientTeamPicked", localPlayer, "Enforcer") -- pass "Enforcer" with the trigger as string
					closePanel()					
                end
            end
        end
        addEventHandler("onClientGUIClick", resourceRoot, guiClicked) -- add the GUI button click listener
        
    end
)

 

thanks, ill guide myself and learn.. wish I had a real mta scripting teacher xD

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