igthomas Posted February 20, 2013 Share Posted February 20, 2013 Hi I made an afk script , player gets moved to a specified interior and when he writes back it gets him back where he left being afk, but for some reason it gets the position of the interior not the one where he left. Oh and do you suggest doing setTimer every 1 seconds or is it bad for the server? function onIdle (thePlayer) for index, thePlayer in ipairs(getElementsByType("player"))do if (getPlayerIdleTime(thePlayer) > 5000) then --120000 local oldPos = getElementPosition (thePlayer) setElementData(thePlayer,"away",oldPos) setElementInterior ( thePlayer, 1, 681.5, -455.46, -25.6 ) end end end setTimer(onIdle, 1000, 0) function onBack (thePlayer) if getElementData(thePlayer,"away") then local oldPos = tonumber(getElementData(thePlayer,"away")) setElementInterior ( thePlayer, 0, oldPos ) end end addCommandHandler("back", onBack) Link to comment
tosfera Posted February 20, 2013 Share Posted February 20, 2013 try this: function onIdle (thePlayer) for index, thePlayer in ipairs(getElementsByType("player"))do if (getPlayerIdleTime(thePlayer) > 5000) then --120000 local x,y,z = getElementPosition (thePlayer) setElementData(thePlayer,"away-x",x) setElementData(thePlayer,"away-y",y) setElementData(thePlayer,"away-z",z) setElementInterior ( thePlayer, 1, 681.5, -455.46, -25.6 ) end end end setTimer(onIdle, 1000, 0) function onBack (thePlayer) if getElementData(thePlayer,"away") then local x = tonumber(getElementData(thePlayer,"away-x")) local y = tonumber(getElementData(thePlayer,"away-y")) local z = tonumber(getElementData(thePlayer,"away-z")) setElementInterior ( thePlayer, 0, x, y, z ) end end addCommandHandler("back", onBack) Link to comment
igthomas Posted February 20, 2013 Author Share Posted February 20, 2013 Thanks for help but it didn't work, I mean when I write back it doesn't do anything Link to comment
tosfera Posted February 20, 2013 Share Posted February 20, 2013 (edited) okee let me test the code than, hang on a sec! edit: okee I made 2 commands out of it, it works now. The thing is, he didn't add the elementData to the player, so it couldn't go back. right now I've saved them in your account data, and it works. All you've to do it create the functions around it again. if you got a minute I'll do it for you: addCommandHandler("afk", function (source) local x,y,z = getElementPosition (source) setAccountData(getPlayerAccount(source),"away-x",x) setAccountData(getPlayerAccount(source),"away-y",y) setAccountData(getPlayerAccount(source),"away-z",z) setElementInterior ( source, 1, 681.5, -455.46, -25.6 ) end) addCommandHandler("back", function (source) if getAccountData(getPlayerAccount(source),"away-x") then local x = getAccountData(getPlayerAccount(source),"away-x") local y = getAccountData(getPlayerAccount(source),"away-y") local z = getAccountData(getPlayerAccount(source),"away-z") setElementInterior ( source, 0, x, y, z ) end end) Edited February 20, 2013 by Guest Link to comment
csiguusz Posted February 20, 2013 Share Posted February 20, 2013 function onIdle (thePlayer) for index, thePlayer in ipairs(getElementsByType("player"))do if (getPlayerIdleTime(thePlayer) > 5000) then --120000 local oldX, oldY, oldZ = getElementPosition (thePlayer) setElementData(thePlayer,"away",{oldX, oldY, oldZ}) setElementInterior ( thePlayer, 1, 681.5, -455.46, -25.6 ) end end end setTimer(onIdle, 5000, 0) function onBack (thePlayer) if getElementData(thePlayer,"away") then local oldPos = getElementData(thePlayer,"away") setElementInterior ( thePlayer, 0, oldPos[1], oldPos[2], oldPos[3] ) end end addCommandHandler("back", onBack) setElementPosition returns 3 values not just one! And checking every player every second is not a good idea I think... Link to comment
tosfera Posted February 20, 2013 Share Posted February 20, 2013 function onIdle (thePlayer) for index, thePlayer in ipairs(getElementsByType("player"))do if (getPlayerIdleTime(thePlayer) > 5000) then --120000 local oldX, oldY, oldZ = getElementPosition (thePlayer) setElementData(thePlayer,"away",{oldX, oldY, oldZ}) setElementInterior ( thePlayer, 1, 681.5, -455.46, -25.6 ) end end end setTimer(onIdle, 5000, 0) function onBack (thePlayer) if getElementData(thePlayer,"away") then local oldPos = getElementData(thePlayer,"away") setElementInterior ( thePlayer, 0, oldPos[1], oldPos[2], oldPos[3] ) end end addCommandHandler("back", onBack) setElementPosition returns 3 values not just one! And checking every player every second is not a good idea I think... use his code. Link to comment
igthomas Posted February 20, 2013 Author Share Posted February 20, 2013 Thanks for your help guys csiguusz your script worked just one thing how could I stop the checking if the player is still afk? Because I have a feeling that if the player stays afk and it check everytime and save the position, it will save the new one where he's in the interior right? Link to comment
csiguusz Posted February 20, 2013 Share Posted February 20, 2013 Yes it will save the new position in the interior. For example just check if the player is in interior 0 (in the 3rd line): if (getPlayerIdleTime(thePlayer) > 3000) and ( getElementInterior == 0 ) then --120000 Link to comment
igthomas Posted February 20, 2013 Author Share Posted February 20, 2013 Awesome works thanks man Link to comment
csiguusz Posted February 20, 2013 Share Posted February 20, 2013 You are welcome! Just one advice: don't just copy my code and be happy because it's working. Try to understand it! So next time you will be able to do it alone. Link to comment
igthomas Posted February 20, 2013 Author Share Posted February 20, 2013 Well the funny thing is that I was very close once just instead of setElementData(thePlayer,"away",{oldX, oldY, oldZ}) I did this without { } Anyways now I know my mistake and won't repeat them ;D setElementData(thePlayer,"away",oldX, oldY, oldZ) Link to comment
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