Jump to content

Need some help with Client-Server interaction


AlexMiller

Recommended Posts

Hi there, that's my first post on the forum but i'm not new here.

I'm having a problem with a starting GUI that let you choose joining a faction between Groove and police. The GUI is visible but the join buttons does nothing. Here's the client-side script:

function creafinestra()
showCursor(true)
GUIEditor_Button = {}
 
skinwdw = guiCreateWindow(327,162,370,210,"CHOOSE A FACTION TO JOIN",false)
guiWindowSetMovable(skinwdw,false)
guiWindowSetSizable(skinwdw,false)
copskin2 = guiCreateStaticImage(0.0243,0.0952,0.2568,0.8619,"images/copskin.png",true,skinwdw)
robskin2 = guiCreateStaticImage(0.7108,0.1143,0.2649,0.8429,"images/robskin.png",true,skinwdw)
copskin = guiCreateButton(0.2811,0.381,0.2054,0.2333,"POLICE",true,skinwdw)
robskin = guiCreateButton(0.5054,0.381,0.2027,0.2333,"ROBBER",true,skinwdw)
 
end
addEventHandler("onClientResourceStart", getResourceRootElement( getThisResource( ) ), creafinestra)
 
function spawnacops(button)
if button == "left" then
triggerServerEvent("Cops", player)
guiSetInputEnabled(false)
guiSetVisible(skinwdw, false)
showCursor(false)
end
end
addEventHandler("onClientGUIClick", copskin, spawnacops)
 
 
function spawnarobs(button)
if button == "left" then
triggerServerEvent("Robs", player)
guiSetInputEnabled(false)
guiSetVisible(skinwdw, false)
showCursor(false)
end
end
 
addEventHandler("onClientGUIClick", robskin, spawnarobs)

and that's the server-side script:

addEvent( "Cops", true )
function entercops ( player, command, teamName )
local myTeam =  getPlayerTeam ( player )
if ( myTeam ) then
outputChatBox ( "You are already member of a team!", player )
else 
setPlayerTeam ( player, teamCops )
outputChatBox ( "You are now a Cops member!", player )
killPed( player )
end
end
 
addEventHandler ( "Cops", getRootElement(), entercops )
addCommandHandler ( "joincops", entercops, createTeamsOnStart )
 
 
addEvent( "Robs", true )
function enterrobs ( player, command, teamName )
local myTeam =  getPlayerTeam ( player )
if ( myTeam ) then
outputChatBox ( "You are already member of a team!", player )
else
setPlayerTeam ( player, teamRobbers )
outputChatBox ( "You are now a Robs member!", player )
killPed( player )
end
end
 
addEventHandler ( "Robs", getRootElement(), enterrobs )
addCommandHandler ( "joinrobs", enterrobs, createTeamsOnStart )

Thanks for help!

Link to comment

Use the debug window to let you know whether there is something wrong. I'm sure you'll get "bad argument" warnings.

/debugscript 3 (it's the command to show the debug window which will show you all warnings/errors/info messages)

Link to comment

Thanks for answering, i used the debugger and there were a badargument on addeventhandler in the client file. The incriminated lines are

addEventHandler("onClientGUIClick", copskin, spawnacops)

and

addEventHandler("onClientGUIClick", robskin, spawnarobs)

Any suggestion?

Link to comment

It's very simple, just please try to read this step by step, don't go 1,6,7,2... Just because code executed from top to the bottom doesn't mean it goes straight to the chunks/blocks/functions, so follow the steps 1,2,3,... just like Lua engine reads it.

  Quote
-- 1st, this function is defined but is not called, why should it be called now?
function resourceStarted( )
-- 6th, finally onClientResourceStart is triggered and calls this function
-- which makes a button! Now you should addEventHandler with this button because it's created
-- and ready to be used
   button = guiCreateButton( 10, 200, 100, 20, "1337 button", false )
-- 7th, so if "button" variable exists now, why don't we use it?
addEventHandler( "onClientGUIClick", button, buttonClicked, false )
end
-- 2nd, you call this addEventHandler function which attaches a function to an event
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource() ), resourceStarted )
 
-- 3rd, this function is defined but is not called, why should it be called now?
function buttonClicked( )
outputChatBox( "Wow! You clicked a button! Do it again!" );
end
-- 4th, you call addEventHandler function which attaches a function to an event
-- but what is "button"? it wasn't created yet! keep going (5th step)  -->
addEventHandler( "onClientGUIClick", button, buttonClicked, false )
-- 5th, there is nothing here, right? so, onClientResourceStart is triggered, lets do it now! (go to 6th step)

Link to comment

Ok, thanks! i did it by following you tut, but now there's another problem:

I get this error: Bad Argument @ SetPlayerTeam line... and Bad argument @ killPed

I added another event for assigning a team to player

function cop ( player, teamName )
local theTeam = "Cops"
 setPlayerTeam ( player, theTeam )
 outputChatBox ( "You are now a Cops member!", player )
killPed( player )
end
 addEventHandler ( "Cops", getRootElement(), cop )
 
 
 addEvent( "Robs", true )
 
function rob ( player, teamName )
local theTeam = "Robbers"
local thePlayer = getLocalPlayer
 setPlayerTeam ( player, theTeam )
 outputChatBox ( "You are now a Robs member!", player )
killPed( player )
end
 addEventHandler ( "Robs", getRootElement(), rob )

I'll login this evening surely, so write any suggestion you have, please :D

Note: That happens when i press the button. And this is the server side script

Link to comment

You can use the same function for your commands and triggerServerEvent.

Add this code in your function that is called when you press a button of the teams:

local me = getLocalPlayer();
-- when you want to join Cops (cobs button):
triggerServerEvent( "ClientWantsToJoinTeam", me, me, "", "Cops" );
-- or when you want to join Robs (robs button):
triggerServerEvent( "ClientWantsToJoinTeam", me, me, "", "Robs" );

This is server-side code for joining teams (1 function for 1 event and 1 commands):

function joinTeam( plr, cmd, teamName ) -- these are parameters passed from commands
local team = getTeamFromName( teamName ); -- get the team element needed for setPlayerTeam
if ( team ) then
setPlayerTeam( plr, team );
outputChatBox( "You are now '".. teamName .."' member!", plr );
killPed( plr );
else
outputChatBox( "Team '".. teamName .. "' does not exists.", plr );
end
end
addEvent( "ClientWantsToJoinTeam", true );
addEventHandler( "ClientWantsToJoinTeam", getRootElement( ), joinTeam );
addCommandHandler( "jointeam", joinTeam );

With this code you can use a command to join a team by name like:

  Quote
/jointeam Robs

/jointeam Cops

Or, you can trigger server event with the team name as the 5th argument because 5th argument will be teamName in the joinTeam function.

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