Submitter Posted July 14, 2016 Posted July 14, 2016 How to set a cameraMatrix on a player join to his last position before he disconnect from the server?
Noki Posted July 14, 2016 Posted July 14, 2016 (edited) You can store the coordinates of the player's last location in a database of some sorts. When they join, query the coordinates for a row that matches their serial. If it exists, set it. function saveMatrix() local serial = getPlayerSerial(source) local x, y, z, lx, ly, lz = getCameraMatrix(source) dbExec(db, "UPDATE `saveMatrix` SET `x` = ?, `y` = ?, `z` = ?, `lx` = ?, `ly` = ?, `lz` = ? WHERE `serial` = ?", serial, x, y, z, lx, ly, lz) end addEventHandler("onPlayerQuit", root, saveMatrix) function loadMatrix() -- db is not defined, use your own database here local serial = getPlayerSerial(source) local result = dbPoll(dbQuery(db, "SELECT `x`,` y`, `z`, `lx, `ly`, `lz` FROM `saveMatrix` WHERE `serial` = ? LIMIT 1", serial), -1) if (#result >= 1) then local x, y, z, lx, ly, lz = result[1].x, result[1].y, result[1].z, result[1].lx, result[1].ly, result[1].lz setCameraMatrix(source, x, y, z, lx, ly, lz) end end addEventHandler("onPlayerJoin", root, loadMatrix) Here's a basic rubric to go off. Edited July 14, 2016 by Guest
Noki Posted July 14, 2016 Posted July 14, 2016 -- db is not defined, use your own database here You also need to insert it if it's not currently there, which I did not write.
roaddog Posted July 14, 2016 Posted July 14, 2016 You got an extra quotation in the last line of code after root here: addEventHandler("onPlayerJoin", root", loadMatrix)
Walid Posted July 14, 2016 Posted July 14, 2016 Try this one : -- db is not defined, use your own database here function saveMatrix() local serial = getPlayerSerial(source) local x, y, z, lx, ly, lz = getCameraMatrix(source) local camera = toJSON({x, y, z, lx, ly, lz}) dbExec(db, "UPDATE `saveMatrix` SET `camera` = ? WHERE `serial` = ?", serial, camera) end addEventHandler("onPlayerQuit", root, saveMatrix) function loadMatrix() local serial = getPlayerSerial(source) local result = dbPoll(dbQuery(db, "SELECT * FROM `saveMatrix` WHERE `serial` = ?", serial), -1) if (#result >= 1) then local x, y, z, lx, ly, lz = unpack( fromJSON(result[1]["camera"])) setCameraMatrix(source, x, y, z, lx, ly, lz) end end addEventHandler("onPlayerJoin", root, loadMatrix)
Noki Posted July 14, 2016 Posted July 14, 2016 You got an extra quotation in the last line of code after root here: addEventHandler("onPlayerJoin", root", loadMatrix) Cheers. Edited it. I don't have a DB script Then you can just use account data.
Walid Posted July 14, 2016 Posted July 14, 2016 I don't have a DB script you can use sth like this: local database = "dbName.db" local db = dbConnect("sqlite", database) dbExec(db, "CREATE TABLE IF NOT EXISTS saveMatrix (serial TEXT, camera TEXT)" )
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now