Jump to content

[Help] Joining at last location


Moomasterq

Recommended Posts

  
local spawnX = math.random(-1715,-1705) 
local spawnY = math.random(-495,-485) 
local spawnZ = 14 
  
local playeraccount = getPlayerAccount(source) 
  
--Join Handler, how player joins when spawns 
function joinHandler() 
    --Spawning Player 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.X") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.Y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.Z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerJoin", getRootElement(), joinHandler) 
  
--Quit handler, handles the position of player on last login 
function quitHandler() 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local logoutX, logoutY, logoutZ = getElementPosition(source) 
        setAccountData(playeraccount, "cowpi.loginlocation.X", logoutX) 
        setAccountData(playeraccount, "cowpi.loginlocation.Y", logoutY) 
        setAccountData(playeraccount, "cowpi.loginlocation.Z", logoutZ) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 
  
  

Hello, this is my first time posting here so pardon me if I break any rules, but I have encountered an issue with logging in at the same location. Using the setAccountData I attempted to make the login location the same as the exit location. In theory I think my script would work, but unfortunately, it doesn't.

I have no idea what would cause this to not work, so any insight is helpful. My only concerns are trying to get this to work, and how can I let new players join as they don't have a last login.

Edited by Guest
Link to comment
When a player joins, he won't be logged, you have to use the event onPlayerLogin instead.

Ok, I'm relatively new to lua (came from python), so I'm learning gradually (so pardon my idiocracy). I changed to the onPlayerLogin event but it still does not work.

--Join Handler, how player joins when spawns 
function joinHandler() 
    --Spawning Player 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.X") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.Y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.Z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 

Should I change my login method from one of them that I found in MTA resources or should I create the basic one from the wiki here.

Link to comment

Ok I defined joinhandler, and then did a refreshall in console and MTA gave me an error concerning getPlayerAccount as I was trying to take in source instead of thePlayer. Here is my revised script, but I now spawn at 0, 0, 0. If I set player health to 0 through the admin panel I spawn at my original spawn points that I selected (spawnX etc.), but if I reconnect from there I will spawn at 0, 0, 0 once more.

Here is my current code

local spawnX = math.random(-1715,-1705) 
local spawnY = math.random(-495,-485) 
local spawnZ = 14 
  
local playeraccount = getPlayerAccount(thePlayer) 
  
--Join Handler, how player joins when spawns 
function joinHandler(_, playeraccount) 
    --Spawning Player 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.X") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.Y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.Z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 
  
--Quit handler, handles the position of player on last login 
function quitHandler(_, playeraccount) 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local logoutX, logoutY, logoutZ = getElementPosition(source) 
        setAccountData(playeraccount, "cowpi.loginlocation.X", logoutX) 
        setAccountData(playeraccount, "cowpi.loginlocation.Y", logoutY) 
        setAccountData(playeraccount, "cowpi.loginlocation.Z", logoutZ) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 

Link to comment
Remove that getPlayerAccount line, is not required and is wrong.

Ok, even when I remove/disable that line, my player still spawns at 0, 0 ,0. Does account data get stored in strings and requires me to tonumber() it or do you think that my leaving function is improper?

--Join Handler, how player joins when spawns 
function joinHandler(_, playeraccount) 
    --Spawning Player 
    if (playeraccount) then --and not isGuestAccount(playeraccount) then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.x") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
        --spawnPlayer(source, spawnX, spawnY, spawnZ) 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 
  
--Quit handler, handles the position of player on last login 
function quitHandler(_, playeraccount) 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local logoutX, logoutY, logoutZ = getElementPosition(thePlayer) 
        setAccountData(playeraccount, "cowpi.loginlocation.x", logoutX) 
        setAccountData(playeraccount, "cowpi.loginlocation.y", logoutY) 
        setAccountData(playeraccount, "cowpi.loginlocation.z", logoutZ) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 

I also have a question about how should I go about making players who never joined before have a spawn? Should I set login X Y and Z as spawnX,y,z and have it redefined if they have a location or no?

Link to comment
  • Discord Moderators

Remember, this script runs when the resource starts (which is when your server starts).

The wiki contains documentation for every function and event.

You would most likely want to store a set of default spawn coordinates to use, when the player is logging in for the first time.

In your function quitHandler, you must evaluate your parameters. You've connected it to onPlayerQuit - see the wiki for this event.

Events usually -

onElementAction

Where the Element (eg. onPlayerLogin) is stored under the hidden "source" variable and the variables returned relating to Action are passed into the function.

an example:

local default_spawn_X, default_spawn_Y, default_spawn_Z = 0, 0, 3 
  
--Join Handler, how player joins when spawns 
function joinHandler(_, playeraccount) 
    --Spawning Player 
    if (playeraccount) then --and not isGuestAccount(playeraccount) then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.x") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
        --spawnPlayer(source, spawnX, spawnY, spawnZ) 
    else 
        spawnPlayer(source, default_spawn_X, default_spawn_Y, default_spawn_Z) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 
  
