local spawnpoints =
{
	--{-2043.9245605469,250.03681945801,27.518507003784},
	{-2043.8293457031,263.13189697266,27.518514633179},
        {206.89907836914,-2267.0266113281,1.2799999713898},
        {225.50675964355,-2267.6848144531,1.2799999713898},
       --{216.5924987793,-2267.7780761719,1.2799999713898},
}
local vehicleDestroyTimers = {}
local validSkins = {0}
local playerVehicles = {}
local function spawn(player)
	if player and isElement(player) then
		local x,y,z,r = unpack(spawnpoints[math.random(1,#spawnpoints)])
		spawnPlayer(player,x+math.random(-3,3),y+math.random(-3,3),z,r,validSkins[math.random(1,#validSkins)],0,0)
		fadeCamera(player, true)
		setCameraTarget(player, player)
		showChat(player, true)
	end
end
local function onJoin()
	spawn(source)
end
local function destroyPlayerVehicles()
	local vehicles = playerVehicles[source]
	for _,vehicle in ipairs(vehicles) do
		if isElement(vehicle) then
			destroyElement(vehicle)
		end
	end
	playerVehicles[source] = nil
end
local function onEnter(player)
	if not playerVehicles[player] then
		playerVehicles[player] = {}
		addEventHandler("onPlayerQuit",player,destroyPlayerVehicles)
	end
end
local function destroyVehicle(vehicle)
	if vehicle and isElement(vehicle) and getElementType(vehicle) == "vehicle" then
		destroyElement(vehicle)
	end
end
local function destroyTimer()
	if vehicleDestroyTimers[source] and isTimer(vehicleDestroyTimers[source]) then killTimer(vehicleDestroyTimers[source]) end
	removeEventHandler("onVehicleEnter",source,destroyTimer)
end
local function onExit()
	local t = tonumber(get("vehicleExpireTime")) or 600000
	vehicleDestroyTimers[source] = setTimer(destroyVehicle,(t > 50 and t or 50),1,source)
	addEventHandler("onVehicleEnter",source,destroyTimer)
end
local function initScript()
	resetMapInfo()
	local players = getElementsByType("player")
	for _,player in ipairs(players) do spawn(player) end
	addEventHandler("onPlayerJoin",root,onJoin)
	addEventHandler("onPlayerWasted",root,onWasted)
end
addEventHandler("onResourceStart",resourceRoot,initScript)