Jump to content

Problems with client events.


aralmo

Recommended Posts

Im working on a login / register screen for my first gamemod ever in MTA, but im having some weird problems with it. Perhaps some experienced user can help me :P

I have two problems.

The first is that when i connect to my localhost server for first time the client script don't work, i must disconnect and connect again and then it loads ok.

I think is some kind of bug with the download manager?.

The second one is a problem calling the client event that shows the login screen, it only works the first time i call it.

Thats my code:

Server side:

  
function playerJoinHandler() 
     outputChatBox("Player connecting...",source)                                       
     triggerClientEvent("ShowLoginGUIEvent",source) --trigger the client event of the player 
end                             
addEventHandler("onPlayerJoin", getRootElement(), playerJoinHandler) 
  
function SubmitLoginHandler(user,pass) 
    outputChatBox("slh",source) 
    local acc = getAccount(user) 
    if (not acc) then 
        outputChatBox("This name is not registered.",source) 
        triggerClientEvent("ShowLoginGUIEvent",source) --trigger the client event of the player 
    else                                                      
        if (logIn(source,acc,pass))  then        
            outputChatBox("Login correct.",source) 
        else 
            outputChatBox("Login incorrect.",source) 
            triggerClientEvent("ShowLoginGUIEvent",source) --trigger the client event of the player  
        end 
    end  
end 
addEvent("SubmitLoginEvent",true)     
addEventHandler("SubmitLoginEvent",getRootElement(),SubmitLoginHandler); 
  

Client side:

  
function guiShowLogin() 
   outputChatBox("Login.", getLocalPlayer())     
   CreateLoginWindow()  
   addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
    if (wdwLogin ~= nil) then 
             guiSetVisible(wdwLogin, true) 
    end 
    showCursor(true) 
    guiSetInputEnabled(true) 
end    
addEventHandler("ShowLoginGUIEvent", getLocalPlayer(),guiShowLogin,false) 
  

Link to comment

50p Thanks!, now it works perfect.

And you trigger it for all clients.. Although using the getlocalplayer in event handler is good too, it's better imo to trigger it only for the player who joined. Check the first argument on triggerclientevent from wiki.

Do you mean the SubmitLoginEvent in server side? im triggering it without propagation on the root element is that right? or should i create a new eventhandler when a player connects and attach the event to it?

By the way im very impressed with the scripting in MTA much better than SA-MP's PAWNO imho. I'm definitely switching to it :mrgreen:

Link to comment
The second one is a problem calling the client event that shows the login screen, it only works the first time i call it.

Thats my code:

Server side:

  
function playerJoinHandler() 
     outputChatBox("Player connecting...",source)                                       
     triggerClientEvent("ShowLoginGUIEvent",source) --trigger the client event of the player 
end                             
addEventHandler("onPlayerJoin", getRootElement(), playerJoinHandler) 
  
function SubmitLoginHandler(user,pass) 
    outputChatBox("slh",source) 
    local acc = getAccount(user) 
    if (not acc) then 
        outputChatBox("This name is not registered.",source) 
        triggerClientEvent("ShowLoginGUIEvent",source) --trigger the client event of the player 
    else                                                      
        if (logIn(source,acc,pass))  then        
            outputChatBox("Login correct.",source) 
        else 
            outputChatBox("Login incorrect.",source) 
            triggerClientEvent("ShowLoginGUIEvent",source) --trigger the client event of the player  
        end 
    end  
end 
addEvent("SubmitLoginEvent",true)     
addEventHandler("SubmitLoginEvent",getRootElement(),SubmitLoginHandler); 
  

Client side:

  
function guiShowLogin() 
   outputChatBox("Login.", getLocalPlayer())     
   CreateLoginWindow()  
   addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
    if (wdwLogin ~= nil) then 
             guiSetVisible(wdwLogin, true) 
    end 
    showCursor(true) 
    guiSetInputEnabled(true) 
end    
addEventHandler("ShowLoginGUIEvent", getLocalPlayer(),guiShowLogin,false) 
  

You're having problems with it. Ok let's see...

1. Adding addEvent in client-side might help? :P

2. triggerClientEvent("ShowLoginGUIEvent",source) Should be: triggerClientEvent(source,"ShowLoginGUIEvent",source)

3. Source isn't specified in function SubmitLoginHandler. Are you using it while doing triggerServerEvent?

Link to comment

Gamesnert: Yeah thanks, im using it without target because the event was attached to the player element. But that its not working at all.

Right now i got a login/registration page, a working character sheet and a skill upgrade window. Everything works perfect if you don't change the nickname before login.

So my next question is, there's anyway i can attach a client event to the client element, not the player? because the player element changes when you change your nick.

Right now i have something like this

Client code:

  
addEvent("MyClientEvent",true) 
addEventHandler("MyClientEvent",getLocalPlayer(),myEventHandler) 
  

Server code:

  
function clientLogin(old,new) 
   triggerClientEventHandler(source,"MyClientEvent",source,false) 
end 
  

It works if old=new in clientLogin else it dosn't ejecutes the handler on client-side.

Thats making me crazy, did all the stuff in about 5 hours but those small things took-me like 2 days until now.

[edit] oh! and another noob question :P is lua object-oriented? can you create a class with inheritance on it?

Thanks.

Link to comment

Are you sure that player element changes on nick change? I don't think so.

And it's triggerClientEvent() not triggerClientEventHandler()

client

  
addEvent("myEvent", true) 
addEventHandler("myEvent", getRootElement(), handlerFunction) 
  

server

  
triggerClientEvent(thePlayer, "myEvent", getRootElement(), variable) 
  

I think it should work that way. Haven't checked my resources or wiki atm :P

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