local eventStarted = false
local playersJoinedEvent = { }
local weaponEvent, weaponAmmo, eventEnded
local adminPositionX, adminPositionY, adminPositionZ
local playersData = { }
function convertPlayerWeaponsToTable(player)
local weapons = { }
for slot = 0, 12 do
local weapon = getPedWeapon( player, slot )
if ( weapon > 0 ) then
local ammo = getPedTotalAmmo( player, slot )
if ( ammo > 0 ) then
weapons[weapon] = ammo
end
end
end
return weapons
end
function playerJoinsEvent ( thePlayer, command )
if ( eventStarted == true and not playersJoinedEvent[thePlayer] and command == "joinevent" ) then
local pX, pY, pZ = getElementPosition ( thePlayer )
local playerSkin = getElementModel ( thePlayer )
playersData[thePlayer] =
{
position = { pX, pY, pZ },
skin = playerSkin,
weapons = convertPlayerWeaponsToTable( thePlayer )
}
if spawnPlayer( thePlayer, adminPositionX, adminPositionY, adminPositionZ, 0, tonumber( skinID ) ) then
playerJoinedEvent = true
giveWeapon ( thePlayer, weaponEvent, tonumber( weaponAmmo ) )
outputChatBox( "You successfully joined the event", thePlayer )
playersJoinedEvent[thePlayer] = true
end
elseif ( eventStarted == false and not playersJoinedEvent[thePlayer] and command == "joinevent" ) then
outputChatBox( "#FF8000 |Event| : There is no event currently ongoing.", thePlayer, 42, 100, 100, true )
elseif ( eventStarted == true and playerJoinedEvent == true and command == "joinevent" ) then
outputChatBox( "#FF8000 |Event| : You are already in the event", thePlayer, 42, 100, 100, true )
end
if ( eventEnded == false and not playersJoinedEvent[thePlayer] and command == "endevent" ) then
loadOriginalData ( thePlayer )
eventEnded = true
end
if ( command == "quitevent" and eventStarted == true and playersJoinedEvent[thePlayer] )then
loadOriginalData ( thePlayer )
outputChatBox( "#008000|Event| : You successfully quit the event.", thePlayer, 42, 100, 100, true )
end
end
addCommandHandler("joinevent",playerJoinsEvent)
addCommandHandler("quitevent",playerJoinsEvent)
function loadOriginalData ( thePlayer )
local savedData = playersData[thePlayer]
if ( savedData and type ( savedData ) == "table" ) then
local x, y, z = unpack ( savedData.position )
playersJoinedEvent[thePlayer] = nil
local spawned = spawnPlayer( thePlayer, x, y, z, 0, savedData.skin)
if (spawned ) then
takeAllWeapons ( thePlayer )
for weapon, ammo in pairs ( savedData.weapons ) do
giveWeapon ( thePlayer, weapon, ammo )
end
end
end
end
function adminStartEvent(thePlayer, command, weaponId, ammoCMD)
if ( eventStarted == false and command == "startevent" and weaponId ~= null ) then
playersData = { }
playersJoinedEvent = { }
--Declaring booleans and variables
weaponAmmo = ammoCMD
weaponEvent = weaponId
eventEnded = false
eventStarted = true
local weaponName = getWeaponNameFromID ( weaponEvent )
adminPositionX, adminPositionY, adminPositionZ = getElementPosition ( thePlayer )
--***********************
outputChatBox( "#FF8000 |Event| : Event started by Admin", getRootElement( ), 42, 100, 100, true )
outputChatBox( "#FF8000 |Event| : Weapon being used ".. weaponName, thePlayer,42, 100, 100, true )
end
if ( eventStarted == false and command == "startevent" and weaponId == null ) then
outputChatBox( "#FF0000 |Event|Syntax|: /startevent [weaponID][ammo]", thePlayer, 42, 100, 100, true )
end
if ( eventStarted == true and command == "endevent" ) then
eventStarted = false
eventEnded = true
playerJoinedEvent = false
outputChatBox( "#FF8000 |Event| : Event ended by an Admin", thePlayer, 42, 100, 100, true )
elseif ( eventStarted == false and command == "endevent" ) then
outputChatBox( "#FF0000 |Event| : There is no event in progress.", thePlayer, 42, 100, 100, true )
end
end
addCommandHandler( "startevent", adminStartEvent )
addCommandHandler( "endevent", adminStartEvent )
addEventHandler( "onPlayerSpawn", getRootElement( ),
function ( )
if ( eventStarted == true and playersJoinedEvent[source] ) then
loadOriginalData ( source )
end
end
)
Edit: Code updated.