--Quit handler, handles the position of player on last login 
function quitHandler() 
    local playeraccount = getPlayerAccount (source) 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local logoutX, logoutY, logoutZ = getElementPosition(source) 
        setAccountData(playeraccount, "cowpi.loginlocation.x", logoutX) 
        setAccountData(playeraccount, "cowpi.loginlocation.y", logoutY) 
        setAccountData(playeraccount, "cowpi.loginlocation.z", logoutZ) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 

Good luck and happy scripting!

Link to comment

Thanks thisisdoge I have it working now. I already had the spawn coordinates set but I didn't want to include them in the prior post because they would clutter it more so than it already is.

I was aware of the wiki and the events, but thank you anyway for linking it and being helpful, had I not been in the posistion that I'm in that would've been immensely helpful.

Also thank you to solidsnake14 who helped me out originally clean up some of my script.

Here is the final script if anyone ever wants to use/reference it in the future

local spawnX = math.random(-1715,-1705) 
local spawnY = math.random(-495,-485) 
local spawnZ = 14 
  
--local playeraccount = getPlayerAccount(thePlayer) 
  
--Join Handler, how player joins when spawns 
function joinHandler(_, playeraccount) 
    --Spawning Player 
    if (playeraccount) then --and not isGuestAccount(playeraccount) then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.x") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
        --spawnPlayer(source, spawnX, spawnY, spawnZ) 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 
  
--Quit handler, handles the position of player on last login 
function quitHandler() 
    local playeraccount = getPlayerAccount (source) 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local logoutX, logoutY, logoutZ = getElementPosition(source) 
        setAccountData(playeraccount, "cowpi.loginlocation.x", logoutX) 
        setAccountData(playeraccount, "cowpi.loginlocation.y", logoutY) 
        setAccountData(playeraccount, "cowpi.loginlocation.z", logoutZ) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 

Link to comment

Ok, after having this working, I decided I haven't had enough and I'd like to set up the function for guest accounts/first time joining accounts. However, I have no way of doing this as of now. Here is what I have so far, yet it does not work.

--Join Handler, how player joins when spawns 
function joinHandler(_, playeraccount) 
    --Spawning Player 
    if (getAccountData(playeraccount, "cowpi.loginlocation.y") then 
        if (playeraccount) then --and not isGuestAccount(playeraccount) then 
            local loginX = getAccountData(playeraccount, "cowpi.loginlocation.x") 
            local loginY = getAccountData(playeraccount, "cowpi.loginlocation.y") 
            local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.z") 
            spawnPlayer(source, loginX, loginY, loginZ) 
        else 
            spawnPlayer(source, spawnX, spawnY, spawnZ) 
        end 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 

I cut out the spawn variables so you don't have to look at them and cut out the quitHandler as that isn't really needed now...

Link to comment
Well, if you want to spawn the player without having to login, then use 'onPlayerJoin'.

If I change it to onPlayerJoin it then makes the player spawn instantly. If I removed guest accounts I could force onPlayerLogin essentailly, but then I have no where for the new players to spawn on their first spawn.

Link to comment

I have no guest login function, essentially removing the guest login option. I also disabled the logout feature, so no guests can be created, ever.

For the second much more confusing sentence, I was asking if it'd be possible to make it so that once the player leaves, it sets a data value of 1 to account data making it so the player has essentially been "tracked". If the value is empty it will spawn you at the spawn coordinates instead of the last location.

Link to comment

Ok, thanks for all the help, and for putting up with me as I slowly transition from my lovely python syntax world to this creation which is lua. (Also thanks for putting up with David1544).

Here is my final script if anyone would like to ever use it/reference it in their own creation

--Join Handler, how player joins when spawns 
function joinHandler(_, playeraccount) 
    --Spawning Player 
    if getAccountData(playeraccount, "did.they.ever.login") == 1 then 
        local loginX = getAccountData(playeraccount, "cowpi.loginlocation.x") 
        local loginY = getAccountData(playeraccount, "cowpi.loginlocation.y") 
        local loginZ = getAccountData(playeraccount, "cowpi.loginlocation.z") 
        spawnPlayer(source, loginX, loginY, loginZ) 
    else 
        spawnPlayer(source, spawnX, spawnY, spawnZ) 
    end 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to CowPi MTA", source) 
end 
addEventHandler("onPlayerLogin", getRootElement(), joinHandler) 
  
--Quit handler, handles the position of player on last login 
function quitHandler() 
    local playeraccount = getPlayerAccount (source) 
    if (playeraccount) and not isGuestAccount(playeraccount) then 
        local logoutX, logoutY, logoutZ = getElementPosition(source) 
        setAccountData(playeraccount, "cowpi.loginlocation.x", logoutX) 
        setAccountData(playeraccount, "cowpi.loginlocation.y", logoutY) 
        setAccountData(playeraccount, "cowpi.loginlocation.z", logoutZ) 
        setAccountData(playeraccount, "did.they.ever.login", 1) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 

Just make sure to set the setaccountdata things to whatever you'd like to call them, and to define spawnX/Y/Z as well. Once again thanks.

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