--[[
	New Zero-G Handler by Phoenix
	Old = BmanGames
	YEET
]]


local ZGClient = game:GetService("ReplicatedStorage").Game.Events.Game.ZG
local Active = {}

local Deactivate = function(Player, Lab)
	Active[Player] = nil

	local Character = Player.Character
	
	if Character ~= nil then	
		if Character:FindFirstChild("Humanoid") then
			Character.Humanoid.PlatformStand = false
			
			wait()
			
			for Index, Desc in pairs(Character:GetDescendants()) do
				if Desc:FindFirstChild("BodyForce") then
					Desc.BodyForce:Destroy()
				elseif Desc:FindFirstChild("Handle") and Desc.Handle:FindFirstChild("BodyForce") then
					Desc.Handle.BodyForce:Destroy()
				end
			end
	
			ZGClient:FireClient(Player, false, Lab)
		end
	end
end

local Activate = function(Player, Lab)
	Active[Player] = Player

	local Character = Player.Character
	
	if Character:FindFirstChild("Humanoid") then
		for Index, Child in pairs(Character:GetChildren()) do
			if Child:IsA("BasePart") or Child:IsA("MeshPart") then
				local BF = Instance.new("BodyForce")
				BF.force = Vector3.new(0, 196.2, 0) * Child:GetMass()
				BF.Parent = Child
			elseif Child:IsA("Accessory") then
				local BF = Instance.new("BodyForce")
				BF.force = Vector3.new(0, 196.2, 0) * Child.Handle:GetMass()
				BF.Parent = Child.Handle							
			end
		end
		
		Character.Humanoid.PlatformStand = true

		ZGClient:FireClient(Player, true, Lab)

		--if Lab == true then
		--	wait(75) --// Region Check just in case of noclip
			
		--	local Region = Region3.new(RegionPart.Position - (RegionPart.Size/2), RegionPart.Position + (RegionPart.Size/2))
		--	local Found  = false
	
		--	for Index, Ins in pairs(game:GetService("Workspace"):FindPartsInRegion3(Region, RegionPart, math.huge)) do
		--		local DetectedPlayer = game.Players:GetPlayerFromCharacter(Ins.Parent)
				
		--		if Player ~= nil then
		--			if DetectedPlayer == Player then
		--				Found = true
		--			end
		--		end
		--	end
	
		--	if Found == false and Active[Player] ~= nil then
		--		Deactivate(Player)
		--	end
		--end
	end
end


game:GetService("ServerScriptService").Server.Events.ZGSet.Event:Connect(function(Player, State, Lab)
	if Lab == nil then Lab = false end
	
	if State == true then
		Activate(Player, Lab)
	elseif Active[Player] ~= nil then
		Deactivate(Player, Lab)
	end
end)
