Jump to content

spwanPlayer Problems


matucha123

Recommended Posts

I'm starting scripter in lua. While scripting I run into problem with SpwanPlayer. When I login(or register) get error

playerDataSaving_loading.lua:13: Bad argument @ 'spwanPlayer'

playerDataSaving_loading(serverside):

addEvent("playerDataLoad", true) 
  
function playerDataLoading(thePlayer, username) 
    local result = mysql_query(mysqldb, "Select * From players where username='"..username.."'") 
    if not result then 
        outputDebugString( "EVENT:playerDataLoading mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
        mysql_free_result(result) 
        return 
    end 
    local field = mysql_fetch_field(result) 
    setElementData(thePlayer, "playerUsername", username) 
    --spawnPlayer(thePlayer, field["pos_X"], field["pos_Y"], field["pos_Z"]) tried simple coordination's.  
    spawnPlayer(theplayer, 0, 0, 3) 
    mysql_free_result(result)    
end 
  
addEventHandler("playerDataLoad", root, playerDataLoading) 
  
function playerDataSaving() 
    if get("AutoSaveOnQuit") then 
        local posX, posY, posZ = getElementPosition(source) 
        mysql_query(mysqldb, "Update players Set pos_X="..posX.." pos_Y="..posY.." pos_Z="..posZ.." Where username='"..getElementData(source, "playerUsername").."'") 
    end 
end 
  
addEventHandler("onPlayerQuit", root, playerDataSaving) 

GUI creating, events calling(client):

addEvent( "falseLogin", true) 
      
function createLoginRegisterGUI( ) 
    local X = 0.375 
    local Y = 0.375 
    local Width = 0.35 
    local Height = 0.35 
    myFont = guiCreateFont( "password.ttf", 15 ) 
    wdwLogin = guiCreateWindow(X, Y, Width, Height, "Login/Register window", true) 
    tabPanel = guiCreateTabPanel ( 0, 0.1, 1, 1, true, wdwLogin) 
    tabLogin = guiCreateTab( "Login", tabPanel ) 
    local tabRegister = guiCreateTab( "Register", tabPanel ) 
    X = 0.0825 
    Y = 0.2 
    Width = 0.25 
    Height = 0.25 
    guiCreateLabel(X, Y, Width, Height, "Username", true, tabLogin) 
    Y = 0.5 
    guiCreateLabel(X, Y, Width, Height, "Password", true, tabLogin) 
    X = 0.315 
    Y = 0.2 
    Width = 0.6 
    Height = 0.15 
    edtUser = guiCreateEdit(X, Y, Width, Height, "", true, tabLogin) 
    Y = 0.5 
    edtPass = guiCreateEdit(X, Y, Width, Height, "", true, tabLogin) 
    guiSetFont(edtPass, myFont ) 
    guiEditSetMaxLength(edtUser, 50) 
    guiEditSetMaxLength(edtPass, 50) 
    X = 0.05 
    Y = 0.75 
    Width = 0.9 
    Height = 0.15 
    btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, tabLogin) 
    --========================Regsiter=============================================== 
    X = 0.0825 
    Y = 0.21 
    Width = 0.2 
    Height = 0.2 
    guiCreateLabel(X, Y, Width, Height, "Username", true, tabRegister) 
    Y = 0.41 
    guiCreateLabel(X, Y, Width, Height, "Password", true, tabRegister) 
    Y = 0.61 
    guiCreateLabel(X, Y, Width, Height, "Repeat password", true, tabRegister) 
    X = 0.315 
    Y = 0.2 
    Width = 0.6 
    Height = 0.1 
    edtReUser = guiCreateEdit(X, Y, Width, Height, "", true, tabRegister) 
    Y = 0.4 
    edtRegPass = guiCreateEdit(X, Y, Width, Height, "", true, tabRegister) 
    Y = 0.6 
    edtRePass = guiCreateEdit(X, Y, Width, Height, "", true, tabRegister) 
    myFont = guiCreateFont( "password.ttf", 10 ) 
    guiSetFont( edtRegPass, myFont ) 
    guiSetFont( edtRePass, myFont ) 
    guiEditSetMaxLength(edtReUser, 50) 
    guiEditSetMaxLength(edtRegPass, 50) 
    guiEditSetMaxLength(edtRePass, 50) 
    X = 0.05 
    Y = 0.8 
    Width = 0.9 
    Height = 0.15 
    btnRegister = guiCreateButton(X, Y, Width, Height, "Register", true, tabRegister) 
    guiSetVisible(wdwLogin, false) 
    addEventHandler( "onClientGUIClick", btnLogin, clientSubmitLogin, false ) 
    addEventHandler( "onClientGUIClick", btnRegister, clientSubmitRegister, false )   
end 
     
addEvent("succesRegister", true) 
addEventHandler("succesRegister", resourceRoot,  
    function(password) 
        guiSetInputEnabled( false ) 
        guiSetVisible( wdwLogin, false ) 
        showCursor( false ) 
        outputChatBox("* You have registeres succesfuly! Your password is: "..password, 0, 255, 0) 
    end 
)      
      
