zer0w Posted January 17, 2012 Share Posted January 17, 2012 Ok, then, try this: local spawnpoint addEventHandler("onResourceStart", resourceRoot, function() spawnpoint = getRandomSpawnPoint() resetMapInfo() for i,player in ipairs(getElementsByType("player")) do spawn(player) end end ) function joinHandler ( ) spawnPlayer (source, -1969.2669677734, 137.71185302734, 30, 0, 0, 0) fadeCamera (source, true) setCameraTarget ( source, source ) end addEventHandler ( "onPlayerJoin" , getRootElement ( ) , joinHandler ) function greetPlayer ( ) outputChatBox ( "Welcome to Zer0w's server" , source, 0, 159, 255 ) end addEventHandler ( "onPlayerLogin", getRootElement(), greetPlayer ) function onPlayerQuit() local playerAccount = getPlayerAccount(source) if (playerAccount) and not isGuestAccount(playerAccount) then local playerMoney = getPlayerMoney(source) local playerSkin = getElementModel(source) setAccountData(playerAccount, "money", playerMoney) setAccountData(playerAccount, "skin", playerSkin) end end addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuit) function onPlayerLogin() local playerAccount = getPlayerAccount(source) if (playerAccount) and not isGuestAccount(playerAccount) then local playerMoney = tonumber(getAccountData(playerAccount, "money")) local playerSkin = tonumber(getAccountData(playerAccount, "skin")) if (playerMoney and playerSkin) then setPlayerMoney(source, playerMoney) setElementModel(source, playerSkin) end end end addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin) function spawn(player) if not isElement(player) then return end if get("spawnreset") == "onSpawn" then spawnpoint = getRandomSpawnPoint() end exports.spawnmanager:spawnPlayerAtSpawnpoint(player,spawnpoint,false) setElementModel(player, tonumber(getElementData(player,"tempSkin"))) fadeCamera(player, true) setCameraTarget(player, player) showChat(player, true) local weapons = getElementData(player,"tempWeapons") if (weapons and weapons ~= "") then for weapon, ammo in pairs(fromJSON(weapons)) do if (weapon and ammo) then giveWeapon(player, tonumber(weapon), tonumber(ammo)) end end end end function getRandomSpawnPoint () local spawnpoints = getElementsByType("spawnpoint") return spawnpoints[math.random(1,#spawnpoints)] end addEventHandler("onPlayerJoin", root, function() spawn(source) end ) addEventHandler("onPlayerQuit",root, function () if getPlayerCount() == 1 and get("spawnreset") == "onServerEmpty" then spawnpoint = getRandomSpawnPoint() end end ) addEventHandler("onPlayerWasted", root, function() weaponSlots = 12 local weaponsTable = {} for slot=1, weaponSlots do local weapon = getPedWeapon( source, slot ) local ammo = getPedTotalAmmo( source, slot ) if (weapon > 0 and ammo > 0) then weaponsTable[weapon] = ammo end end setElementData(source,"tempSkin",getElementModel(source)) setElementData(source,"temWeapons",toJSON(weaponsTable)) setTimer(spawn, 1800, 1, source) end ) Was hoping for it to work, but unfortunately it didn't save, i tried to give myself a weapon and try and logout and then /kill and then another /login but weapons were gone, thanks for the effort though Link to comment
Castillo Posted January 17, 2012 Share Posted January 17, 2012 That's because this only saves when respawn, it doesn't save when you quit and login back. Link to comment
zer0w Posted January 17, 2012 Share Posted January 17, 2012 That's because this only saves when respawn, it doesn't save when you quit and login back. Actually tried the respawn too while having guns, also removes it Link to comment
Castillo Posted January 17, 2012 Share Posted January 17, 2012 Ok, I found out what was my problem, I put "temWeapons", I forgot the "P" for "temp". I also added now to save after quit and give them back after login. local spawnpoint addEventHandler("onResourceStart", resourceRoot, function() spawnpoint = getRandomSpawnPoint() resetMapInfo() for i,player in ipairs(getElementsByType("player")) do spawn(player) end end ) function onPlayerLogin() outputChatBox ( "Welcome to Zer0w's server" , source, 0, 159, 255 ) local playerAccount = getPlayerAccount(source) if (playerAccount) and not isGuestAccount(playerAccount) then local playerMoney = tonumber(getAccountData(playerAccount, "money")) local playerSkin = tonumber(getAccountData(playerAccount, "skin")) if (playerMoney and playerSkin) then setPlayerMoney(source, playerMoney) setElementModel(source, playerSkin) end local playerWeapons = getAccountData(playerAccount, "weapons") if (playerWeapons and playerWeapons ~= "") then giveWeaponsFromJSON(source, playerWeapons) end end end addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin) function spawn(player) if not isElement(player) then return end if get("spawnreset") == "onSpawn" then spawnpoint = getRandomSpawnPoint() end exports.spawnmanager:spawnPlayerAtSpawnpoint(player,spawnpoint,false) setElementModel(player, tonumber(getElementData(player,"tempSkin")) or math.random(312)) fadeCamera(player, true) setCameraTarget(player, player) showChat(player, true) local weapons = getElementData(player,"tempWeapons") giveWeaponsFromJSON(player, weapons) end function getRandomSpawnPoint () local spawnpoints = getElementsByType("spawnpoint") return spawnpoints[math.random(1,#spawnpoints)] end addEventHandler("onPlayerJoin", root, function() spawnPlayer (source, -1969.2669677734, 137.71185302734, 30, 0, 0, 0) fadeCamera (source, true) setCameraTarget ( source, source ) spawn(source) end ) addEventHandler("onPlayerQuit",root, function () if getPlayerCount() == 1 and get("spawnreset") == "onServerEmpty" then spawnpoint = getRandomSpawnPoint() end local playerAccount = getPlayerAccount(source) if (playerAccount) and not isGuestAccount(playerAccount) then local playerMoney = getPlayerMoney(source) local playerSkin = getElementModel(source) setAccountData(playerAccount, "money", playerMoney) setAccountData(playerAccount, "skin", playerSkin) local weapons = convertWeaponsToJSON(source) setAccountData(playerAccount, "weapons", weapons) end end ) addEventHandler("onPlayerWasted", root, function() local weapons = convertWeaponsToJSON(source) setElementData(source,"tempSkin",getElementModel(source)) setElementData(source,"tempWeapons",weapons) setTimer(spawn, 1800, 1, source) end ) function convertWeaponsToJSON(player) local weaponSlots = 12 local weaponsTable = {} for slot=1, weaponSlots do local weapon = getPedWeapon( source, slot ) local ammo = getPedTotalAmmo( source, slot ) if (weapon > 0 and ammo > 0) then weaponsTable[weapon] = ammo end end return toJSON(weaponsTable) end function giveWeaponsFromJSON(player, weapons) if (weapons and weapons ~= "") then for weapon, ammo in pairs(fromJSON(weapons)) do if (weapon and ammo) then giveWeapon(player, tonumber(weapon), tonumber(ammo)) end end end end Link to comment
zer0w Posted January 17, 2012 Share Posted January 17, 2012 Thanks will try this right away EDIT: thank you so much, now I got everything working. Thanks I tried it for both so /kill and I kept the weapon same for disconnect so it works Link to comment
zer0w Posted January 17, 2012 Share Posted January 17, 2012 (edited) If you wanted to know for what I was aiming for and what I will be using it for. Offtopic: IRRELEVANT Edited January 18, 2012 by Guest Link to comment
Castillo Posted January 17, 2012 Share Posted January 17, 2012 Yeah, I know about Open Vice. P.S: I don't care for what do you use it, I'm here to help. Link to comment
zombienation Posted January 30, 2012 Share Posted January 30, 2012 hello, i have a question, so will this work when i add these lines in the spawnscript i use? or do i need to replace it with something in the script? if i need to replace, can i show the spawn lines here? thx Link to comment
Castillo Posted January 30, 2012 Share Posted January 30, 2012 That script is the "play" game mode but edited. Link to comment
zombienation Posted January 30, 2012 Share Posted January 30, 2012 so it wont work if i add it to my mode then? its not the play game mode.. Link to comment
Castillo Posted January 30, 2012 Share Posted January 30, 2012 No, it won't, but you can copy the weapon saving part, is not that hard. Link to comment
zombienation Posted January 30, 2012 Share Posted January 30, 2012 ok i'll try it quick, just copy and paste, no replacing? Edit: cant make it work i tried this weaponSlots = 11 ammoSlot = {} gunSlot = {} for slot = 1, weaponSlots do if getPedWeapon ( player, slot ) then ammoSlot[slot] = getPedTotalAmmo ( player, slot ) gunSlot[slot] = getPedWeapon ( player, slot ) end end -- If you want save your WEAPON CODE later than you can use ElementData -> global for gun = 1, #gunSlot do if gunSlot[gun] ~= nil then setElementData(player, "weapon#"..gun, getWeaponNameFromID(gunSlot[gun]) setElementData(player, "ammo#"..gun, ammoSlot[gun]) end end and this addEventHandler("onPlayerWasted", root, function() local weapons = convertWeaponsToJSON(source) setElementData(source,"tempSkin",getElementModel(source)) setElementData(source,"tempWeapons",weapons) setTimer(spawn, 1800, 1, source) end ) function convertWeaponsToJSON(player) local weaponSlots = 12 local weaponsTable = {} for slot=1, weaponSlots do local weapon = getPedWeapon( source, slot ) local ammo = getPedTotalAmmo( source, slot ) if (weapon > 0 and ammo > 0) then weaponsTable[weapon] = ammo end end return toJSON(weaponsTable) end function giveWeaponsFromJSON(player, weapons) if (weapons and weapons ~= "") then for weapon, ammo in pairs(fromJSON(weapons)) do if (weapon and ammo) then giveWeapon(player, tonumber(weapon), tonumber(ammo)) end end end end Link to comment
Castillo Posted January 30, 2012 Share Posted January 30, 2012 You're using two different systems. addEventHandler("onPlayerWasted", root, function() local weapons = convertWeaponsToJSON(source) setElementData(source,"tempWeapons",weapons) end ) addEventHandler("onPlayerSpawn", root, function () local weapons = getElementData(source,"tempWeapons") if (weapons) then giveWeaponsFromJSON(source, weapons) end end ) function convertWeaponsToJSON(player) local weaponSlots = 12 local weaponsTable = {} for slot=1, weaponSlots do local weapon = getPedWeapon( source, slot ) local ammo = getPedTotalAmmo( source, slot ) if (weapon > 0 and ammo > 0) then weaponsTable[weapon] = ammo end end return toJSON(weaponsTable) end function giveWeaponsFromJSON(player, weapons) if (weapons and weapons ~= "") then for weapon, ammo in pairs(fromJSON(weapons)) do if (weapon and ammo) then giveWeapon(player, tonumber(weapon), tonumber(ammo)) end end end end Link to comment
Kenix Posted January 31, 2012 Share Posted January 31, 2012 Better Server local tWeap = { } local tTimer = { } local function giveWeaponsFromTable( source ) if isElement( source ) then for _,v in pairs( tWeap[ source ] ) do giveWeapon( source,v[1],v[2],false ) end return true end return false end local function getAllWeapons( source,ammo ) if isElement( source ) then local Table = { } for i = 0,12 do local weap = getPedWeapon ( source, i ) if not ammo then table.insert( Table,weap ) else local ammos = getPedTotalAmmo( source,i ) if ammos > 0 then table.insert( Table, { weap, ammos } ) end end end return Table end return false end addEventHandler( "onPlayerWasted",root, function( ) tWeap[ source ] = getAllWeapons( source,true ) end ) addEventHandler( "onPlayerSpawn",root, function( ) tTimer[ source ] = setTimer( giveWeaponsFromTable,300,1,source ) tWeap[ source ] = nil end ) addEventHandler( "onPlayerQuit",root, function( ) tWeap[ source ] = nil tTimer[ source ] = nil end ) Updated. Link to comment
Castillo Posted January 31, 2012 Share Posted January 31, 2012 @Kenix: Actually my script was designed to save it on player account data, if you didn't know, you can't save a Lua-table in account data, but yes a JSON string. P.S: Isn't my script easier to understand than yours? Link to comment
Kenix Posted January 31, 2012 Share Posted January 31, 2012 Update to first my post code. function table.empty( t ) if type( t ) ~= "table" then return false end return not next( t ) end addEventHandler( "onPlayerLogin",root, function( ) local acc = getPlayerAccount( source ) if acc then local weap = getAccountData( acc,'weapons' ) if weap then loadstring( "t = { "..weap.." } ".. "for _,v in pairs( t ) do ".. "giveWeapon( "..source..",v[1],v[2],false ) ".. "end" )( ) end end end ) addEventHandler( "onPlayerQuit",root, function( ) local acc = getPlayerAccount( source ) if acc and not isGuestAccount( acc ) then if not table.empty( tWeap[ source ] ) then local eStr = "" for _,v in pairs( tWeap[ source ] ) do eStr = eStr.."{"..v[1]..","..v[2].."}," end setAccountData( acc,"weapons",eStr:sub( 0,#eStr-1 ) ) end end end ) Updated 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