Daron90 Posted June 28, 2013 Posted June 28, 2013 Hello all. I downloaded a resource some days ago and it works very well, but i want to add a function. I'm a pawno scripter and i'm starting with Lua.. maybe its just a noob change but i don't know how to make it for now.. if you can help me, i'll really tank you. Here is the Lua code: --[[ Save Load System ]]-- --[[ By |Mr|-Talal07-| ]]-- --[[ Don't Remove This ]]-- LoadKey = "1" -- to load your position. SaveKey = "2" -- yo save your position. function getPlayerSaves(player) local x,y,z = getElementData(player,"x"),getElementData(player,"y"),getElementData(player,"z") local rx,ry,rz = getElementData(player,"rx"),getElementData(player,"ry"),getElementData(player,"rz") local vx,vy,vz = getElementData(player,"vx"),getElementData(player,"vy"),getElementData(player,"vz") local id,hp = getElementData(player,"id"),getElementData(player,"hp") return x,y,z,rx,ry,rz,vx,vy,vz,id,hp end function SavePosition(player,x,y,z,rx,ry,rz,vx,vy,vz,id,hp) setElementData(player,"x",x) setElementData(player,"y",y) setElementData(player,"z",z) setElementData(player,"rx",rx) setElementData(player,"ry",ry) setElementData(player,"rz",rz) setElementData(player,"vx",vx) setElementData(player,"vy",vy) setElementData(player,"vz",vz) setElementData(player,"id",id) setElementData(player,"hp",hp) setElementData(player,"SL",true) end function LoadPosition(player) local x,y,z,rx,ry,rz,vx,vy,vz,id,hp = getPlayerSaves(player) setElementModel(getPedOccupiedVehicle(player),tonumber(id)) setElementHealth(getPedOccupiedVehicle(player),hp) setElementPosition(getPedOccupiedVehicle(player),x,y,z) setElementRotation(getPedOccupiedVehicle(player),rx,ry,rz) addVehicleUpgrade(getPedOccupiedVehicle(player),1010) setTimer(setElementFrozen,1000,1,getPedOccupiedVehicle(player),false) setTimer(setElementVelocity,1000,1,getPedOccupiedVehicle(player),vx,vy,vz) end function LoadPlayer(player) if not isPedInVehicle(player) then return end if getElementData(player,"SL") == true then setElementFrozen(getPedOccupiedVehicle(player),true) LoadPosition(player) outputChatBox("*[LOAD] Position loaded !!",player,0,255,0) else outputChatBox("*[POSITION] You didn't saved any position yet. Use 2 key to save position.",player,255,0,0) end end function SavePlayer(player) if not isPedInVehicle(player) then return end local x,y,z = getElementPosition(getPedOccupiedVehicle(player)) local rx,ry,rz = getElementRotation(getPedOccupiedVehicle(player)) local vx,vy,vz = getElementVelocity(getPedOccupiedVehicle(player)) local id,hp = getElementModel(getPedOccupiedVehicle(player)),getElementHealth(getPedOccupiedVehicle(player)) SavePosition(player,x,y,z,rx,ry,rz,vx,vy,vz,id,hp) outputChatBox("*[sAVE] Position saved !!",player,0,255,0) end addEventHandler("onPlayerWasted",root, function () setElementData(source,"SL",false) outputChatBox("*[POSITION] The current position saved were removed.",source,255,0,0) end ) addEventHandler("onGamemodeMapStart",root, function () for i,player in ipairs(getElementsByType("player")) do setElementData(player,"SL",false) outputChatBox("*[POSITION] The current position saved were removed.",player,255,0,0) end end ) addEventHandler("onPlayerSpawn",root, function () setElementData(source,"SL",false) outputChatBox("*[POSITION] The current position saved were removed.",source,255,0,0) end ) addEventHandler("onPlayerJoin",root, function () setElementData(source,"SL",false) bindKey(source,LoadKey,"down",LoadPlayer) bindKey(source,SaveKey,"down",SavePlayer) end ) addEventHandler("onResourceStart",resourceRoot, function () for i,player in ipairs(getElementsByType("player")) do setElementData(player,"SL",false) bindKey(player,LoadKey,"down",LoadPlayer) bindKey(player,SaveKey,"down",SavePlayer) end end ) The change is add a limit to use the LOAD key. Maybe it's posible with a counter or something like ... For example.. you only can use 1 key (Load position) 5 times, and when the player is respawned set it as 0 again. Also a thing.. maybe can be added is.. save the nos. If you had NOS before saving.. load it, else.. no. This is just a plus But the most important is that about the limit. Hope you can help me. Regards
Castillo Posted June 28, 2013 Posted June 28, 2013 You could use tables. http://lua-users.org/wiki/TablesTutorial
Vector Posted June 29, 2013 Posted June 29, 2013 local __count = 0; -- this hold number of times the user press the "load" key local __max_count = 5; -- this hold maximum number of times that user can press "load" key. addEventHandler ("onPlayerSpawn", root, function () setElementData(source,"SL",false); outputChatBox("*[POSITION] The current position saved were removed.",source,255,0,0); -- reset the count. __count = 0; end); addEventHandler("onPlayerJoin",root, function () setElementData(source,"SL",false); bindKey(source,LoadKey,"down", function (thePlayer) -- player press "load" key more than 5 times ? if __count < __max_count then -- no, so ... LoadPlayer (thePlayer); -- increment count. __count = __count + 1; else -- "load" pressed 5 times. -- do whatever, output an error or something ... end; end); bindKey(source,SaveKey,"down",SavePlayer) end);
Castillo Posted June 29, 2013 Posted June 29, 2013 That'll create a global counter, so if one player uses it 5 times and another tries to use it, he won't be able to. That's why I told him to use tables.
Vector Posted June 29, 2013 Posted June 29, 2013 oh. forgot it. or setElementData (thePlayer, "count", ...) getElementData (thePlayer, "count"); I guess.
Castillo Posted June 29, 2013 Posted June 29, 2013 No point on using element data when you can use tables.
Vector Posted June 29, 2013 Posted June 29, 2013 local __max_count = 5; local __count = {}; addEventHandler ("onPlayerSpawn", root, function () setElementData(source,"SL",false); outputChatBox("*[POSITION] The current position saved were removed.",source,255,0,0); -- reset the count. __count [source] = 0; end); addEventHandler("onPlayerJoin",root, function () setElementData(source,"SL",false); bindKey(source,LoadKey,"down", function (thePlayer) -- player press "load" key more than 5 times ? if __count [thePlayer] < __max_count then -- no, so ... LoadPlayer (thePlayer); -- increment count. __count [thePlayer] = __count[thePlayer] + 1; else -- "load" pressed 5 times. -- do whatever, output an error or something ... end; end); bindKey(source,SaveKey,"down",SavePlayer) end);
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