function clientSubmitRegister(button, state) 
    if button == "left" and state == "up" then 
        local username = guiGetText(edtReUser) 
        local password = guiGetText(edtRegPass) 
        local repass = guiGetText(edtRePass) 
        if username and password then 
            triggerServerEvent( "submitRegister", localPlayer, username, password, repass ) 
        end 
    end 
end 
     
function clientSubmitLogin( button,state ) 
    if button == "left" and state == "up" then 
        local username = guiGetText( edtUser ) 
        local password = guiGetText( edtPass ) 
        if username and password then 
            triggerServerEvent( "submitLogin", localPlayer, username, password ) 
            guiSetInputEnabled( false) 
            guiSetVisible( wdwLogin, false) 
            showCursor( false ) 
        else 
            outputChatBox("Please enter a username and password.") 
        end 
    end 
end 
          
addEventHandler( "onClientResourceStart", resourceRoot, 
    function ( ) 
        createLoginRegisterGUI( ) 
        outputChatBox( "Welcome to My MTA:SA Server." )           
        if wdwLogin then 
            guiSetVisible( wdwLogin, true ) 
        else 
            outputChatBox( "An unexpected error has occurred and the log in GUI has not been created." ) 
        end 
        showCursor( true ) 
        guiSetInputEnabled( true ) 
    end 
) 
          
    addEventHandler( "falseLogin", root, 
        function( ) 
            guiSetInputEnabled( true ) 
            guiSetVisible( wdwLogin, true ) 
            showCursor( true ) 
        end 
    ) 
      

login_register (server)

local spawnX, spawnY, spawnZ = 1959.55, -1714.46, 20 
  
addEvent( "submitLogin", true ) 
addEvent( "submitRegister", true) 
      
function loginHandler(username, password ) 
    local password2=whirlpool(password) 
    local result = mysql_query( mysqldb, "SELECT * FROM players WHERE username = '"..username.. "' AND pass = '"..password2.. "'" ) 
    if not result then 
        outputDebugString( "EVENT:submitLogin mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
    else 
        if mysql_num_rows( result ) > 0 then 
            fadeCamera(source, true) 
            setCameraTarget(source, source) 
            outputChatBox( "Welcome to My Server.", source ) 
            triggerEvent("playerDataLoad", resourceRoot, source, username) 
        else 
            outputChatBox( "Invalid username and password. Please try again.",source, 255, 0, 0) 
            triggerClientEvent ( "falseLogin", root ) 
        end 
        mysql_free_result( result ) 
    end 
end 
        
function registerHandler(user, pass, repass ) 
    local result = mysql_query( mysqldb, "SELECT * FROM players WHERE username = '"..user.."'" ) 
    if not result then 
        outputDebugString( "EVENT:submitRegister mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
    else 
        if mysql_num_rows( result ) > 0 then 
            outputChatBox( "Username which you selected already exists!",source, 255, 0, 0 ) 
        else 
            if pass == repass then 
                local pass2 = whirlpool(pass) 
                local Result = mysql_query( mysqldb, "INSERT INTO players (username, pass) VALUES ('"..user.."', '"..pass2.."')" ) 
                if Result then 
                    mysql_free_result( Result ) 
                    fadeCamera(source, true) 
                    setCameraTarget(source, source) 
                    spawnPlayer(source, spawnX, spwanY, spawnZ) 
                    setElementData(source, "pMoney", get("PlayerStartingMoney")) 
                    givePlayerMoney(source, get("PlayerStartingMoney")) 
                    triggerClientEvent(source, "succesRegister", root, pass) 
                else 
                    outputDebugString( "Register not succes: mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
                end 
            else 
                    outputChatBox( "Passwords dosen't match. Please re-enter passwords!",source, 255, 0, 0 ) 
            end 
        end  
        mysql_free_result( result ) 
    end 
end 
  
addEventHandler( "submitRegister", root, registerHandler ) 
addEventHandler( "submitLogin", root, loginHandler ) 

MySQL callings are right. Inputs in database appears. I think I messed whit triggering events :roll:

Link to comment

Try

Server

addEvent( "playerDataLoad", true ) 
  
function playerDataLoading( username ) 
    local result = mysql_query( mysqldb, "Select * From players where username = '"..username.."'" ) 
    if not result then 
        outputDebugString( "EVENT:playerDataLoading mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
        return 
    end 
    setElementData( source, "playerUsername", username ) 
    spawnPlayer( source, 0, 0, 3) 
    mysql_free_result( result )    
end 
  
function playerDataSaving( ) 
    if get "AutoSaveOnQuit" then 
        local posX, posY, posZ = getElementPosition( source ) 
        mysql_query( mysqldb, "Update players Set pos_X = "..posX..", pos_Y = "..posY..",\ 
        pos_Z = "..posZ.." Where username = '"..tostring( getElementData( source, "playerUsername" ) ).."'" ) 
    end 
end 
addEventHandler( "onPlayerQuit", root, playerDataSaving ) 
addEventHandler( "playerDataLoad", root, playerDataLoading ) 

Login

local spawnX, spawnY, spawnZ = 1959.55, -1714.46, 20 
  
addEvent( "submitLogin", true ) 
addEvent( "submitRegister", true) 
      
function loginHandler(username, password ) 
    local password2 = whirlpool( password ) 
    local result = mysql_query( mysqldb, "SELECT * FROM players WHERE username = '"..username.. "' AND pass = '"..password2.. "'" ) 
    if not result then 
        outputDebugString( "EVENT:submitLogin mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
    else 
        if mysql_num_rows( result ) > 0 then 
            fadeCamera( source, true ) 
            setCameraTarget( source, source ) 
            outputChatBox( "Welcome to My Server.", source ) 
            triggerEvent( "playerDataLoad", source, username ) 
        else 
            outputChatBox( "Invalid username and password. Please try again.",source, 255, 0, 0) 
            triggerClientEvent ( "falseLogin", root ) 
        end 
        mysql_free_result( result ) 
    end 
end 
        
function registerHandler(user, pass, repass ) 
    local result = mysql_query( mysqldb, "SELECT * FROM players WHERE username = '"..user.."'" ) 
    if not result then 
        outputDebugString( "EVENT:submitRegister mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
    else 
        if mysql_num_rows( result ) > 0 then 
            outputChatBox( "Username which you selected already exists!",source, 255, 0, 0 ) 
        else 
            if pass == repass then 
                local pass2 = whirlpool( pass ) 
                local Result = mysql_query( mysqldb, "INSERT INTO players (username, pass) VALUES ('"..user.."', '"..pass2.."')" ) 
                if Result then 
                    mysql_free_result( Result ) 
                    fadeCamera( source, true ) 
                    setCameraTarget( source, source ) 
                    spawnPlayer( source, spawnX, spwanY, spawnZ ) 
                    setElementData( source, "pMoney", get( "PlayerStartingMoney" ) ) 
                    givePlayerMoney( source, get( "PlayerStartingMoney" ) ) 
                    triggerClientEvent( source, "succesRegister", root, pass ) 
                else 
                    outputDebugString( "Register not succes: mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
                end 
            else 
                    outputChatBox( "Passwords dosen't match. Please re-enter passwords!",source, 255, 0, 0 ) 
            end 
        end  
        mysql_free_result( result ) 
    end 
end 
  
addEventHandler( "submitRegister", root, registerHandler ) 
addEventHandler( "submitLogin", root, loginHandler ) 

Link to comment

Remember lua is case sensitive.

Example

  
var = 5 
print( var ) -- 5 
print( Var ) -- nil 

So your code:

function playerDataLoading(thePlayer, username) -- in argument thePlayer 
    local result = mysql_query(mysqldb, "Select * From players where username='"..username.."'") 
    if not result then 
        outputDebugString( "EVENT:playerDataLoading mysql_query failed: (" .. tostring( mysql_errno( mysqldb ) ).. ") " ..tostring(  mysql_error( mysqldb ) ) ) 
        mysql_free_result(result) 
        return 
    end 
    local field = mysql_fetch_field(result) 
    setElementData(thePlayer, "playerUsername", username) 
    spawnPlayer(theplayer, 0, 0, 3) -- theplayer is nil 
    mysql_free_result(result)    
end 

Events:

I always see how scripters triggered some event

Example:

-- wrong code 
-- Server 
addCommandHandler( 'some', 
    function( player ) 
        triggerClientEvent( 'some',root,player ) 
        --[[ 
            So it triggered base element root ( instead of source player ) ( see on wiki ) then player element 
            Why? 
            If 2 argument is base element and not need triggered next optional arguments. 
        ]] 
    end 
)    
  
-- Client 
addEvent( 'some',true ) 
addEventHandler( 'some',root, 
    function( player ) -- why need use player? If you trigger source event ( not root ) you can use source in event. 
        local x,y,z = getElementPosition( player )  
        createExplosion( x,y,z,4 ) 
    end 
)    
  
  
  
  
-- correct 
-- Server 
addCommandHandler( 'some', 
    function( player ) 
        triggerClientEvent( 'some',player ) 
    end 
) 
  
-- Client 
addEvent( 'some',true ) 
addEventHandler( 'some',root, 
    function( ) 
        local x,y,z = getElementPosition( source )  
        createExplosion( x,y,z,4 ) 
    end 
)    
  
--[[ 
    This examples triggered for all elements( root ) 
]] 

Your code

triggerEvent("playerDataLoad", resourceRoot, source, username) 

You triggered base element resource root and then source and then username , but why?

You can trigger base element source ( source player ) and then username.

https://wiki.multitheftauto.com/wiki/Pre ... ables_list

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

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